Enemies horizontal and vertical random movement

0 favourites
  • 15 posts
From the Asset Store
a full game example, menu system, with the first level and a boss, to create a horizontal shooter
  • Hi,

    I have been having problems trying to figure out how to make my enemies move randomly in all four directions (up, down, right, left).

    All of my enemies are the same sprite, but I want them all to move on different directions randomly.

    Thanks a lot guys!

  • Hey jminfantem!

    What movement behavior are you using on your enemies?

  • broknecho I am using the 8 direction behavior.

  • One way you could try is to:

    for each enemy:

    1- Set a local variable direction to a random number from 1-4 to pick a direction

    2- Compare that variable to pick the direction for each enemy.

    Depends if you want the enemy to just turn 90 degrees or possibly turn right around for a 180.

    I'll make a sample capx to illustrate.

  • [Condition: Pick random instance] every x second will be straight forward enough for you to achieve what you want and set random direction.

  • broknecho this is exactly what I am looking for. The only problem is that there seems to be a bug that replicates the sprite that is moving.

    Do you know why?

  • I was working on it. But I still don't get why the sprite replicates itself.

    Also I would like that for every red box(enemy), I have one skeleton...

    any ideas on how I might work this out?

  • You should use a single sprite not 4. Create different animation set for a singular sprite, then change the animation as per condition.

  • I agree to use one sprite, but the capx attached should help work out what you're doing wrong.

    Edit:

    The sprite wasn't replicating itself. You can check this by running in debug mode and seeing the instance count.

    To have an instance of a sprite always associated with another sprite, use a Container (like I did in the capx)

    You weren't setting the position of the skeleton sprite to the enemy sprite every tick, so they were not together (I could see that by turning the Enemy sprites Visible).

    You don't need to do certain things every tick, like Start ignoring 8Direction input.

    You needed 1 direction variable (i.e. instance variable) per sprite. However I changed it to just check the Enemy sprite's angle instead.

  • codah looks to have a solution there.

    I would also agree that a single sprite would be better here. Then you could just make 4 animations based on the 4 directions. Once a direction is chosen, you can then use the Set Animation action to change what direction animation is playing.

    Also for smaller lists, an easier expression for random lists of things is the choose() function:

    i.e.: choose(1,2,3,4) - for direction

    OR

    choose ("SWalkUp","SWalkDown","SWalkLeft","SWalkRight") - For random animation (but keep in mind you have to then move in the correct direction as well)

  • codah thanks a lot for your help and your time. I tried implementing what you did and fixed but I seem to be having a conflict of instances. I can't pinpoint the bug. Do you know what it might be? <img src="{SMILIES_PATH}/icon_redface.gif" alt=":oops:" title="Embarrassed">

    https://www.dropbox.com/s/w1dlofmnbj3v5 ... .capx?dl=0

  • codah thanks a lot for your help and your time. I tried implementing what you did and fixed but I seem to be having a conflict of instances. I can't pinpoint the bug. Do you know what it might be? <img src="{SMILIES_PATH}/icon_redface.gif" alt=":oops:" title="Embarrassed">

    https://www.dropbox.com/s/w1dlofmnbj3v5 ... .capx?dl=0

    By the way I was not saying my capx contained best practices.. I would not really suggest all that visible/invisible stuff, especially if you had to repeat it lots of times..

    Edit: ok so you have an object called 'Sprite' that is not placed on any layout (C2 needs this). However Sprite is not being used so just remove it.

  • codah Thanks a lot for your help. I was able to figure it out.

    I know it might not be the best practice especially if the game will have around 10 enemies. But I will use it until I can device a simpler solution.

    Again, thanks a lot for your time.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • codah Thanks a lot for your help. I was able to figure it out.

    I know it might not be the best practice especially if the game will have around 10 enemies. But I will use it until I can device a simpler solution.

    Again, thanks a lot for your time.

    Very true, sometimes good enough is best

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)