I’m trying to build a simple snowball game. Two guys, one snowball. When the snowball hits the opposite guy or the floor, it should disappear and re-appear in the hand of the throwing guy.
Sounds simple. But I’m having problems with re-initializing the snowball after it hit the bottom or the other guy. My code looks like this:
.....
......
local floor = ........
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode( "normal" )
local snowball = display.newImage("snowball.png")
snowball.x = 50
snowball.y = display.stageHeight - 60
physics.addBody( snowball, { density=0.3, friction=0.3, bounce=0.1, radius=20 } )
snowball:addEventListener( "touch", dragBody )
local function dragBody( event )
physics.setGravity(0, 10)
gameUI.dragBody( event, { maxForce=55, frequency=5, dampingRatio=10} )
end
local function onCollision( event )
if event.object1 == floor then
if ( event.phase == "began" ) then
local oldsnowball = event.object2
oldsnowball:removeSelf()
local snowball = display.newImage("snowball.png")
snowball.x = 50
snowball.y = display.stageHeight - 60
snowball:addEventListener( "touch", dragBody )
end
end
end
The problem is: the new snowball appears but as soon as I try to drag it, the whole Corona Simulator crashes. So I can only drag the first snowball. If I try to re-add the new snowball to the physics with physics.addBody in onCollision(), the game crashes as well.
Do you guys have any hints what I’m doing wrong? [import]uid: 11219 topic_id: 3616 reply_id: 303616[/import]
local function dragBody( event )
physics.setGravity(0, 10)
gameUI.dragBody( event, { maxForce=55, frequency=5, dampingRatio=10} )
end
local function createSnowBall
– probably doesnt need to be local… refer to the original object instead
snowball = display.newImage(“snowball.png”)
snowball.x = 50
nowball.y = display.stageHeight - 60
snowball:addEventListener( “touch”, dragBody )
end
local function onCollision( event )
if event.object1 == floor then
if ( event.phase == “began” ) then
local oldsnowball = event.object2
oldsnowball:removeEventListenert(“touch”, dragBody)
oldsnowball:removeSelf()
createSnowBall()
end
end
end[/lua]
(altho note there is no real reason to destroy the snowball, if nothing is changing (physics/image) etc… just move it to a new position and reset velocities etc)
[import]uid: 6645 topic_id: 3616 reply_id: 10996[/import]
Thanks a lot for your answer, I got it working now with re-initializing the ball. The problem seems to be that I have to wait a bit before reinitializing the ball and adding event listeners / physics to it. I’ve now put a timer in that recreates the ball some time after onCollision was called but not instantly.
I’ve tried not-reinitializing and simply moving the snowball to the original position before by setting its x and y values but that didn’t work.
Another thing: as you can see I’m using gameUI for dragging the snowball (after drag it’s released and then flies through the air). The game works nicely in the simulator with every possible device, whether android or iphone. But on the two android devices I got here (HTC myTouch, HTC Hero) I can’t drag the snowball at all. It simply stands still on its starting point and doesn’t move.
As if I would never release or move my finger. But the game recognizes that I’m pressing the screen cause the snowball is then “swinging” on the original point with the head down.
[import]uid: 11219 topic_id: 3616 reply_id: 11001[/import]
OK i now found out that it must have something to do with the size of the background picture. if it’s 1000x800 for example, the game will run very slow on android. i have to resize the picture to almost the original display size for the game to run smoothly.
let’s see whether i can get gameui-drag running again with this setup, right now i’m using touch events and applyForce calls. [import]uid: 11219 topic_id: 3616 reply_id: 11007[/import]
OK i now found out that it must have something to do with the size of the background picture. if it’s 1000x800 for example, the game will run very slow on android. i have to resize the picture to almost the original display size for the game to run smoothly.
let’s see whether i can get gameui-drag running again with this setup, right now i’m using touch events and applyForce calls. [import]uid: 11219 topic_id: 3616 reply_id: 11008[/import]
Ah ok, thanks.
Could be that I’m having another problem combined to this bug.
Right now I’m doing the following:
the user touches the snowball
with the “began” and “ended” events i calculate how long the user touched the snowball
depending on the time the user touched it, the snowball is thrown with applyForce
–> each 4 or 5 throws on android, the time the user touched the snowball is way higher than i actually touched it. showing nearly 2 seconds of touching time although i only touched it for half a second or shorter.
–> seems to be that exact bug you mentioned? [import]uid: 11219 topic_id: 3616 reply_id: 11023[/import]