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

Hello!

Currently I’m working on a space shooter game to improve my skills at programming.

Now I’m facing the following problem.

When the user taps somewhere on the screen, the player’s ship should fire a missle in the direction of the tap location (event.x, event.y). But it shouldn’t stop there, it shall pass the screen.

I’ve attached an image for a better understanding:

eRUYWTo.png

How can I make this happen, what’s the logic behind it?

Thanks for the help & best regards

Hi @edwin.pichler,

The solution depends on how you’re moving the missile. Are you using the physics engine, or are you using transitions to move the missiles?

Brent

Ya. So if your using physics you can refer to this thread. My last post. You can use that to shoot your missile. If you don’t understand it i can extract the code to fit your needs.

https://forums.coronalabs.com/topic/62325-spawn-object-easy-way/

Well, I tried to solve it with transitions, but I will try it with the physics engine, because I need it anyways for the collision.

But I’m curious, how is it possible to solve this with transitions? Just want it to know

@InfiSnyp thanks! I will try this

Ok thanks this helped me, but I’m facing 2 problems now.

  1. When I click near the players ship, the missles are fired at lower speed, as when I click further away from the ship.

  2. What’s the best method to delete the objects when they pass the screen borders? Placing invisible rectangles at the borders and add a collision detection to them?

Heres the code for it:

local function shoot(event) local y = event.y - player.y local x = event.x - player.x local bullet = display.newCircle( player.x, player.y, 10 ) physics.addBody(bullet, "dynamic") bullet.gravityScale = 0 bullet.isBullet = true bullet:setLinearVelocity(x, y) return true end

Best regards

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.

Hi @edwin.pichler,

The solution depends on how you’re moving the missile. Are you using the physics engine, or are you using transitions to move the missiles?

Brent

Ya. So if your using physics you can refer to this thread. My last post. You can use that to shoot your missile. If you don’t understand it i can extract the code to fit your needs.

https://forums.coronalabs.com/topic/62325-spawn-object-easy-way/

Well, I tried to solve it with transitions, but I will try it with the physics engine, because I need it anyways for the collision.

But I’m curious, how is it possible to solve this with transitions? Just want it to know

@InfiSnyp thanks! I will try this

Ok thanks this helped me, but I’m facing 2 problems now.

  1. When I click near the players ship, the missles are fired at lower speed, as when I click further away from the ship.

  2. What’s the best method to delete the objects when they pass the screen borders? Placing invisible rectangles at the borders and add a collision detection to them?

Heres the code for it:

local function shoot(event) local y = event.y - player.y local x = event.x - player.x local bullet = display.newCircle( player.x, player.y, 10 ) physics.addBody(bullet, "dynamic") bullet.gravityScale = 0 bullet.isBullet = true bullet:setLinearVelocity(x, y) return true end

Best regards