4. | Within the Main script, create the assignTextures() method, as shown here: on assignTextures wrld = member("memory") tList = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8] randList = [] repeat while tList <> [] ind = random(tList.count) randList.add(tList[ind]) tList.deleteAt(ind) end repeat repeat with cnt = 1 to 16 modName = "c" & string(cnt) tex = "t" & string(randList[cnt]) wrld.model(modName).shaderList[2].texture = wrld.texture(tex) end repeat _global.solveList = randList end First, tList is created and contains the numbers 1 through 8, each duplicated twice, because the same texture needs to be applied to two different cards. To randomize the list, an empty list is created and stored in randList. Next, as long as tList it not empty, the repeat loop will continue to execute. Within the loop a random index is created by using the random function on the count of tList. Next, the value within tList at position ind is added to the randListand then deleted from tList. By continuing this until tList is empty, randList will contain all of the items initially in tList, but in a random order. Let's walk through an iteration of this to see how randList becomes a random version of tList. Initially, tList and randList appear like so: tList = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8] randList = [] Next, a random index into tList is created: ind = random(tList.count) Because tList initially has 16 items, this is equivalent to: ind = random(16) Let's assume ind comes back as 5. The value of tList at position 5 is then added to randList: randList.add(tList[ind]) Because the number 3 is the fifth item in tList, this can be restated as: randList.add(3) Finally, the value is deleted from tList: tList.deleteAt(ind) This removes the first 3 from tList, leaving tList with 15 items in it. Because the list is not empty, the repeat loop continues. ind = random(tList.count) This is equivalent to: ind = random(15) Let's say ind comes back as 10 this time: randList.add(tList[10]) Because the number 6 is the tenth element in the list, randList now looks like so: randList = [3, 6] The tenth item is then deleted from tList, leaving 14 items remaining. This continues until tList has been emptied and randList contains the original 16 items, but in random order: randList = [1, 8, 2, 7, 4, 5, 2, 3, 6, 5, 6, 4, 1, 3, 7, 8] Tip Developers often use this technique of creating one list with known values and then pulling values out at random locations to create a second, random list. If you were creating a card game, for instance, you might fill the original list with the numbers 1 to 52 in sequential order, representing the deck of cards, before randomizing it. With the list in random order and stored in randList, the next repeat loop within the method executes: repeat with cnt = 1 to 16 modName = "c" & string(cnt) tex = "t" & string(randList[cnt]) wrld.model(modName).shaderList[2].texture = wrld.texture(tex) end repeat This loop counts from 1 to 16 and sets modName to "c1" through "c16" as it does. At the same time, tex is set to "t" and concatenated with the value in the random listrandList. Do you see how this works? Let's assume randList = [2, 4, 7, 4, 8, 6, 1, 3, 2, 5, 8, 7, 3, 1, 5, 6] Then as the loop progresses modName and tex would look like so: modName | tex |
---|
c1 | t2 | c2 | t4 | c3 | t7 | c4 | t4 | c5 | t8 |
And so on, through 16. As you can see, cards 2 and 4 would have a match. Now, the final line within the loop is what does the actual work of placing each texture into the appropriate place in the card: wrld.model(modName).shaderList[2].texture = wrld.texture(tex) I mentioned that a shader could contain up to eight textures, and that the material given each card within 3ds max had three sub-materials in it. That second sub-material is the material placed on the back of the card, and is accessed, in Director, by referencing the second shader, or shaderList[2]. By setting the texture of each card's second shader to the texture from the random list, all eight textures are placed onto the backs of the cards in random fashion. The final line in the assignTextures() method creates a global variable solveList and sets it equal to the randList. This will be used later to detect matches. You're nearly ready to see these two methods in action. However, all the cards are showing their tops, so you won't be able to see what happens unless you flip them over. Let's create a quick test method that will allow you to flip all of the cards at once. |