function not executing

So after getting corona yesterday I have been hard at work creating my first app. :)  It was all going so well until I have come across this one problem :frowning:

Basically I have a ball rolling across the screen:

function onEveryFrame(event)

enemy:applyForce( 0.1,0 )

end

Runtime:addEventListener( “enterFrame”, onEveryFrame )

This works fine and the ball rolls forever and ever which is intended.

Now the section which doesn’t seem to work comes straight afterwards

function slowDown(self,event)

self.applyForce( -100,0, self.x, self.y )

end

function touchScreen(event)

print(“Touch”)

if event.phase == “began” 

then enemy.enterFrame = slowDown

Runtime:addEventListener(“enterFrame”, enemy)

end

if event.phase == “ended” 

then Runtime:removeEventListener(“enterFrame”, enemy)

end

end

Runtime:removeEventListener(“enterFrame”, touchScreen)

This section is basically meant to slow the ball down on each screen click, but doesn’t do anything :(  

I have tried changing the slowDown function to:

function slowDown(event)

enemy:applyForce( -100,0)

_ end _

But this still does not work so the ball does not slow down, even the print doesn’t work yet no errors and the simulator runs fine as if this section of code just doesn’t exist?

here is the whole section together

Any help appreciated greatly!!!  :slight_smile:

hey matt im pretty sure u already found it out, but i believe there error there is that you never start the listener for “touchScreen” (you just remove it)

Im not sure if anything else is wrong, i would have to test it myself if that doesnt fix it :stuck_out_tongue:

Basically you never call touchScreen

Edit: also if you just plan to leave it there, i would sugest you to use a “tap” event instead of a “touch” one, to make it “easier”.

Hi @matthewb30,

As @tuku473 said, touch listener should be object:addEventListener(“touch”, touchScreen).

Anyway try this edited code. 

physics= require( "physics") physics.start( ) physics.setGravity( 0, 0 ) enemy= display.newRect( 50, 50, 50, 50 ) enemy.x=50 enemy.y=100 physics.addBody(enemy,"dynamic",{density=0,friction=0,bounce=0}) --function onEveryFrame(event) enemy:applyForce( 0.9,0 ) --end --Runtime:addEventListener( "enterFrame", onEveryFrame ) -- you don't need this since you are using physics. function slowDown(event) -- your code below will cause the enemy to move opposite not slow down --enemy:applyForce( -0.1,0, enemy.x, enemy.y ) -- get velocity of enemy enemy.vx, enemy.vy = enemy:getLinearVelocity() print("enemy velocity before", enemy.vx, enemy.vy) enemy:setLinearVelocity(enemy.vx - 0.4 , 0 ) --set velocity of enemy adjust 0.1 to your req. -- for checking your enemy velocity --if goes negative enemy will move opposite. enemy.vx2, enemy.vy2 = enemy:getLinearVelocity() print("enemy velocity after touch", enemy.vx2, enemy.vy2) end function touchScreen(event) print("Touch") if event.phase == "began" then enemy.enterFrame = slowDown Runtime:addEventListener("enterFrame", enemy) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", enemy) end end enemy:addEventListener("touch", touchScreen)

Good Luck!

burhan

Right I have this all set up now and it works so thank you :) 

what change would I have to make to change what you have to touch for the enemy to slow down? Currently it only slows down when you touch the enemy yet I need it to slow down when you touch anywhere on the screen?

Hey just change “enemy:addEventListener()” for “Runtime:addEventListener()” or any object you want to use to detect the movement of your enemy (the runtime will detect any touch on the screen)

hey matt im pretty sure u already found it out, but i believe there error there is that you never start the listener for “touchScreen” (you just remove it)

Im not sure if anything else is wrong, i would have to test it myself if that doesnt fix it :stuck_out_tongue:

Basically you never call touchScreen

Edit: also if you just plan to leave it there, i would sugest you to use a “tap” event instead of a “touch” one, to make it “easier”.

Hi @matthewb30,

As @tuku473 said, touch listener should be object:addEventListener(“touch”, touchScreen).

Anyway try this edited code. 

physics= require( "physics") physics.start( ) physics.setGravity( 0, 0 ) enemy= display.newRect( 50, 50, 50, 50 ) enemy.x=50 enemy.y=100 physics.addBody(enemy,"dynamic",{density=0,friction=0,bounce=0}) --function onEveryFrame(event) enemy:applyForce( 0.9,0 ) --end --Runtime:addEventListener( "enterFrame", onEveryFrame ) -- you don't need this since you are using physics. function slowDown(event) -- your code below will cause the enemy to move opposite not slow down --enemy:applyForce( -0.1,0, enemy.x, enemy.y ) -- get velocity of enemy enemy.vx, enemy.vy = enemy:getLinearVelocity() print("enemy velocity before", enemy.vx, enemy.vy) enemy:setLinearVelocity(enemy.vx - 0.4 , 0 ) --set velocity of enemy adjust 0.1 to your req. -- for checking your enemy velocity --if goes negative enemy will move opposite. enemy.vx2, enemy.vy2 = enemy:getLinearVelocity() print("enemy velocity after touch", enemy.vx2, enemy.vy2) end function touchScreen(event) print("Touch") if event.phase == "began" then enemy.enterFrame = slowDown Runtime:addEventListener("enterFrame", enemy) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", enemy) end end enemy:addEventListener("touch", touchScreen)

Good Luck!

burhan

Right I have this all set up now and it works so thank you :) 

what change would I have to make to change what you have to touch for the enemy to slow down? Currently it only slows down when you touch the enemy yet I need it to slow down when you touch anywhere on the screen?

Hey just change “enemy:addEventListener()” for “Runtime:addEventListener()” or any object you want to use to detect the movement of your enemy (the runtime will detect any touch on the screen)