Need Help with addEventListener

Hi. I am currently making a simple game where I need my “starship/jet” icon to stay in the screen, instead of falling out or flying out of it.

http://imgur.com/vy0L8zO

When I have nothing on line 84, like the picture above, it lets me click on the simulator screen once and the jet flies up and out of view.

When I have           Runtime:removeEventListener(“enterFrame”, jet)        on line 84 like how I thought it was supposed to be, the jet just falls out of the screen and doesn’t fly up when I touch the screen like it did before.

(picture of how I thought it was supposed to be) http://imgur.com/OBTo2WH

if anyone could try to help, that’d be really appreciated (:

thanks!

First of all, welcome to the forums!

Next, posting screen shots of code isn’t a very efficient way for people to help you. Instead you should use “copy and paste”. That is highlight the code in your editor then CTRL-C (Windows) or CMD-C (Mac) then use CTRL-V/CMD-V to paste it as text into your forum post. However, code won’t format well unless you click on the blue <> button in the edit bar (with bold, italics, etc.). Paste your code into the window that pops up and things will be much easier for people trying to help.

And for the specific issue, we need to know more about what you’re trying to do. How is your ship supposed to move?

What action causes it to move?

Should it float on the screen while the background moves behind it? etc If you can describe your goals in more detail we might have a better answer.

But in the mean time, when you make a body “dynamic”, it gets gravity applied to it. It will fall from the top to the bottom at 9.8 meters per second/per second unless you’ve modified the world’s gravity parameters. That is it will fall, getting faster and faster.  Depending on your goals, you can apply a linear velocity to the object to counter gravity, or if you want it to just float, you can always use:

object.gravityScale = 0

(see: https://docs.coronalabs.com/api/type/Body/gravityScale.html) to make physics objects float. But I’m just throwing things out since I don’t know what you’re trying to do.

Rob

thanks for all the newbie tips Rob!

I want the ship to move only on the y-axis (so up and down) while the background moves behind it (which I already set up), and I want it to be controlled by a touch on the screen. 

when you touch the screen, I want the ship to fly up, but then fall back down when you’re no longer touching the screen. I also do not want the ship to be able to fly out visibility. 

on the second image where my code is:

local jet = display.newImage("Starship.png") jet.x = 100 jet.y = 100 jet.width = 50 jet.height = 30 physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=25}) function activateJets(self, event) self:applyForce(0, -2, self.x, self.y) end function touchScreen(event) -- print("touch") if event.phase == "began" then jet.enterFrame = activateJets Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", jet) end end

my ship doesn’t fly when I touch the screen at all. when I start the simulation in Corona and tap on the screen, it will only drop out of view.

then, when I take out “Runtime:removeEventListener(“enterFrame”, jet)” from my code and relaunch the simulator, my ship will fly up when I touch the screen, but it flys out of view and doesn’t come back down into view.

am I making sense yet? I sure hope so. if not, I’ll try harder with my next response.

To stop the ship from flying off the screen, you can create rectangles that are just off screen that span the width/height of the device.

local topWall = display.newRect(display.contentCenterX, -5, display.contentWidth, 10 ) physics.addBody( topWall, "static" ) local bottomWall = display.newRect(display.contentCenterX, display.contentHeight + 5, display.contentWidth, 10 ) physics.addBody(bottomWall, "static" )

You can do the same for making side walls if necessary. You can also hide them by setting their alpha to 0 or setting their .isVisiible property to false, but they should be off screen so it doesn’t really matter.

There are different ways to move your ship. What I would recommend is getting rid of the Runtime listeners and don’t use .addForce(), but just change the objects Y velocity.  See:  https://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Rob

