applyForce problem.

So my applyForce isn’t working properly for some reason… Here’s the code…

bullet[bulletCounter]:applyForce( 0, -10, bullet[bulletCounter].x, bullet[bulletCounter].y )

So if i increase that “-10” it wont go faster and if i decrease it it wont go slower… what am i doing wrong?

Is that in an enterFrame listener, because applyForce must be called every frame to apply the force.  If you want an instantaneous kick, use applyLinearImpulse()

Hey, i didnt have it on the enterFrame listener… but now i do and still nothing…

Heres the whole function 

local function showBullet(event) if event.phase == "began" then bullet = display.newImageRect( "bullet.png", 2, 8 ) bullet.x = display.contentCenterX bullet.y = display.contentHeight \* 0.9 physics.addBody( bullet, "dynamic" ) bullet.gravityScale = 0 bullet.myName = "bullet" bullet.isFixedRotation = true elseif event.phase == "ended" then local function shootBullet() bullet:applyLinearImpulse( 0, -1, bullet.x, bullet.y ) end Runtime:addEventListener( "enterFrame", shootBullet ) end end BG:addEventListener( "touch", showBullet )

So when the background is pressed the bullet apears and when released the bullet is supposed to fire at a slow motion speed … but doesn’t seem to want to works…

This is being all done in main.lua

Thanks

I see you’re using globals, so my guess is you’re losing track of the bullet.

And this is wrong:

local function shootBullet() bullet:applyLinearImpulse( 0, -1, bullet.x, bullet.y ) end Runtime:addEventListener( "enterFrame", shootBullet )

This is how you apply force over time in enterFrame:

bullet.enterFrame = function( self ) self:applyForce( 0, -1, self.x, self.y ) end Runtime:addEventListener( "enterFrame", bullet)

Note: Even if you resolve all this, you aren’t going to get the result you want.

If you apply force every frame, forever, the bullet will accelerate forever.  

You could simply use setLinearVelocity instead(), which doesn’t need to be in a enterFrame listener.

local function showBullet(event) if event.phase == "began" then bullet = display.newImageRect( "bullet.png", 2, 8 ) bullet.x = display.contentCenterX bullet.y = display.contentHeight \* 0.9 physics.addBody( bullet, "dynamic" ) bullet.gravityScale = 0 bullet.myName = "bullet" bullet.isFixedRotation = true elseif event.phase == "ended" then bullet:setLinearVelocity( 0, -100 ) -- up at -100 pixels per second end end

Alright! Thanks! Works like a charm now! Now what if i wanted to do something like to bullet shoot to wherever i touched the screen… how would i do that? Btw i love your “red and dead 2” game!

Is that in an enterFrame listener, because applyForce must be called every frame to apply the force.  If you want an instantaneous kick, use applyLinearImpulse()

Hey, i didnt have it on the enterFrame listener… but now i do and still nothing…

Heres the whole function 

local function showBullet(event) if event.phase == "began" then bullet = display.newImageRect( "bullet.png", 2, 8 ) bullet.x = display.contentCenterX bullet.y = display.contentHeight \* 0.9 physics.addBody( bullet, "dynamic" ) bullet.gravityScale = 0 bullet.myName = "bullet" bullet.isFixedRotation = true elseif event.phase == "ended" then local function shootBullet() bullet:applyLinearImpulse( 0, -1, bullet.x, bullet.y ) end Runtime:addEventListener( "enterFrame", shootBullet ) end end BG:addEventListener( "touch", showBullet )

So when the background is pressed the bullet apears and when released the bullet is supposed to fire at a slow motion speed … but doesn’t seem to want to works…

This is being all done in main.lua

Thanks

I see you’re using globals, so my guess is you’re losing track of the bullet.

And this is wrong:

local function shootBullet() bullet:applyLinearImpulse( 0, -1, bullet.x, bullet.y ) end Runtime:addEventListener( "enterFrame", shootBullet )

This is how you apply force over time in enterFrame:

bullet.enterFrame = function( self ) self:applyForce( 0, -1, self.x, self.y ) end Runtime:addEventListener( "enterFrame", bullet)

Note: Even if you resolve all this, you aren’t going to get the result you want.

If you apply force every frame, forever, the bullet will accelerate forever.  

You could simply use setLinearVelocity instead(), which doesn’t need to be in a enterFrame listener.

local function showBullet(event) if event.phase == "began" then bullet = display.newImageRect( "bullet.png", 2, 8 ) bullet.x = display.contentCenterX bullet.y = display.contentHeight \* 0.9 physics.addBody( bullet, "dynamic" ) bullet.gravityScale = 0 bullet.myName = "bullet" bullet.isFixedRotation = true elseif event.phase == "ended" then bullet:setLinearVelocity( 0, -100 ) -- up at -100 pixels per second end end

Alright! Thanks! Works like a charm now! Now what if i wanted to do something like to bullet shoot to wherever i touched the screen… how would i do that? Btw i love your “red and dead 2” game!