Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to have a car driving off screen — Gideros Forum

How to have a car driving off screen

flyhereflyhere Member
edited February 2014 in Game & application design
Sorry if this is basic, but I spent a couple of hours looking and still couldn't find the answer.

I want to have a car moving along and going off the screen, and of course it came from off screen into play. Just like traffic passing by. I used MovieClip to get the tweening but that's for animation within the frame, how to get the continuous traffic coming into the screen and going off the screen?

I don't want to use some kind of endless moving background, as my other characters are static within my frame.

Thanks

Comments

  • I played around a bit more, and I think I found the answer.
  • You can try using Gtween

    http://appcodingeasy.com/Gideros-Mobile/Gideros-GTween-with-easing

    In short if you set your objects position off screen first and then set up a tween to a position on the other side of the screen this should work fine.
  • In my experience, I used onEnterframe event to control object position. Create a car (bitmap or movieClip) at left center of the screen then move from left to right like below:
    local function runGame()
           car:setX(car:getX() + CAR_SPEED)
    end
    stage:addEventListener(Event.ENTER_FRAME, runGame)
    If you want more details answer, just ask. I will post tonight.
  • flyhereflyhere Member
    edited February 2014
    Thanks, that's exactly what I did. I use movieclip to position the car off frame, then play the movie clip to the other side (also off screen), and that worked fine.

    My current problem is I want to loop thru and randomly generate a new car, trying to detect when a car has gone off the frame before spawning a new car. I either only spawn 1 car or too many cars at the same time.

    It's a simple game with random car traffic running pass and the hero has to avoid collision, but I want no more than 2 cars (or max # of cars) in the frame.

    @kredor7: Thanks, I'll take a look at Gtween, may use it for something else.
    @thanhquan1512: I like your simple logic, will try it, detecting car going off frame when X coord is beyond the display frame. Then randomly spawn another car.

    Thanks
    Phi
  • local _W = SCREEN_WIDTH
    local _xPos = car:getX()
    local CAR_SPEED = 4 -- adjust for your speed and liking
     
    if _xPos > _W then 
      --Car is off the screen
      -- either reposition this one of spawn a new one
      car:setX(-car:getWidth()) -- this will wrap the car off the right to the left
    else 
      car:setX(xPos + CAR_SPEED)
    end
    if you want more, pull a random number and after those many frames have passed, spawn another car, when you spawn a new car, increase the number of cars by 1 and when the car goes off screen, decrease the number by 1. That way you can track the number of cars on screen at any given moment. Place the cars into an array rather than have variables like car1, car2, car3 and so on

    Here are some articles I had written long long ago on starting out with game development

    http://howto.oz-apps.com/2011/08/designing-game-movement.html
    http://howto.oz-apps.com/2011/09/referencing-multiple-objects-of-same.html
    http://howto.oz-apps.com/2011/09/creating-dynamic-variables.html

    Some more that could help (written for another Lua Framework, but principles are the same)
    http://howto.oz-apps.com/2011/09/scopes-for-functions.html
    http://howto.oz-apps.com/2011/09/function-scope-answers.html
    http://howto.oz-apps.com/2011/09/how-to-change-event-handlers-without.html

    Plus there are many more articles if you scroll down on the page.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • edited February 2014
    Try this:
    local grpCar = Sprite.new()
    local spawnCar() --check to spawn car every 1 second
         if frameCount % 60 == 0 and grpCar:getNumOfChilren() < 2 then
              local car = createCar(carX, carY)  --write a func to create a car at x=0, y = math.random(0, application:getContentHeight)
              grpCar:addChild(car)
         end
    end
     
    local function moveCar()
         car:setX(car:getX() + CAR_SPEED)
         if car:getX() > application:getContentWidth then
              car:removeFromParent()
              car = nil
         end
    end
     
    local function runGame()
         frameCount = frameCount + 1
         moveCar()
         spawnCar()
    end
     
    stage:addEventListener(Event.ENTER_FRAME, runGame)
  • Many thanks. Will try.
  • OZApps, thanhquan1512: I ended up not using your codes, but I got enough ideas from you to make it work with MovieClip.

    Many thanks for your help.
  • @flyhere, cool as long as you got it working ;)
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
Sign In or Register to comment.