Cant keep dynamic object on screen while gravity is set at 0

I am very new to coding and am recreating a simple game as seen on the corona youtube channel.  Once i relaunch the simulator the main object “pacmanghost1” flies off the screen.  I have tried to set gravity at 0.  i am not getting any errors but most have something wrong or in the wrong order.  Any help is appreciated. code is below:

thanks

blake


– main.lua


– Your code here

local physics = require “physics”

physics.start()

physics.setGravity(0,0)

local pacmanghost1 = display.newImage( “pacmanghost1.png” )

pacmanghost1 : setFillColor(10, 0, 0)

pacmanghost1.x = 160

pacmanghost1.y = 200

pacmanghost1 : scale(.10, .10)

physics.addBody(pacmanghost1, “dynamic”)

local pacmanghost2 = display.newImage(“pacmanghost2.png”)

pacmanghost2.x = 200

pacmanghost2.y = 300

pacmanghost2 : scale(.10, .10)

physics.addBody(pacmanghost2, “dynamic”)

local pacmanghost3 = display.newImage(“pacmanghost3.png”)

pacmanghost3.x = 250

pacmanghost3.y = 400

pacmanghost3 : setFillColor(0, 10, 0)

pacmanghost3 : scale(.10, .10)

physics.addBody(pacmanghost3, “dynamic”)

function touchScreen ( event )

if event.phase == “began” then

transition.to(pacmanghost1, {time=1000, x=event.x, y=event.y})

end

end

Runtime : addEventListener (“touch”, touchScreen)

–transition.to(pacmanghost1, {x=250, y=250, time=10000})

–local text = display.newText( “Blake’s”, 160, 200, “Arial”, 60 )

–local text = display.newText( “Arcade”, 160, 300, “Arial”, 80 )

function movepacmanghost2()

transition.to(pacmanghost2, {time=1000, x=math.random(20, 290), y=math.random(10, 500), onComplete=movepacmanghost2})

transition.to(pacmanghost3, {time=1000, x=math.random(20, 290), y=math.random(10, 500), onComplete=movepacmanghost3})

end

movepacmanghost2()

function onCollision ( event )

print(“collide!”)

end

Runtime : addEventListener (“collision”, onCollision)

here is a little more info.  all ghosts are dynamic, but if i switch one to static it changes which ghost flies off.  while two remain on screen.  Ive tried setting the gravity two ways:  1- setGravity(0,0) and 2- gravityScale = 0

the second i placed under pacmanghost1 no luck though.

thanks

blake

no ideas?

Are you adding bodies to any other objects?

If you try to overlap two (non-sensor) dynamic physics bodies, they will be ‘pushed’ apart.

My guess is, those ghosts are overlapping.

You can easily see this by making all of the bodies static, and then enable ‘hybrid’ physics draw mode:

physics.setDrawMode( "hybrid" )

You need to make the bodies static so they stay put.  Once you do the above, I’m pretty sure you’ll see overlapping issues.

Regardless, I also see you are scaling the product of newImage() call.

local pacmanghost1 = display.newImage( "pacmanghost1.png" ) pacmanghost1 : scale(.10, .10)

I strongly suggest you NOT use newImage(), but instead use newImageRect() and specify the size of the object that way.  

I think if you create a display object with newImage() and scale it, then add a body, that the body will be the size of the original image.

(I’m not 100% sure about this because I almost never use newImage().)

Finally, please format code when you post it as I’ve done.  It makes helping you easier as the code is more legible.

formatyourcode.jpg

Cheers,

Ed

i add bodies to all three ghosts.  should i try removing bodies from the ghost thats flying off the screen?  currently they are all dynamic because as i understand that was the only way to have them collide?

thanks

blake

funny you mention the newImageRect as i remember seeing that recommended in place of newImage but dont remember why???  I will switch the code.  how do i go about changing the size after newImageRect?  Is the same as before?

Thanks

blake

so after applying physics.setDrawMode( “hybrid” ) i think i know what you mean by overlapping.  There seems to be three different shades of green that are very large most likely the original size of the image and they move as the ghosts move.  so i will apply the newImageRect and see if that solves the problem.

thanks

blake

Please read the docs:

Awesome!!!  Had a few minutes before work and got it to function without an error and all three ghosts are on screen and collisions are being detected.  

Thanks

Blake

here is a little more info.  all ghosts are dynamic, but if i switch one to static it changes which ghost flies off.  while two remain on screen.  Ive tried setting the gravity two ways:  1- setGravity(0,0) and 2- gravityScale = 0

the second i placed under pacmanghost1 no luck though.

thanks

blake

no ideas?

Are you adding bodies to any other objects?

If you try to overlap two (non-sensor) dynamic physics bodies, they will be ‘pushed’ apart.

My guess is, those ghosts are overlapping.

You can easily see this by making all of the bodies static, and then enable ‘hybrid’ physics draw mode:

physics.setDrawMode( "hybrid" )

You need to make the bodies static so they stay put.  Once you do the above, I’m pretty sure you’ll see overlapping issues.

Regardless, I also see you are scaling the product of newImage() call.

local pacmanghost1 = display.newImage( "pacmanghost1.png" ) pacmanghost1 : scale(.10, .10)

I strongly suggest you NOT use newImage(), but instead use newImageRect() and specify the size of the object that way.  

I think if you create a display object with newImage() and scale it, then add a body, that the body will be the size of the original image.

(I’m not 100% sure about this because I almost never use newImage().)

Finally, please format code when you post it as I’ve done.  It makes helping you easier as the code is more legible.

formatyourcode.jpg

Cheers,

Ed

i add bodies to all three ghosts.  should i try removing bodies from the ghost thats flying off the screen?  currently they are all dynamic because as i understand that was the only way to have them collide?

thanks

blake

funny you mention the newImageRect as i remember seeing that recommended in place of newImage but dont remember why???  I will switch the code.  how do i go about changing the size after newImageRect?  Is the same as before?

Thanks

blake

so after applying physics.setDrawMode( “hybrid” ) i think i know what you mean by overlapping.  There seems to be three different shades of green that are very large most likely the original size of the image and they move as the ghosts move.  so i will apply the newImageRect and see if that solves the problem.

thanks

blake

Please read the docs:

Awesome!!!  Had a few minutes before work and got it to function without an error and all three ghosts are on screen and collisions are being detected.  

Thanks

Blake