Moving object on collision

Hi,

I am currently creating a new game.
There’s 2 squares, one that moves down, and one that moves right.

Sorry I’m typing this from my phone, I don’t know how to add my following lines in a code typo, but here an approximate on what I wrote:

local squareRight = display.newRect(0,0,50,50)
local squareDown = display.newRect(200,0,50,50)

function touchRight(event)
if event.phase == “began” then
transition.to(squareRight, {time=200, x=squareRight.x+50, y=squareRight.y})

function touchDown(event)
if event.phase == “began” then
transition.to(squareDown, {time=200, x=squareDown.x, y=squareDown.y+50})

Event listners…

Now you can see that after touching the squareRight 4 times, it reaches the squareDown, what I want it to do is to move the squareDown when it reach it.

Is there anyway to do it? There are more box, this is just an example of what I wrote, thank you for your help, and for taking time to read this.

Regards,
Mike

Found a lot of grammar faults lol.

You might want to be a bit more specific about what you’re trying to achieve here. When squareRight is moving, and squareDown is immediately to its right, do you want squareRight to continue right, as squareDown moves down, with some overlap, or would you like squareRight to stop and allow squareDown to move down? How strict do you want their positions to be? Will they always be aligned to an invisible grid, always moving north, south, east and west, or are you looking for movement at a variety of angles. The general approach to take will depend on these factors.

Hi, thank you for your reply

Please check the image in the link http://postimg.org/image/m5cc333ix/

The blue one moves right, the red one moves down

When squareRight reaches squareDown coordinates, squareDown moves to the right automatically.

That doesn’t really clear things up. Nevertheless, you might find this helpful:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local squareRight = display.newRect(0,0,50,50) local squareDown = display.newRect(200,0,50,50) physics.addBody(squareRight, "dynamic", {radius = 5, isSensor = true}) physics.addBody(squareDown, "dynamic", {radius = 5, isSensor = true}) function squareRight:move() transition.to(self, {time = 200, x = self.x + 50}) end function squareDown:move() transition.to(self, {time = 200, y = self.y + 50}) end local function onTap(event) event.target:move() end local function onCollision(event) if event.phase == "began" then squareDown:move() end end Runtime:addEventListener("collision", onCollision) squareRight:addEventListener("tap", onTap) squareDown:addEventListener("tap", onTap)

Thank you, but no that’s not what I meant, when the squareRight reaches squareDown, I want squareDown to move 50 pixels on the x axis not the y one.

But you said you wanted it to move down. That’s the y axis.

Well, you just need to change the transition in squareDown:move(), then. Use x instead of y.
 

EDIT: oh, I see, you meant it moves down when you touch it.

Yes, have you got my question?

You could change your move functions to take some arguments:

function squareDown:move(direction, amount) if direction == "x" then transition.to(self, {time = 200, x = self.x + amount}) else transition.to(self, {time = 200, y = self.y + amount}) end end

Then you’ll need to change the calls in the tap and collision functions accordingly.

Found a lot of grammar faults lol.

You might want to be a bit more specific about what you’re trying to achieve here. When squareRight is moving, and squareDown is immediately to its right, do you want squareRight to continue right, as squareDown moves down, with some overlap, or would you like squareRight to stop and allow squareDown to move down? How strict do you want their positions to be? Will they always be aligned to an invisible grid, always moving north, south, east and west, or are you looking for movement at a variety of angles. The general approach to take will depend on these factors.

Hi, thank you for your reply

Please check the image in the link http://postimg.org/image/m5cc333ix/

The blue one moves right, the red one moves down

When squareRight reaches squareDown coordinates, squareDown moves to the right automatically.

That doesn’t really clear things up. Nevertheless, you might find this helpful:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local squareRight = display.newRect(0,0,50,50) local squareDown = display.newRect(200,0,50,50) physics.addBody(squareRight, "dynamic", {radius = 5, isSensor = true}) physics.addBody(squareDown, "dynamic", {radius = 5, isSensor = true}) function squareRight:move() transition.to(self, {time = 200, x = self.x + 50}) end function squareDown:move() transition.to(self, {time = 200, y = self.y + 50}) end local function onTap(event) event.target:move() end local function onCollision(event) if event.phase == "began" then squareDown:move() end end Runtime:addEventListener("collision", onCollision) squareRight:addEventListener("tap", onTap) squareDown:addEventListener("tap", onTap)

Thank you, but no that’s not what I meant, when the squareRight reaches squareDown, I want squareDown to move 50 pixels on the x axis not the y one.

But you said you wanted it to move down. That’s the y axis.

Well, you just need to change the transition in squareDown:move(), then. Use x instead of y.
 

EDIT: oh, I see, you meant it moves down when you touch it.

Yes, have you got my question?

You could change your move functions to take some arguments:

function squareDown:move(direction, amount) if direction == "x" then transition.to(self, {time = 200, x = self.x + amount}) else transition.to(self, {time = 200, y = self.y + amount}) end end

Then you’ll need to change the calls in the tap and collision functions accordingly.