I'm trying to make a basketball that can bounce off the ground but can also be dragged. However, after adding a bunch of code from the DragPlatforms source code and making it applicable to the basketball, the screen on my simulator just shows black. Why is this? Here's all the drag code I am using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | local function startDrag( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y event.target.bodyType = "kinematic" event.target:setLinearVelocity( 0, 0 ) event.target.angularVelocity = 0 elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false event.target.bodyType = "dynamic" end end end return true end basketball:addEventListener( "touch", startDrag ) |
just check your terminal window for error messages...
I've figured out how to use terminal finally. I tried using terminal on the program that was having issues, and got this message: Syntax error: ... 'eof' expected near 'end'
I don't entirely understand. I know that something is expected to be closer to the 'end' function, but what does the 'eof' mean?
P.S. 'eof' is in the < > brackets.
I just replaced all my code with the drag code from "TimeAnimation". It now works, but the ball is behaving erratically. I try dragging it and it begins to fall with gravity or fly upwards as though gravity was negative (though it somewhat stays with my touch while I'm moving the mouse). Perhaps it's not entirely kinematic while I'm dragging? Here's my drag code (it's really long, just so you know):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | function onMoveCircle(event) local timePassed = event.time - lastTime lastTime = lastTime + timePassed speedY = speedY + gravity basketball.x = basketball.x + speedX*timePassed basketball.y = basketball.y + speedY*timePassed if basketball.x >= screenW - basketball.width*.5 then basketball.x = screenW - basketball.width*.5 speedX = speedX*friction speedX = speedX*-1 --change direction elseif basketball.x <= basketball.width*.5 then basketball.x = basketball.width*.5 speedX = speedX*friction speedX = speedX*-1 --change direction elseif basketball.y >= screenH - basketball.height*.5 then basketball.y = screenH - basketball.height*.5 speedY = speedY*friction speedX = speedX*friction speedY = speedY*-1 --change direction elseif basketball.y <= basketball.height*.5 then basketball.y = basketball.height*.5 speedY = speedY*friction speedY = speedY*-1 --change direction end end -- A general function for dragging objects local function startDrag( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y -- Stop current motion, if any Runtime:removeEventListener("enterFrame", onMoveCircle) -- Start tracking velocity Runtime:addEventListener("enterFrame", trackVelocity) elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then lastTime = event.time Runtime:removeEventListener("enterFrame", trackVelocity) Runtime:addEventListener("enterFrame", onMoveCircle) display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Stop further propagation of touch event! return true end function trackVelocity(event) local timePassed = event.time - prevTime prevTime = prevTime + timePassed speedX = (basketball.x - prevX)/timePassed speedY = (basketball.y - prevY)/timePassed prevX = basketball.x prevY = basketball.y end basketball:addEventListener( "touch", startDrag ) Runtime:addEventListener( "enterFrame", onMoveCircle ) |
cannot see the physics part of ur game.. can I see the addbody code for the basketball ?
Sure. I'll give you everything before the drag code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | local physics = require("physics") physics.start() system.activate( "multitouch" ) basketball = display.newImage( "Basketball.png" ) basketball.x = display.contentWidth / 2; basketball.y = display.contentHeight / 2 basketball.width = 100; basketball.height = 100 physics.addBody( basketball, { density = 1, bounce = 0.5, radius = 45 } ) print( "basketball" ) ground = display.newImage( "sludge.tif" ) ground.x = display.contentWidth / 2; ground.y = 550 physics.addBody( ground, "static", { density = 1, bounce = 0.1 } ) physics.setDrawMode( "hybrid" ) |
try putting
t.bodyType ="static"
in began phase and t.bodyType ="static"
in ended phase
I have seen the code that you have posted... and it should work without using physics... just copy the below code and try running it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | local screenW, screenH = display.stageWidth, display.stageHeight local friction = 0.8 local gravity = .9 local speedX, speedY = 0, 0 local prevX, prevY = 0, 0 local ball = display.newCircle( 0, 0, 25) ball:setFillColor(255, 255, 0) ball.x = screenW*.5 ball.y = 20 function onMoveCircle(event) speedY = speedY + gravity ball.x = ball.x + speedX ball.y = ball.y + speedY if( ball.x >= screenW - ball.width*.5 ) then ball.x = screenW - ball.width*.5 speedX = speedX*friction speedX = speedX*-1 --change direction elseif( ball.x <= ball.width*.5) then ball.x = ball.width*.5 speedX = speedX*friction speedX = speedX*-1 --change direction end if( ball.y >= screenH - ball.height*.5 ) then ball.y = screenH - ball.height*.5 speedY = speedY*friction speedX = speedX*friction speedY = speedY*-1 --change direction elseif( ball.y <= ball.height*.5 ) then ball.y = ball.height*.5 speedY = speedY*friction speedY = speedY*-1 --change direction end end -- A general function for dragging objects local function startDrag( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y -- Stop current motion, if any Runtime:removeEventListener("enterFrame", onMoveCircle) -- Start tracking velocity Runtime:addEventListener("enterFrame", trackVelocity) elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false Runtime:removeEventListener("enterFrame", trackVelocity) Runtime:addEventListener("enterFrame", onMoveCircle) end end -- Stop further propagation of touch event! return true end function trackVelocity() speedX = ball.x - prevX speedY = ball.y - prevY prevX = ball.x prevY = ball.y end ball:addEventListener("touch", startDrag) Runtime:addEventListener("enterFrame", onMoveCircle) |
I figured it out. Using the t.bodyType things you gave me above, I was able to get it to work so that it would be static while I was dragging and dynamic when I let go. Thanks for your kind help! ;) Hope a lot of people have someone like you to help them out when they need it.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
How to Check Runtime Events
how to make appear a sprite.sheet right after another sprite.sheet?
Database management app.
issue with keystore
Using physics with the layout exporter goes wonky,
After changed scene by using SceneManager ,I got error about sprite animation
A valid provisioning profile for this executable was not found. Can't add built app to iPhone
Linking external lua files to main lua file
Bau Cua Ca - A traditional Asian Dice Game now AVAILABLE: