Collision Question...

Im been stuck for a while trying to get this to work. Basically I have this object which floats about randomly and moves through the screen. when the player touches it, I want that floating object to revert back to its starting position or another random location so that it can float in again. 

Any ideas how i’d go about doing this? is there a better way to do this? is there a code I need to put into the collision which makes the object basically spawn again?

Right now all the happens is when it floats of screen it goes back to beginning again. 

function moveUFO(self,event) if self.x \< -50 then self.x = math.random(500,1500) self.y = math.random(90,220) self.speed = math.random(2,6) self.amp = math.random(20,100) self.angle = math.random(1,360) else self.x = self.x - self.speed self.angle = self.angle + .1 self.y = self.amp\*math.sin(self.angle)+self.initY end end

Hi @dip,

Do you want to re-spawn the object, or just move it? I assume if it’s moving off the screen, you want to just move it back (versus removing and recreating it, which I would not suggest in this case). By “collision” I assume you mean a touch/tap listener, since I don’t see any use of the physics engine here. What does your touch listener look like?

Brent

Hi Brent thanks for the reply. I do want to just move it back to a random x location after it hits the player so that it floats in again ready for another collision. basically I plan to increment score by 1 every time you collide into it. I am making use of the physics engine, mostly for the player controlled ship.

It is a side scroller and you move the ship up and down through touch controls. I have a collision on the player ship set up.

function ship:collision(event) if (event.other.name == 'star') then scoreAnim = display.newText('+10', ship.x, ship.y-10, native.systemFontBold, 16) transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end}) scoreAnim:setTextColor(0,255,12) Runtime:removeEventListener("enterFrame", star) --increases score collectedN.text = tostring(tonumber(collectedN.text) + 1)

and the listener for it is 

ship:addEventListener("collision", onCollision, ship, addTime)

and touch controls 

Runtime:addEventListener("touch" , touchScreen)

Hi @dip,
At first glance, this appears to be just a minor coding error. If this is a local collision (and it is, because you’re putting the collision listener on the ship, not the global Runtime), then the correct code is:

[lua]
ship.collision = onCollision
ship:addEventListener( “collision”, ship )
[/lua]

 
And the function above that should be named this:

[lua]

local function onCollision( self, event )

[/lua]

Try this and see if it works! :slight_smile:

Brent

thanks Brent, that seems to work the way I need.

However is there a way I can lock the player in the in X axis? It seems that whenever I collide with the object The ship starts to get bumped out of position and thus fall behind the scrolling function. I only need the player to move up and down on the X axis, no moving forward or backward. 

How about just setting the ship as a sensor object? That way, collisions would be sensed, but the ship wouldn’t have a collision force applied to it (and thus, it wouldn’t be knocked off its X axis).

Brent

Yeah that seems a lot better for what I need. Thanks for the help Brent. Much appreciated.

Hi @dip,

Do you want to re-spawn the object, or just move it? I assume if it’s moving off the screen, you want to just move it back (versus removing and recreating it, which I would not suggest in this case). By “collision” I assume you mean a touch/tap listener, since I don’t see any use of the physics engine here. What does your touch listener look like?

Brent

Hi Brent thanks for the reply. I do want to just move it back to a random x location after it hits the player so that it floats in again ready for another collision. basically I plan to increment score by 1 every time you collide into it. I am making use of the physics engine, mostly for the player controlled ship.

It is a side scroller and you move the ship up and down through touch controls. I have a collision on the player ship set up.

function ship:collision(event) if (event.other.name == 'star') then scoreAnim = display.newText('+10', ship.x, ship.y-10, native.systemFontBold, 16) transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end}) scoreAnim:setTextColor(0,255,12) Runtime:removeEventListener("enterFrame", star) --increases score collectedN.text = tostring(tonumber(collectedN.text) + 1)

and the listener for it is 

ship:addEventListener("collision", onCollision, ship, addTime)

and touch controls 

Runtime:addEventListener("touch" , touchScreen)

Hi @dip,
At first glance, this appears to be just a minor coding error. If this is a local collision (and it is, because you’re putting the collision listener on the ship, not the global Runtime), then the correct code is:

[lua]
ship.collision = onCollision
ship:addEventListener( “collision”, ship )
[/lua]

 
And the function above that should be named this:

[lua]

local function onCollision( self, event )

[/lua]

Try this and see if it works! :slight_smile:

Brent

thanks Brent, that seems to work the way I need.

However is there a way I can lock the player in the in X axis? It seems that whenever I collide with the object The ship starts to get bumped out of position and thus fall behind the scrolling function. I only need the player to move up and down on the X axis, no moving forward or backward. 

How about just setting the ship as a sensor object? That way, collisions would be sensed, but the ship wouldn’t have a collision force applied to it (and thus, it wouldn’t be knocked off its X axis).

Brent

Yeah that seems a lot better for what I need. Thanks for the help Brent. Much appreciated.