Randomizing enemy shoot times

0 favourites
  • 3 posts
From the Asset Store
Give Sound to the enemies that are part of your game! :)
  • I'm currently spawning multiple of the same enemy on screen at the same time and using "Every X seconds" to have them shoot at the player.

    However, this makes it so the enemies all shoot at the same time.

    I've been playing with other ideas to get them to shoot at different times, but I've not found anything that works yet.

    Oh, also, I'm trying to add 1 to a variable on every tick, but the number doesn't increase. You can see a text field in the play area with the tick number when playing.

    Here's my capx

    Thanks for the help!

  • I couldn't get your example to do anything--just shows the coin. So I created an example to show you where I think the problems you describe lie.

    RandomExperiments.capx

    At the start this example creates a random number of enemies of random colors in random locations. Then every so often (ie randomly) it:

    a) creates another enemy of random color in a random location

    b) randomly selects an enemy and destroys it

    c) have all enemies fire a bullet in random directions.

    d) randomly selects an enemy and has it shoot a bullet in a random direction.

    (EDIT: I added the case (c) where all of the enemies fire at once.)

    Based on your description of the problem with all of the enemies firing at the same time, I suspect that you are doing (c): at random intervals you are (without meaning to) selecting ALL of the enemies and having them fire bullets.

    The other problem you have is that one of your variables is not incrementing. I think you are experiencing first hand variable scope -- that is, the difference between local and global variables. This is a classic bug. Pretty much everyone runs into it at some point. Local and global variables each have their uses, so it is important to understand them.

    In the example I create two variables: LocalVariable and GlobalVariable. LocalVariable is created inside an event or event group each time it runs and is destroyed at the end of the event/event group. GlobalVariable is created outside the group or event and hangs around.

    Both variables are added to every tick and the result is displayed. But notice that since LocalVariable is created and used briefly every time Every Tick is run it only ever gets set to 1. In contrast GlobalVariable hangs around and keeps its value so when it is added to, the number it contains gets larger and larger.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's actually really simple:

    System | For each enemy

    enemy | is on-screen

    System | Every [random (2, 5)] seconds -> enemy | spawn bullet on layer 1

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