Help Please, 'applyForce' (a nil value)

Hi

I’m currently in the process of creating a game which is similar to the helicopter game on blackberry where the goal is to avoid the mines and survive till the timer counts to 0, when this is achieved the game goes to the next level where it will become more difficult. To do this you must touch the screen to make the spaceship fly higher(through applyForce)or let it drop lower(through gravity level) as the ship is a dynamic object. The code for this is below.

[lua]local function flightUp(self,event)
print(“Just before apply force”)
self:applyForce(0,-0.2,self.x,self.y)
print(applyForce)
audio.play(jetSound)
end

function touchS(event)
if event.phase == “began” then
ship.enterFrame = flightUp
Runtime:addEventListener(“enterFrame”, ship)

end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, ship)
end
end[/lua]

I then created the level 2 by copying the code for level 1 and changing a few values into a new lua file. and when using almost similar code i get the error “attempt to call method ‘applyForce’(a nil value)” making my spaceship on screen but unable to fly or drop through gravity.

Apart from this error the level2 lua has no errors until "touchS1"is called.

[lua]local function flightUp1(self,event)
print(“This is the flight up1”)

self:applyForce(0,-0.2,self.x,self.y)
end

function touchS1(event)
if event.phase == “began” then
ship.enterFrame = flightUp1
Runtime:addEventListener(“enterFrame”, ship)

end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, ship)
end
end[/lua]

Someone help me please

Just to be clear, is it working correctly in your first block of code (level 1)? Or are you getting the error in both cases? My guess would be that you’ve perhaps forgotten to attach a physics body to the ship in level 2, but that’s just a guess.

Yes everything is working perfectly fine within the level1.lua. I only get the error in level 2.lua. An i have attached a physical body to the ship however i believe you’re on the right path because even when i comment out the function ‘flightUp1’ my ship still has no physical body. I have no idea why because the code for the physical body has been placed in level2.lua

[lua]ship = display.newImage( “spaceship.png”)

ship.x = 60
ship.y = 140
physics.addBody( ship, “dynamic”, {density = 2,bounce = 0.1, friction=2, radius= 1 } )

[/lua]

If your code is exactly the same as the code you just posted, it could be caused by “ship” being a global variable ( i.e. it’s not local ship = display…). If the ship in both levels is called “ship”, then perhaps the function is being called on the ship from level 1, which no longer exists?

Thank you very much, it actually was that.

I forgot that i needed to make it a local variable.

You have been a big help

Just to be clear, is it working correctly in your first block of code (level 1)? Or are you getting the error in both cases? My guess would be that you’ve perhaps forgotten to attach a physics body to the ship in level 2, but that’s just a guess.

Yes everything is working perfectly fine within the level1.lua. I only get the error in level 2.lua. An i have attached a physical body to the ship however i believe you’re on the right path because even when i comment out the function ‘flightUp1’ my ship still has no physical body. I have no idea why because the code for the physical body has been placed in level2.lua

[lua]ship = display.newImage( “spaceship.png”)

ship.x = 60
ship.y = 140
physics.addBody( ship, “dynamic”, {density = 2,bounce = 0.1, friction=2, radius= 1 } )

[/lua]

If your code is exactly the same as the code you just posted, it could be caused by “ship” being a global variable ( i.e. it’s not local ship = display…). If the ship in both levels is called “ship”, then perhaps the function is being called on the ship from level 1, which no longer exists?

Thank you very much, it actually was that.

I forgot that i needed to make it a local variable.

You have been a big help