Loop to create sprite, but need random frame

Get help using Construct 2

Post » Tue Oct 07, 2014 10:37 pm

I can't seem to figure this out. I'm trying to place a grass sprite that contains three "levels" of grass. It's all for effect, non-interactive graphics. I do it this way:

*************

System, On start of layout: Function, Call "PlaceGrass"()

Function, On "PlaceGrass"
--System, Repeat 600 times: System, Create object Sprite on layer 0 at (random(0,WindowWidth),random(0,WindowHeight))

Sprite, On created: Sprite, Set animation frame to random(0,2)

**************

Yet when I run the code it sets the grass sprite like I want, but the frames are all the same. Could anyone help me out with this? Thank you in advance.
B
25
S
6
G
6
Posts: 265
Reputation: 3,696

Post » Tue Oct 07, 2014 10:46 pm

Make sure your Animation speed is set to 0 on the Default animation. Also use int(random(0,3)).
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Tue Oct 07, 2014 11:16 pm

You also don't need "Sprite, On created: Sprite, Set animation frame to random(0,2)", you can put just "Sprite, Set animation frame to random(0,2)" under the "create object" line.
B
124
S
43
G
41
Posts: 849
Reputation: 28,235

Post » Wed Oct 08, 2014 3:25 am

Yeah I've tried both things and neither worked. I have tried quite a bit of options and nothing seems to isolate each one on their own. Look under "FloraGen" event sheet.

https://dl.dropboxusercontent.com/u/11127546/wip.capx
B
25
S
6
G
6
Posts: 265
Reputation: 3,696

Post » Wed Oct 08, 2014 3:59 am

;) It is working! (Change the 2 to a 3 like I said though). The problem is your frames 1 & 2 are so close in colour to the background you can't see them. Change your background and you'll see the other grass is there.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Wed Oct 08, 2014 4:08 am

No 0,2 is correct there are 3 frames and its zero count based therefore 0,1,2 are the frames you want to pick from
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Wed Oct 08, 2014 4:33 am

From the manual:
https://www.scirra.com/manual/126/system-expressions
random(x)
Generate a random float from 0 to x, not including x. E.g. random(4) can generate 0, 2.5, 3.29293, but not 4. Use floor(random(4)) to generate just the whole numbers 0, 1, 2, 3.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Wed Oct 08, 2014 4:49 am

ok that might be right idk I normally use Round(random(x,y) or floor, ect i havent used just random in awhile i thought it would
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Wed Oct 08, 2014 5:13 am

Awesome, thanks guys! After looking at it the grass sprites seem kind of bleh. I can't believe I didn't think about the colors being too close!!!

Edit: I changed the number of times it runs (down to 150) and the graphics look fine. I had it doing it too often that it was all globbed together in a mess. But again, thanks guys!
B
25
S
6
G
6
Posts: 265
Reputation: 3,696


Return to How do I....?

Who is online

Users browsing this forum: Darknessed, FabianB and 43 guests