Actor picks random directions to move

So I have an actor that I want to randomly path around. I'd like it to pick one of 4 directions (0, 90, 180, 270) to move, move for a random amount of time (say 1-3 seconds), then stop for a random amount of time, and finally picking a new direction continuing the cycle.

I've started by creating new integers for the actor, e.q. MoveUp = 90. How can I randomly pick one of these 4 integers as the direction and have the actor move? Or is there a way to set these numbers without making the actor integers?

If I can get the movement right I'll see about changing the image per direction. :)

Comments

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772
    edited December 2012
    Pick a random number between 1 and 4, then use those numbers to set the direction. Something like:

    change attribute move to random(1,4)

    if move = 1 then change your actors direction to 0

    if move = 2 then change your actors direction to 90

    if move = 3 then change your actors direction to 180

    if move = 4 then change your actors direction to 270

    That should do it.
  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    I've added a quick file below - See if that helps.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    Try something like this:

    Timer Every: 3 seconds
    -- Timer After: random(1,3) seconds (√ Run to Completion)
    ---- Change Velocity
    ------ Direction: random(0,3)*90 Relative to: scene
    ------ Speed: (your choice)
    ---- Timer After: random(1,3) seconds (√ Run to Completion)
    ------ Change Velocity
    -------- Direction: 0 Relative to: scene
    -------- Speed: 0
  • OrbInteractiveOrbInteractive Member Posts: 14
    Thanks for the advise guys.. I ended up going with Jamie_c's suggestion since it seemed more straight forward to add in image changes per direction, but am also worked in some of RThurman's suggestion for more interesting random movement. There seems to be little bugs, but will not get to look at it close till I'm back from Xmas. Thanks again for help.
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    @rthurman method is the slimmest code. Always go with the slimmest code. Just because something works doesn't make it the best coding option. Slim code keeps your game running smooth and at a high frame rate.
Sign In or Register to comment.