Random Spawn Point

0 favourites
  • 4 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • Hi guys, how are you doing folks?

    I'm producing a game and I've got a doubt.

    I'd like to know how could I create a random spawn point just in the red area?

    For example, every x seconds the spawn point would be in a different position in the red square.

    The game is a drag and drop for kids and I'm just in the beginning of the project.

    I hope you can help me a little. I already thank you for this.

  • On creating the object you could:

    - Set the X position to be random(RedSquare.X-(RedSquare.Width/2), Red_Square.X+(RedSquare.Width/2))

    - Set the Y position to be random(RedSquare.Y-(RedSquare.Height/2), Red_Square.Y+(RedSquare.Height/2))

    That way it picks a random position between both edges of the red square for both the X and the Y.

  • On creating the object you could:

    - Set the X position to be random(RedSquare.X-(RedSquare.Width/2), Red_Square.X+(RedSquare.Width/2))

    - Set the Y position to be random(RedSquare.Y-(RedSquare.Height/2), Red_Square.Y+(RedSquare.Height/2))

    That way it picks a random position between both edges of the red square for both the X and the Y.

    It worked perfectly my friend, awesome!

    Thank you very much.

    However, may you explain to me, if it is possible, how do the expressions that you've created work, please? Because I just copyed them and I don't know how they work.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Sure thing!

    The random() expression is used to find a random number.

    - Random(X): Used to find a random number between 0 and X.

    - Random(X,Y): Used to find a random number between X and Y.

    Red_Square.Width and Red_Square.Height are expressions that return the width and height of the red square in pixels. Red_Square.X and Red_Square.Y are expressions that return the red square's X and Y position in the layout.

    Since the sprite's origin is placed in the center of the sprite by default, we can find the edges by doing:

    - Left Edge: Red_Square.X-Red_Square.Width/2 (Lets call this A)

    - Right Edge: Red_Square.X+Red_Square.Width/2 (Lets call this B)

    - Top Edge: Red_Square.Y-Red_Square.Height/2 (Lets call this C)

    - Bottom Edge: Red_Square.Y+Red_Square.Height/2 (Lets call this D)

    This is because since the origin is in the center, that means the edge of the sprite would be half of the sprites width/height away, yeah?

    Now that we have the edges of the Red Square, we can just plug it into the random expression.

    Using random(A, B) would get us a random point between the Left and the Right edges.

    Using random(C, D) would get us a random point between the Top and the Bottom edges.

    And that's pretty much how it works! Was I clear enough or should I explain more?

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