Randomize Unique Tiles

Get help using Construct 2

Post » Wed Jan 08, 2014 6:20 pm

Alright so i am a bit stumped again and I am not sure why. I have been at this for two days now and it's so strange being so far on a project and done things similar to this before and had no issues. I have feeling I am just overlooking a few steps.

Anyway what I want to do is fill the screen with tiles in random order, without repeating the same tile twice.

What I did was make one sprite with different frames, made my randomize placement events and randomized frame select by a random number generator.
For the purpose of my example I am only using four tiles, and every time the program runs it does three tiles the same and one that it different. Everything else placement wise is fine.

I have tried several combinations of this and can't get it it to work. I taught myself arrays and have looked over a few of the tutorials but haven't found one that really helps in this case.

For my example there is an extra frame that is a red X, I do NOT wan't this frame to show up as it is just a place holder since Construct requires one instance somewhere. I will be attaching pictures to these tiles based on their frame once I get this to work but I need them all random and to only have once instance of each frame.

If anyone could look over my example and advise me on what I am missing I would greatly appreciate it!

https://www.dropbox.com/s/l6ou2249oty264c/Random%20Tile.capx

B
9
S
3
G
2
Posts: 51
Reputation: 1,522

Post » Thu Jan 09, 2014 5:56 pm

Anyone able to advise on this? I realize it's a lack of programming knowledge on my end but I haven't been able to find a work arond that helps me. I have found a few maze threads and tutorials but can't seem to reverse engineer one successfully that works with only once instance of each object or frame being created.

If anyone has an idea of what I can search for or which tutorial to check out that would be great too!
B
9
S
3
G
2
Posts: 51
Reputation: 1,522

Post » Thu Jan 09, 2014 6:11 pm

Hola,

Not really sure about your method... How I would do this is create an array... fill the array with random numbers... and then place tiles based on the array... You can also do things like print the contents of the array to test the effectiveness of your randoms..

regards,
B
4
S
1
Posts: 12
Reputation: 817

Post » Thu Jan 09, 2014 6:35 pm

@Digera
Well basically what I did was create an array to set the tiles as you mentioned. However I just used one object with several frames, so created another array to pick a a line of numbers and each number once while also corresponding to the number of frames. Then used those numbers to change the frames of the objects placed. Works just fine...for only one object. For testing purposes I am just doing four tiles, it fills the layout with the four perfectly, three tiles are the same frame and then the fourth is always different from the others. So it works but just for one out of three and I can't get the other three to be unique, they always all go to the same frame. Refresh and they go to a different frame from last time but all three are the same,
B
9
S
3
G
2
Posts: 51
Reputation: 1,522

Post » Thu Jan 09, 2014 9:39 pm

I went ahead and modified your file with an alternate way to do what you want. Here is the file.

The method consists in populating a "possibilities" array, then building your grid with one random element from the array, removing it from the array everytime, so it doesn't get picked again.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Thu Jan 09, 2014 11:16 pm

@magistross
Hey man thanks a lot! Seems like I had somewhat of the right idea going, I was starting to think I was way off.

Only thing I don't get isn't with what you did but why it worked...you have floor(random(0,PossibilitiesArray.width))
But the array width is 4 and from what I understand the random function always takes the last number -1, so the number pool would be 0-3 yet we have 4 frames to choose from. I follow everything else in here but I don't get why that particular event works. Is it just because it's unnecessary to assign the last object the frame since all others have been eliminated?

At any-rate thanks a lot man! This has been a headache for days.
B
9
S
3
G
2
Posts: 51
Reputation: 1,522

Post » Fri Jan 10, 2014 2:07 pm

Simply put, I want a random index between 0 and the number of remaining possibilities minus 1, since arrays are 0-indexed.

With that index, I can then retrieve the corresponding frame in the array.

When the layout begins, the possibilities array get initialized with values from 1 to 4. If the first random index is 0, the first tile is set to animation frame 1. In the next pass, random indexes can only from 0 to 2 (three remaining possibilities). Index 2 get picked, the next tile is set to animation frame 4. And so on so forth...
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Fri Jan 10, 2014 4:08 pm

@MAGISTROSS
AH! I get it okay I was interpreting it completely differently and I think that's why I couldn't get this to work. Makes a lot of sense now thanks a lot man!

You should do a tutorial or something :p
B
9
S
3
G
2
Posts: 51
Reputation: 1,522

Post » Fri Jan 23, 2015 6:48 pm

Thanks for doing this and @Magistross your example was great.

I have a slight addition to add to this. in my case i want to be able to click one of those boxes and idenify it. since it's the same "object" all of the boxes are the same box with different colors..

my goal would be to say have it where it tells me to pick the color from the same pool of colors.. so if it said click the red one, how would i know i'm clicking the red one or if i clicked the wrong one? it doesn't seem like the frame of animation (what determines the color) would be an identifiable parameter.. my other thought was maybe having an instance variable that would grab the random number, but this loop is happening so fast i'm not sure if it would work like that. Any suggestions?

Thanks,
Caleb
B
59
S
9
G
1
Posts: 946
Reputation: 6,561

Post » Fri Jan 23, 2015 7:00 pm

AnimationFrame is a sprite expression that will give you the which of the frame is currently playing. You could also go ahead with an instance variable if you prefer.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Next

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, CreativeMind, jezjones24, NN81, totoe and 13 guests