How to change the properties of a physics object little by little

Hi everyone…

I have a circle with some bounce

    local myCircle = display.newCircle( 0, 0, 30 )     group:insert(myCircle)     myCircle.x = display.contentCenterX     myCircle.y = display.contentCenterY - 150     myCircle:setFillColor( 1, 1, 1 )     physics.addBody(myCircle, "dinamic", {density = 1.0, friction = 0.3, bounce = .9, radius=30, isSensor = false})

Then I have a rect as abutton, when I tap on that button

I would like a function to lower the bounce property, like bounce.8, bounce.7, bounce.6 everytime I tap

    local lessBounce = display.newRect( 0, 0, 20, 20 )     group:insert(lessBounce)     lessBounce.x = display.contentCenterX - 100     lessBounce.y = display.contentCenterY + 165     lessBounce:setFillColor( 1, 1, 1 )     local function lessBounceListener()             print("less bounce")                  end     lessBounce:addEventListener("tap", lessBounceListener)

Or more bounce, how would I do that?

Thanks

I’m pretty new to Corona but the easiest way, I believe, would be to call a physics.removeBody( object ) and then add the physics.addBody( object ) again with your new properties.  Just make sure to not call it during a collision.

Hi PixelJump…

I am also kind of new at Corona, and that works fine…

The problem is that I want to increment the bounce property

If I have a button, and tap it. the bounce is

– bounce = .3

if I tap again

– bounce = .4

if I tap again

– bounce = .5

Like that.

Your solution works only 1 time

I have it at

– bounce = .2

tap

remove – bounce = .2

and set it a new at – bounce = .3

but if I tap again

it will remove the – bounce = .3

but it will set it again at – bounce = .3

because it’s the same function

That is the problem, I hope we can solve this together, or with someone else help

I’m not sure if this would work but could you have a variable that held the bounce value and increased it to do something like the following?

newBounce = newBounce + 0.1 physics.addBody(myCircle, "dynamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=30, isSensor = false}) 

I can’t try this myself right now but I don’t see why it wound’t work?!

Here is the code

local lessBounce = display.newRect( 0, 0, 20, 20 )     group:insert(lessBounce)     lessBounce.x = display.contentCenterX - 100     lessBounce.y = display.contentCenterY + 150     lessBounce:setFillColor( 1, 1, 1 )     local function lessBounceListener()             print("less bounce")             local newBounce = newBounce + 0.1             physics.removeBody(myCircle)             physics.addBody(myCircle, "dinamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=20, isSensor = false})                  end     lessBounce:addEventListener("tap", lessBounceListener)

I get this error

Attempt to perform arithmetic on global ‘newBounce’ (a nil value)

I guess the bounce = 4

only accepts numbers, not variables… I don’t know


I think I got it in a little different way, but it works

    local newBounce = 1.2     local function lessBounceListener()             print("less bounce")             physics.removeBody(myCircle)             physics.addBody(myCircle, "dinamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=20, isSensor = false})             newBounce = newBounce - .01                  end     lessBounce:addEventListener("tap", lessBounceListener)

Now if I keep tapping the button the bounce goes less and less

I think we got it!

Thanks

Figured it had to work… good job!  :slight_smile:

Thanks, check my other post, to see if you can help

I’m pretty new to Corona but the easiest way, I believe, would be to call a physics.removeBody( object ) and then add the physics.addBody( object ) again with your new properties.  Just make sure to not call it during a collision.

Hi PixelJump…

I am also kind of new at Corona, and that works fine…

The problem is that I want to increment the bounce property

If I have a button, and tap it. the bounce is

– bounce = .3

if I tap again

– bounce = .4

if I tap again

– bounce = .5

Like that.

Your solution works only 1 time

I have it at

– bounce = .2

tap

remove – bounce = .2

and set it a new at – bounce = .3

but if I tap again

it will remove the – bounce = .3

but it will set it again at – bounce = .3

because it’s the same function

That is the problem, I hope we can solve this together, or with someone else help

I’m not sure if this would work but could you have a variable that held the bounce value and increased it to do something like the following?

newBounce = newBounce + 0.1 physics.addBody(myCircle, "dynamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=30, isSensor = false}) 

I can’t try this myself right now but I don’t see why it wound’t work?!

Here is the code

local lessBounce = display.newRect( 0, 0, 20, 20 )     group:insert(lessBounce)     lessBounce.x = display.contentCenterX - 100     lessBounce.y = display.contentCenterY + 150     lessBounce:setFillColor( 1, 1, 1 )     local function lessBounceListener()             print("less bounce")             local newBounce = newBounce + 0.1             physics.removeBody(myCircle)             physics.addBody(myCircle, "dinamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=20, isSensor = false})                  end     lessBounce:addEventListener("tap", lessBounceListener)

I get this error

Attempt to perform arithmetic on global ‘newBounce’ (a nil value)

I guess the bounce = 4

only accepts numbers, not variables… I don’t know


I think I got it in a little different way, but it works

    local newBounce = 1.2     local function lessBounceListener()             print("less bounce")             physics.removeBody(myCircle)             physics.addBody(myCircle, "dinamic", {density = 1.0, friction = 0.3, bounce = newBounce, radius=20, isSensor = false})             newBounce = newBounce - .01                  end     lessBounce:addEventListener("tap", lessBounceListener)

Now if I keep tapping the button the bounce goes less and less

I think we got it!

Thanks

Figured it had to work… good job!  :slight_smile:

Thanks, check my other post, to see if you can help