creating a sprite randomly within specific area

0 favourites
  • 8 posts
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • Hi, I wanted to know how to create a sprite randomly within specific area inside the layout. say I have a 640X480 layout with a 640X480 window size. but I want a sprite to be created randomly just at the bottom or at the middle of the layout. I tried to create an invisible rectangle to use as a reference for the area that covers the creation of the random sprite, by writing down it's X & Y coordinates and then creating the sprite within those coordinates or by using it's width & height, but I still can't get it working correctly. Any ideas please?

    Thanks in advance

  • I believe the "spawn" with "set (random) frame" are the best solution. Just put the empty sprite in the right place, spawn the sprite and use random to set a aleatory frame (set (random(5)) animation frame). You can use a "set position to another object" command too.

  • Thanks for reply, but could you please make it a little bit simple? a sample CAPX file would be great.

  • A bit more complex that you might want as a starting point, but you can see the logic behind the random expression.

    http://www.eli0s.com/Tests/Random.capx

    Basically, the random expression needs 2 values, a low and a high and it will generate a random value in-between (if u use only value it will assume that you want a number from 0 to that value).

    e.g.1 random(20, 30) will produce a number between 20 and 30 (it will be a float value, not an integer)

    e.g.2 random(30) will produce a number between 0 and 30.

    That way you can create random X and Y coordinates.

    e.g.3 every 1 second---> system | create object on layer (whatever) at random(x1, x2), random(y1, y2) whereas x1,x2,y1,y2 can be any number you desire and construct will create the in-between values.

    You can also use the layoutHeight and layoutWidth or windowHeight, windowWidth expressions, this way if the layout or the window size change, the coordinates will adapt to the new size relative to the initial dimensions.

    e.g.4 random(0, layoutWidth) will produce a random value from 0 to the maximum dimension of your layout, in your case 640 px.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks elios for making things clear. I will then keep with the "invisible rectangle" method and use it's X & Y coordinates as a reference.

  • Well, you can use the invisible rectangles' dimensions and position to directly control the spawning area. This way even if you move around the rectangle, or change its size, the changes will reflect in the spawning area.

    http://www.eli0s.com/Tests/invisible_rectangle.capx

  • Thanks a lot! I really appreciate your help

  • You're welcome!

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