need help with changing velocity values during game

I need help trying to set up a velocity that increases when player hits an object. I cannot get this to work, so wanted to see if I am doing this right, or whether corona/ LUA can do this.

The velocity is basically for the movement of the player. I just want it to start getting heavier.

Here is what I have:

global values

local size = 0

function update(event)

if(dir == 'up' and size \<2) then ship:setLinearVelocity(0, -200) elseif(dir == 'up' and size \>2)then ship:setLinearVelocity(0, -40) elseif(dir == 'left') then ship:setLinearVelocity(-100, 0) elseif(dir == 'right') then ship:setLinearVelocity(100, 0) end 

function collisionHandler(e)

if(e.other.name == 'ship' and e.target.name == 'enemy') then display.remove(e.target) ship.width = ship.width +10 ship.height = ship.height +10 size = size +1 print("size 1") end

thanks.

Maybe something like this?

[lua]

if(dir == ‘up’) then

        ship:setLinearVelocity(0, -200/ship.size)

    elseif(dir == ‘left’) then

            ship:setLinearVelocity(-100/ship.size, 0)

    elseif(dir == ‘right’) then

            ship:setLinearVelocity(100/ship.size, 0)

    end    

[/lua]

Basically, I divided your velocities by the ship’s size, so as the ship gets bigger, the velocity gets lower (it gets heavier).

  • Andrew

Thanks for the help aukStudios. I just tried out your code and it comes up with a Runtime error: Attempt to perform arithmetic on field ‘size’ (a nil value)

Apparently its an issue with the bottom part of the code, 

 elseif(dir == 'right') then ship:setLinearVelocity(100/ship.size, 0) \<\<\< error line.

Any idea whats wrong?

Sorry, I mistakenly thought that size was a property of the ship, whereas you’ve set it as a variable in the entire module.  So in my code, remove “ship.” and just divide by size directly, like “ship:setLinearVelocity(100/size, 0)”.

  • Andrew

Its fixed the error but as soon as I start the game and press a directional button, the player image becomes invisible. There is no error printed in the terminal either.

Try setting size = 1 at the beginning of your code instead of size = 0 and see if that helps.

That did fixed it. Works great now. Thank you so much for your help and time Andrew.

Cool, no problem.  For reference, the reason we had to change it to size = 1 is that, if size were 0, we’d be dividing be zero when we try to set the velocity.  That would make the velocity infinite, which is why the ship disappeared from the screen instantly.

  • Andrew

I see, I ll remember that for next time if a similar problem happens again. Thanks again, much appreciated.

Maybe something like this?

[lua]

if(dir == ‘up’) then

        ship:setLinearVelocity(0, -200/ship.size)

    elseif(dir == ‘left’) then

            ship:setLinearVelocity(-100/ship.size, 0)

    elseif(dir == ‘right’) then

            ship:setLinearVelocity(100/ship.size, 0)

    end    

[/lua]

Basically, I divided your velocities by the ship’s size, so as the ship gets bigger, the velocity gets lower (it gets heavier).

  • Andrew

Thanks for the help aukStudios. I just tried out your code and it comes up with a Runtime error: Attempt to perform arithmetic on field ‘size’ (a nil value)

Apparently its an issue with the bottom part of the code, 

 elseif(dir == 'right') then ship:setLinearVelocity(100/ship.size, 0) \<\<\< error line.

Any idea whats wrong?

Sorry, I mistakenly thought that size was a property of the ship, whereas you’ve set it as a variable in the entire module.  So in my code, remove “ship.” and just divide by size directly, like “ship:setLinearVelocity(100/size, 0)”.

  • Andrew

Its fixed the error but as soon as I start the game and press a directional button, the player image becomes invisible. There is no error printed in the terminal either.

Try setting size = 1 at the beginning of your code instead of size = 0 and see if that helps.

That did fixed it. Works great now. Thank you so much for your help and time Andrew.

Cool, no problem.  For reference, the reason we had to change it to size = 1 is that, if size were 0, we’d be dividing be zero when we try to set the velocity.  That would make the velocity infinite, which is why the ship disappeared from the screen instantly.

  • Andrew

I see, I ll remember that for next time if a similar problem happens again. Thanks again, much appreciated.