### SOLVED - Enemy Movement Behaviour Help Needed

#### SpookyBurger

• Posts: 82
Hi!  I'm having a small difficulty making a enemy movement behaviour for a top down action rpg game I'm making.  I based it off the space shooter behaviour found in the starter kits.  I added a random number generator to add variation to the enemies movement so I don't have to script a path (that'll be for the next behaviour).  The problem is after the first movement change the enemy starts going "crazy" and changes it's movement direction multiple times per second, so I think the problem is somewhere in how I handle the random number.

Here's the behaviour.  Thanks in advance for any help!  I'm sure it's just a stupid tiny problem on my end.

« Last Edit: February 06, 2013, 11:18:46 am by SpookyBurger »
Please try out my latest build of Patient 9 here.   http://www.stencyl.com/game/play/23179

#### dtrungle

• Posts: 1938
You are telling the game to create an event timer 100 times a second with a duration of randomNumber for each of them. You need to rework the logic by creating the timers only once, when needed.

#### Tuo

• Posts: 2469
There are two major things I see:
1. Understand "always": Always coding runs every frame. Thus, as soon as you start the coding, it will keep on going. While you are telling it to do something every n seconds... that's "ALWAYS do this every n seconds", so you are making literally infinite numbers of loops.
2. Understand the "Do every n seconds": This loop only takes in the value ONCE. Say the first time that it runs through, it gets a value of 4 seconds... then it will keep doing the loop every 4 seconds, never changing the amount of time.

Solution: Make a custom event, such as "SetMovement" (Add Event -> Advanced -> Custom Event):

At the end of "When Created":
Do AFTER (RNG from 4 to 10) seconds
Tell this to SetMovement

(New event: "SetMovement")
[Place all of the coding that was inside your "Random Number Generator" "always" code, EXCEPT the "do every n seconds" part]
At the end of that coding, add the same statement as above:
Do AFTER (RNG from 4 to 10) seconds
Tell this to SetMovement

The idea is to make a self-referencing method that will occur AFTER random times (instead of while a single random time). That should solve all of your issues at once (be sure to then delete or turn off your "RNG always" code).
Don't look to me but rather to the One who is the reason for what I do.

If you need help, send me a PM. Even if I haven't been on in the forums in ages, I still receive those messages via email notifications. You can also reply to any of my forum posts, regardless of the age (especially if I created it), and I will likely reply.

If you want to see the programming behind certain types of games, feel free to check out my "Demo-" games on StencylForge (http://community.stencyl.com/index.php/topic,16160.0.html)

#### SpookyBurger

• Posts: 82
@Tuo
Thanks a lot man!  It's working sweetly.  I seem have to screwed up my collision settings in the process, but that's no big deal.  Attached are the changes.

@dtrungle
Thanks for the input, if Tuo didn't post I woulda pestered you on suggestions.

« Last Edit: February 06, 2013, 09:42:20 am by SpookyBurger »
Please try out my latest build of Patient 9 here.   http://www.stencyl.com/game/play/23179