Object Transition outside of the screen after Tap Eventlistener (LOGIC)

I said “last post”. It’s the updated version that kinda fixes that problem. But here’s your code updated. (Not trying to sound harsh here haha)

local function shoot(event) local speed = 2 -- Just increase this for faster speed. local xFly = event.x - player.x local yFly = event.y - player.y local diff = xFly \* yFly local normX = xFly / diff local normY = yFly / diff local bullet = display.newCircle( player.x, player.y, 10 ) physics.addBody(bullet, "dynamic") bullet.gravityScale = 0 bullet.isBullet = true bullet:setLinearVelocity(xFly \* speed, yFly \* speed) return true end

For removing the missiles when they go off screen – I usually use the physics way with the borders on the outside but you can so something like this for non-physics –

local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local mRemove = 1 local function removeMissiles() local borderLeft = centerX - actualW/2 - 30 local borderRight = centerX + actualW/2 + 30 local borderUp = centerY - actualH/2 - 30 local borderDown = centerY + actualH/2 + 30 local mX = missile.x local mY = missile.y if mX \<= borderLeft then display.remove( missile[mRemove] ) mRemove = mRemove + 1 elseif mX \>= borderRight then display.remove( missile[mRemove] ) mRemove = mRemove + 1 elseif mY \<= borderUp then display.remove( missile[mRemove] ) mRemove = mRemove + 1 elseif mY \>= borderDown then display.remove( missile[mRemove] ) mRemove = mRemove + 1 end end Runtime:addEventListener( "touch", removeMissiles )

You will need an additional counter like the one i added as “mRemove” in the code above. 

Note i might have messed up on the “<,>,=” so you might have to play with that and get it right.

Just a note: if using the “physics borders” approach, you can save yourself some effort (and three physics objects) by simply creating one physics object which spans the entire screen (or somewhat larger). Make that a sensor object of “kinematic” type and then detect the “ended” collision phase for objects which exit its bounds (“ended” phase).

So basically, instead of having 4 distinct border objects that detect “began” collisions, you have 1 screen-sized object which detects “ended” phases, and when an object leaves that region, you destroy it.

Brent

*face palm* Wow I never realized that.

This works perfect, thanks!

Just the problem with the speed is remaining :slight_smile:

Oh sorry. I didn’t put in the right things. On the setLinearVelocity put normX I stead of xFly and same for yFly.

Yes, I thought that you would mean that, but it doesn’t work.
The missle just goes infront of the player’s ship

Goes where? Sorry I’m not at home for the weekend so I can’t fix any code for now.

The missle just moves about 10 pixels above the ship, regardless where I click on the screen.
And then it doesnt move anymore.

Hmm that’s weird. In the code i provided i didn’t make it stop anywhere.