Having a hard time with logic to move object back and forth.

I am trying to move a platform back and forth, but my logic to do so is not quite there. 

Any help with guiding me to the correct approach is appreciated.

Here is my code. 

local function movePlatform(self, event) if self.x ~= screenWidth - 110 then self.x = self.x +1 end end

So the above code moves the platform to the right then stops, that was the easy part. I’m having a hard time with bringing the object back to its original location only to move over again, so the platform would move side to side.

The movePlatform function triggers on a Runtime enterFrame event listener. 

You could add an extra property to the object, stating which direction is should move in:

local platform = display.newObject(blah, blah, blah) platform.direction = 1 local function movePlatform(self, event) self.x = self.x + self.direction if self.x \>= maxRightPosition or self.x \<= maxLeftPosition then self.direction = self.direction \* -1 end end

In this example, you would just need to set the values where you want it to change direction (maxRightPosition and maxLeftPosition). It would move by the value of platform.direction, and then when it hits the maximum position in that direction it would reverse the direction property.

Awesome! That worked, still a bit lost on how it actually does what it does but, it works. Thank you very much.

You could add an extra property to the object, stating which direction is should move in:

local platform = display.newObject(blah, blah, blah) platform.direction = 1 local function movePlatform(self, event) self.x = self.x + self.direction if self.x \>= maxRightPosition or self.x \<= maxLeftPosition then self.direction = self.direction \* -1 end end

In this example, you would just need to set the values where you want it to change direction (maxRightPosition and maxLeftPosition). It would move by the value of platform.direction, and then when it hits the maximum position in that direction it would reverse the direction property.

Awesome! That worked, still a bit lost on how it actually does what it does but, it works. Thank you very much.