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.
-
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.
-
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