thank you so much. I will try that and get back to you (:

so I did everything you suggested.

I removed the eventlisteners.

I made the top and bottom rectangles.

I gave the ship linear velocity.

now I am working on how to get the ship to respond when I touch the screen. is there any post or something that could help me out on Corona? I’ve never done this without eventlisteners.

thanks Rob, you’ve helped a lot. (:

You may need to play around with this a bit. I’ll explain more in a bit.

function touchScreen(event) -- print("touch") if event.phase == "began" then activatejets( jet, event ) -- though I don't see you using the 2nd parameter end if event.phase == "ended" then -- Runtime:removeEventListener("enterFrame", jet) -- replace with some new function that stops/slows down the force allowing the object -- to fall back end end

Now that said, you will get exactly one burst of force this way. Touching and holding the touch down doesn’t generate additional events. You get one event when the touch starts, one when it ends and if the touch moves you get multiple events.

You can use this tutorial to move you in that direction:  https://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob

First of all, welcome to the forums!

Next, posting screen shots of code isn’t a very efficient way for people to help you. Instead you should use “copy and paste”. That is highlight the code in your editor then CTRL-C (Windows) or CMD-C (Mac) then use CTRL-V/CMD-V to paste it as text into your forum post. However, code won’t format well unless you click on the blue <> button in the edit bar (with bold, italics, etc.). Paste your code into the window that pops up and things will be much easier for people trying to help.

And for the specific issue, we need to know more about what you’re trying to do. How is your ship supposed to move?

What action causes it to move?

Should it float on the screen while the background moves behind it? etc If you can describe your goals in more detail we might have a better answer.

But in the mean time, when you make a body “dynamic”, it gets gravity applied to it. It will fall from the top to the bottom at 9.8 meters per second/per second unless you’ve modified the world’s gravity parameters. That is it will fall, getting faster and faster.  Depending on your goals, you can apply a linear velocity to the object to counter gravity, or if you want it to just float, you can always use:

object.gravityScale = 0

(see: https://docs.coronalabs.com/api/type/Body/gravityScale.html) to make physics objects float. But I’m just throwing things out since I don’t know what you’re trying to do.

Rob

thanks for all the newbie tips Rob!

I want the ship to move only on the y-axis (so up and down) while the background moves behind it (which I already set up), and I want it to be controlled by a touch on the screen. 

when you touch the screen, I want the ship to fly up, but then fall back down when you’re no longer touching the screen. I also do not want the ship to be able to fly out visibility. 

on the second image where my code is:

local jet = display.newImage("Starship.png") jet.x = 100 jet.y = 100 jet.width = 50 jet.height = 30 physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=25}) function activateJets(self, event) self:applyForce(0, -2, self.x, self.y) end function touchScreen(event) -- print("touch") if event.phase == "began" then jet.enterFrame = activateJets Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", jet) end end

my ship doesn’t fly when I touch the screen at all. when I start the simulation in Corona and tap on the screen, it will only drop out of view.

then, when I take out “Runtime:removeEventListener(“enterFrame”, jet)” from my code and relaunch the simulator, my ship will fly up when I touch the screen, but it flys out of view and doesn’t come back down into view.

am I making sense yet? I sure hope so. if not, I’ll try harder with my next response.

To stop the ship from flying off the screen, you can create rectangles that are just off screen that span the width/height of the device.

local topWall = display.newRect(display.contentCenterX, -5, display.contentWidth, 10 ) physics.addBody( topWall, "static" ) local bottomWall = display.newRect(display.contentCenterX, display.contentHeight + 5, display.contentWidth, 10 ) physics.addBody(bottomWall, "static" )

You can do the same for making side walls if necessary. You can also hide them by setting their alpha to 0 or setting their .isVisiible property to false, but they should be off screen so it doesn’t really matter.

There are different ways to move your ship. What I would recommend is getting rid of the Runtime listeners and don’t use .addForce(), but just change the objects Y velocity.  See:  https://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Rob

thank you so much. I will try that and get back to you (:

so I did everything you suggested.

I removed the eventlisteners.

I made the top and bottom rectangles.

I gave the ship linear velocity.

now I am working on how to get the ship to respond when I touch the screen. is there any post or something that could help me out on Corona? I’ve never done this without eventlisteners.

thanks Rob, you’ve helped a lot. (:

You may need to play around with this a bit. I’ll explain more in a bit.

function touchScreen(event) -- print("touch") if event.phase == "began" then activatejets( jet, event ) -- though I don't see you using the 2nd parameter end if event.phase == "ended" then -- Runtime:removeEventListener("enterFrame", jet) -- replace with some new function that stops/slows down the force allowing the object -- to fall back end end

Now that said, you will get exactly one burst of force this way. Touching and holding the touch down doesn’t generate additional events. You get one event when the touch starts, one when it ends and if the touch moves you get multiple events.

You can use this tutorial to move you in that direction:  https://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob