Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411489 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 05:25:35 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsRandom horizontal movement w/ periodic stopping?
Pages: [1]
Print
Author Topic: Random horizontal movement w/ periodic stopping?  (Read 2038 times)
arkel
Level 0
*


View Profile
« on: December 27, 2011, 09:21:06 PM »

So, I have a few code templates that basically make an NPC move randomly across the screen at a constant speed and were easy to do, but is there a good way to make an NPC walk a few inches horizontally in one random direction, stop for a moment, and then walk again in another?
Think RPGMaker.

Any suggestions would be great, thanks!
Logged
Zack Bell
Level 10
*****



View Profile WWW
« Reply #1 on: December 27, 2011, 09:42:45 PM »

I would just use an alarm and a simple state machine.

Code:
//Create Event

walkSpeed = 2.0;

RIGHT = 1;
LEFT = -1;

facing = choose(LEFT, RIGHT);

IDLE = 10;
WALK = 11;

state = choose(IDLE, WALK);

canChangeState = true;


Code:
//Step Event

if (canChangeState) {
    if (IDLE) {
        alarm[0] = 10 + random(10);
        facing = choose(LEFT, RIGHT);
        state = WALK;
    } else {
        alarm[0] = 10 + random(10);
        state = IDLE;
    }
    canChangeState = false;
}

switch (state) {
    case IDLE: hspeed = 0; break;
    case WALK: hspeed = walkSpeed * facing; break;
}

Code:
//Alarm 0 Event

canChangeState = true;

This is one way to do it. I haven't tested it, but hopefully yo ucan see the logic behind it.              


EDIT: Had a few typos. It's better now.
« Last Edit: December 27, 2011, 09:50:35 PM by Zack Bell » Logged

Ashkin
Guest
« Reply #2 on: December 27, 2011, 09:47:46 PM »

Pseudocode for the update loop:
Code:
timer:int = random number
moveright:Boolean = false
if (timer > 0) timer -= 1;
else moveright = !moveright; timer = random number;
if (moveright == false) x -= 1;
else if (moveright == true) x += 1;

Basically, it creates a timer number that equals a random number, counts it down, and then when it's expired the timer resets to a new random number and the character starts moving in the opposite direction.

Edit: Ninjad. Posting anyway.
Logged
arkel
Level 0
*


View Profile
« Reply #3 on: December 28, 2011, 02:29:44 AM »

@Zack: I copypasted this directly into all the right events, changed the speeds and added some sprite changes, loaded up the game, and the NPC is just moving back and forth without doin' any idling. Did I miss something? Thanks for the quick help, btw!

@Ashkin: Thanks, I'll give this a try.
Logged
Ashkin
Guest
« Reply #4 on: December 28, 2011, 02:39:02 AM »

Oh, if you want to add idling, then do something more like this:
Code:
timer:int = random number
move:int = 0
if (timer > 0) timer -= 1;
else
{
   if (move == -1) move = 0;
   if (move == 1) move = 0;
   if (move == 0) move += (round(random)*2)-1
   timer = random number;
}
if (move == 1) x += 1;
else if (move == -1) x -= 1;
Haha I have no idea if this will work, just sorta slapped together. Good luck!
Logged
Geeze
Level 5
*****


Totally.


View Profile
« Reply #5 on: December 28, 2011, 02:53:38 AM »

@Zack: I copypasted this directly into all the right events, changed the speeds and added some sprite changes, loaded up the game, and the NPC is just moving back and forth without doin' any idling. Did I miss something? Thanks for the quick help, btw!

@Ashkin: Thanks, I'll give this a try.
Changing
Code:
if(IDLE)
to
Code:
if(state == IDLE)
should fix it.
Logged

arkel
Level 0
*


View Profile
« Reply #6 on: December 28, 2011, 03:21:47 AM »

Thanks Geeze, that fixed it just fine!
Surprisingly easy thing to miss.
Logged
Zack Bell
Level 10
*****



View Profile WWW
« Reply #7 on: December 28, 2011, 08:59:17 AM »

Sorry about that! It was really late and I never tested it  Embarrassed

Glad it's working for you now, though  Coffee
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic