X borders Help Needed

Hi, I’m trying to make an object go to the right but when it hits a certain x position it goes to the left.

the object is at a fixed y position, so it just moves left right and
just goes the opposite way when it hits the x coordinate borders of the screen.It’s a cloud. [import]uid: 54001 topic_id: 10398 reply_id: 310398[/import]

You want it to ping pong between the screen??
[lua]local cloud = display.newImage(“cloud.png”,0,10)
local direction = 1

local function animateCloud(e)
cloud:setReferencePoint(display.TopLeftReferencePoint)
cloud.x = cloud.x + direction
cloud.y=10

if (cloud.x + cloud.contentWidth > display.contentWidth) or cloud.x<0 then
direction = -direction
end
end

Runtime:addEventListener(“enterFrame”,animateCloud)[/lua]

This shall ping pong the could from left to right forever…

I am unsure if that is what you want but I feel your vague question actually means this.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10398 reply_id: 37899[/import]

You want it to ping pong between the screen??
[lua]local cloud = display.newImage(“cloud.png”,0,10)
local direction = 1

local function animateCloud(e)
cloud:setReferencePoint(display.TopLeftReferencePoint)
cloud.x = cloud.x + direction
cloud.y=10

if (cloud.x + cloud.contentWidth > display.contentWidth) or cloud.x<0 then
direction = -direction
end
end

Runtime:addEventListener(“enterFrame”,animateCloud)[/lua]

This shall ping pong the could from left to right forever…

I am unsure if that is what you want but I feel your vague question actually means this.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10398 reply_id: 37900[/import]

Thanks a lot! I got it working. [import]uid: 54001 topic_id: 10398 reply_id: 38010[/import]

So, how do I remove the addEventListener if I just wanted the function to happen for a certain amount of time? [import]uid: 54001 topic_id: 10398 reply_id: 39556[/import]