Change object.gravityScale with a tap

hi,

I just started using Corona and am trying to do something that sounds like it should be easy and I haven’t found an answer yet. 

I’m creating an object, adding physics to it, then setting the gravityScale to zero so that it doens’t fall. All I want is for it to fall when I tap it but it just sits there.

I know that placing addBody in the jetMove function then setting the gravity does what I’m looking for (makes it fall on tap) but I want the object to have a physics body before I tap it.

Is it possible to change object.gravityScale once the object has been created? Thanks!! 

Here’s the code:


local function jetMove(event)

        jet.gravityScale = 1

        print("Tapped ")

        return true

end

jet = display.newImage(“jet.png”)

jet.x = 50

jet.y =75

jet:scale(.35, .35)

jet:addEventListener( “tap”, jetMove )

physics.addBody ( jet, “dynamic”, { bounce = .5, density = 3, friction = .5, shape = jetshape }

jet.gravityScale = 0


For anyone trying to do this same thing, I think I figured it out. Just turn the object into a sensor, change the gravityScale, then change it back from a sensor. Here’s how the code has changed and it seems to have fixed my problem!


local function jetMove(event)

    jet.gravityScale = 1

    jet.isSensor = false

    print("Tapped ")

    return true

end

jet = display.newImage(“jet.png”)

jet.x = 50

jet.y =75

jet:scale(.35, .35)

jet:addEventListener( “tap”, jetMove )

physics.addBody ( jet, “dynamic”, { bounce = .5, density = 3, friction = .5, shape = jetshape } )

jet.gravityScale = 0

jet.isSensor = true


I think it will work if you dont set gravityScale directly in the event listener function. Have you tried creating a timer in the tap listener to change the gracityScale, something like:

[lua]

local function jetMove(event)

timer.performWithDelay( 1, function() jet.gravityScale = 1 end )

end

[/lua]

For anyone trying to do this same thing, I think I figured it out. Just turn the object into a sensor, change the gravityScale, then change it back from a sensor. Here’s how the code has changed and it seems to have fixed my problem!


local function jetMove(event)

    jet.gravityScale = 1

    jet.isSensor = false

    print("Tapped ")

    return true

end

jet = display.newImage(“jet.png”)

jet.x = 50

jet.y =75

jet:scale(.35, .35)

jet:addEventListener( “tap”, jetMove )

physics.addBody ( jet, “dynamic”, { bounce = .5, density = 3, friction = .5, shape = jetshape } )

jet.gravityScale = 0

jet.isSensor = true


I think it will work if you dont set gravityScale directly in the event listener function. Have you tried creating a timer in the tap listener to change the gracityScale, something like:

[lua]

local function jetMove(event)

timer.performWithDelay( 1, function() jet.gravityScale = 1 end )

end

[/lua]