Random Number within a certain Range that's divisible by a certain number

Hello! I'm attempting to spawn enemies in patterns, moving from left to right and vice versa across the screen. I've been able to generate and destroy the actors when they leave the screen, but I want to limit the vertical position of each of these actors.

Essentially, I've been able to limit the vertical(y) position of the spawned actor to a value between 80 and 240. However, I only want 4 positions - 80, 120, 160, and 240. I believe I have to use modulus to spawn only a number that is divisible by 40, but I'm having trouble figuring out the expression. I would really appreciate your help. Thanks!

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Hmm... too bad you couldn't have 5 positions (80,120,160,200,240). Then it would only be something like:

    random(2,6)*40

    If you really cant use the 200 that gets randomly generated, I suppose you could do a test to see if its position is 200 and then destroy the actor.

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited September 2014

    Hi @jmkulakowski and welcome to the Forums.

    The solution below assumes you want your spawns at a regular amount of seconds after each other.

    I'll assume the spawning starts straightaway, the following Rules in an actor outside the playing area:

    Integer attribute WhichSpawn initial value set to 0

    Timer: After 2 seconds --- or whatever interval you want
    

    Change Attribute WhichSpawn to random(1,4)

    Now in your Prototype that you want spawned:

    Change Attribute self.Position.X to 25 -- or whatever you've decided
    
    Rule: When WhichSpawn < 4
    Change Attribute self.Position.Y to 40+(WhichSpawn*40)
    Otherwise
    Change Attribute self.Position.Y to 240
    

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • colandercolander Member Posts: 1,610

    Here you go try this. Run the project and click the actor repeatedly and the number will change randomly in the range you specified.

Sign In or Register to comment.