Random Active Spawn in Random Location in Frame

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hey Clickteam Community,

    I'm working on my first Roguelike game, and I need help: I'm making a space shooter, and I need to spawn a variable amount of asteroids within the frame, as well as a variable amount of enemies (different types as well). How do I spawn an Active in a random location within the boundaries of the frame?

    Thanks for the help.

  • -Upon pressing "Spacebar"
    +Start loop "Create objects" 20 times

    If you want to spawn the asteroid only within the boundaries of the visible screen:

    -On loop "Create objects"
    +Create Asteroid
    +Set Asteroid's X position to:
    __X Left Frame + Random (X Right Frame - X Left Frame)
    +Set Asteroid's Y position to:
    __Y Top Frame + Random (Y Bottom Frame - Y Top Frame)

    If you instead want to spawn the asteroid anywhere in the frame, use this:

    -On loop "Create objects"
    +Create Asteroid
    +Set Asteroid's X position to:
    __Random (X Right Frame)
    +Set Asteroid's Y position to:
    __Random (Y Bottom Frame)

    Edited once, last by Snail (December 1, 2013 at 10:54 PM).

  • The first solution didn't work, but the second solution works perfectly! I don't know how you figured that out, but you have really helped me out.

    Here's the .MFA file if anyone gets stuck.

    Thanks Snail!

    Please login to see this attachment.

  • Found the problem with my first expression (already edited). :) Sorry about that! If you want to randomize the position only within the visible screen, you'd do this:

    +Set Asteroid's X position to:
    __X Left Frame + Random (X Right Frame - X Left Frame)
    +Set Asteroid's Y position to:
    __Y Top Frame + Random (Y Bottom Frame - Y Top Frame)

    I'm glad it's working, however. Good luck!

  • Thanks for the update Snail. I used a quick event to destroy any outside the frame.
    I have 1 quick question: What is the random phrase again? I need to spawn a random amount between 10-20, and I can't seem to word the random expression correctly.

  • The expression you need is:

    Random()

    It returns a number between 0 and the calculation within the (). So, Random (10) will return a number between 0-9, whereas Random (480 - 300) would return a number between 0-379. If you want a random number between 1-10, you'd do 1 + Random (10) or you could do Random (10) + 1. So, current X position of the right edge of the screen:

    Random (X Right Frame)

    would return a number between 0-639 if you haven't scrolled anywhere and the frame is 640 pixels wide.

    Calculations are possible within parentheses, so you could do something like:

    Random (((Alterable Value A("Active") * Alterable Value B("Active")) / Alterable Value C("Active")) + X Right Frame)

    which would result in A and B being multiplied, and then the result being divided by C, and then the position of the right side of the visible frame being added to the result, and then the expression would return a random value between 0 and that result.

    Random (Random (Random (10))) is not the same as Random(10) because the value is more likely to be lower. The same goes for Random(Alterable Value A * (Random (Random (Alterable Value B) / Random (Alterable Value C)) - Random (Random(Alterable Value D) * Random(Alterable Value E)))).

    One final note about Random(): It does not work with negative values; therefore, anytime you use a variable that could result in a negative value being put in the Random() pool, you need to use Abs() around it so that the number is positive or 0. Abs() means Absolute Number, as in a number that is always positive (the absolute distance from 0).

    Edited 2 times, last by Snail (December 2, 2013 at 7:19 AM).

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!