creating a sprite randomly within specific area

Get help using Construct 2

Post » Tue Apr 22, 2014 9:17 am

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 :)
B
4
Posts: 24
Reputation: 250

Post » Tue Apr 22, 2014 11:36 am

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.
B
74
S
14
G
4
Posts: 1,018
Reputation: 8,139

Post » Tue Apr 22, 2014 2:28 pm

Thanks for reply, but could you please make it a little bit simple? a sample CAPX file would be great.
B
4
Posts: 24
Reputation: 250

Post » Tue Apr 22, 2014 3:23 pm

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.
composer - multimedia artist
www.eli0s.com/en/
B
69
S
27
G
6
Posts: 1,146
Reputation: 10,379

Post » Wed Apr 23, 2014 7:35 am

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.
B
4
Posts: 24
Reputation: 250

Post » Wed Apr 23, 2014 1:21 pm

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
composer - multimedia artist
www.eli0s.com/en/
B
69
S
27
G
6
Posts: 1,146
Reputation: 10,379

Post » Sun Apr 27, 2014 10:07 am

Thanks a lot! I really appreciate your help :)
B
4
Posts: 24
Reputation: 250

Post » Sun Apr 27, 2014 10:57 am

You're welcome!
composer - multimedia artist
www.eli0s.com/en/
B
69
S
27
G
6
Posts: 1,146
Reputation: 10,379


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests