Randomize Unique Tiles

0 favourites
  • 11 posts
From the Asset Store
Each tile 64 by 64 pixels - Ultimate Tile Set for your awesome games
  • 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! <img src="smileys/smiley10.gif" border="0" align="middle" />

    dropbox.com/s/l6ou2249oty264c/Random%20Tile.capx

  • 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!

  • 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,

  • 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,

  • 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.

  • 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.

  • 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...

  • 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

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • ok nevermind, you CAN use the frame animation as an ID. The solution was to make an instance variable and just after the frame is called on, set the current frame to the instance variable. thanks again for this great thread!

    Also thanks for confirming the frame number is itself an identifying value. i didn't realize it could be called on any time, but now that you say that it makes perfect sense.

    Thanks!

    Caleb

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