Transitioning left to right issue

Hi all,

This is my firs time on the forums. I have been learning to code for around a month now, researching and taking part in all the tutorials, guides etc. I have just recently started to have a go on my first “game”. I use the word loosely as this is more of a training exercise. I have found it so enjoyable as I’ve been able to work out so much for myself using the forums and guides etc but there is just one problem I can’t work out. I think the reason I’m getting it wrong is I’m getting the order of my code wrong and I’m mixing up the value of global and local variables but I can’t be sure. 

Basically its a top down endless runner (but not the usual kind) in portrait view. I have got my character stationary near the bottom of the screen and he can move left and right. Now, I have managed to work out the code to randomly generate a new monster somewhere, off the top of the screen (using math.random), as they pass off the bottom of the screen. They need to be move a certain distance side to side (which needs to be changeable) as they go from the top to the bottom (to simulate that the character is walking up).

--Move the monster down the screen local function monster1Pos(event) monster1.y = monster1.y + obsSpeed if monster1.y \> 600 then monster1:translate (math.random(20, 460),(math.random(680,1040)\*-1)) end end -- Move the monster from left to right as its moving down local function monsterMoveRight() local monsPosLeft = monster1.x local monsPosRight = monster1.x -- Dont think I really need this monsPosRight = monsPosRight - 10 monsPosLeft = monsPosLeft + 10 local monsterMoveLeft = function() transition.to(monster1, {time = 800, x =monsPosLeft, onComplete = monsterMoveRight}) end transition.to(monster1, {time=800, x = monsPosRight, onComplete = monsterMoveLeft}) end monsterMoveRight() Runtime:addEventListener ( "enterFrame", monster1Pos )  

The issue I am having seems to be two fold. 

  1. When the monster moves down the screen he does move left and right but always seems to be move more right and eventually drifts to the right and off the screen. The functions inside of functions I am using seem wrong to be me but I cannot make it work any other way at this stage.

  2. When I include the functions to move the monster left and right he will always spawn (when translating back up off the top of the screen) right of content.width (never towards the left of the screen. This one is odd

I want to try and stay away from using a gravity engine as I don’t think its necessary?

I eventually want to replace the monster1 with a monster[i] so I can randomise putting different sprites in its place but I should be able to work this out with a table.

Anyway thanks a lot for any feedback, would be a great help.

Mark

I didn’t totally understand your code, so I just sort of baked my own version.

The one thing I would say stylistically is that if you’re going to use Runtime to move down, you may as well use it to move left/right, too. Transitions + Runtime seems a bit unpredictable to me…

[lua]-- Presumably you have a table of monsters.

– This makes it easy to iterate and move all of them.

local monsters = {}

– Here’s your first monster, or at least a stand-in

monsters[1] = display.newRect(0,0,32,32)

monsters[1].moveSpeed = math.random(4)

monsters[1].y = -32

monsters[1].x = math.random(display.contentWidth)

– Lets set whether it should move left or right

if monsters[1].x > display.contentCenterX then

    monsters[1].moveTo = “left”

else

    monsters[1].moveTo = “right”

end

– Moves your monster down the screen.

local function moveDown(monster)

    monster.y = monster.y + monster.moveSpeed

end

– Moves your monster to the right or left

local function moveLateral(monster)

    

    – Let’s try to move left

    if monster.moveTo == “left” then

        

        – Already at the left edge!

        if monster.x < 0 then

            monster.moveTo = “right”

        else

            monster.x = monster.x - monster.moveSpeed

        end

    

    – Let’s try to move right

    else

    

        – Already at the right edge!

        if monster.x > display.contentWidth then

            monster.moveTo = “left”

        else

            monster.x = monster.x + monster.moveSpeed

        end

    end

end

    

– Carries out the move per frame

local function moveMonster(event)

    

    – Repeat for every monster, going backwards

    for i = #monsters, 1, -1 do

        – Call seperate functions so you do a single thing in each

        moveDown(monsters[i])

        moveLateral(monsters[i])

        

    end

end

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

Thanks very much for your reply richard9. I will give it a go

I didn’t totally understand your code, so I just sort of baked my own version.

The one thing I would say stylistically is that if you’re going to use Runtime to move down, you may as well use it to move left/right, too. Transitions + Runtime seems a bit unpredictable to me…

[lua]-- Presumably you have a table of monsters.

– This makes it easy to iterate and move all of them.

local monsters = {}

– Here’s your first monster, or at least a stand-in

monsters[1] = display.newRect(0,0,32,32)

monsters[1].moveSpeed = math.random(4)

monsters[1].y = -32

monsters[1].x = math.random(display.contentWidth)

– Lets set whether it should move left or right

if monsters[1].x > display.contentCenterX then

    monsters[1].moveTo = “left”

else

    monsters[1].moveTo = “right”

end

– Moves your monster down the screen.

local function moveDown(monster)

    monster.y = monster.y + monster.moveSpeed

end

– Moves your monster to the right or left

local function moveLateral(monster)

    

    – Let’s try to move left

    if monster.moveTo == “left” then

        

        – Already at the left edge!

        if monster.x < 0 then

            monster.moveTo = “right”

        else

            monster.x = monster.x - monster.moveSpeed

        end

    

    – Let’s try to move right

    else

    

        – Already at the right edge!

        if monster.x > display.contentWidth then

            monster.moveTo = “left”

        else

            monster.x = monster.x + monster.moveSpeed

        end

    end

end

    

– Carries out the move per frame

local function moveMonster(event)

    

    – Repeat for every monster, going backwards

    for i = #monsters, 1, -1 do

        – Call seperate functions so you do a single thing in each

        moveDown(monsters[i])

        moveLateral(monsters[i])

        

    end

end

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

Thanks very much for your reply richard9. I will give it a go