5 Replies Latest reply on Jan 27, 2012 12:19 AM by DallasBrown

    Choose random image script

    DallasBrown

      I'm creating a small movie with a set of random images that all appear in the same spot (no horizontal or vertical movement). I want each image to appear onscreen for two seconds, then be replaced by another randomly-chosen image. There are 17 images in the set. Here is the script I'm using:

       

      on enterFrame

        channel=random(17)

        _movie.delay(2*60)

       

      on exitFrame

        go to the Frame

      end

       

      This script works well. However, because the population is small - only 17 - what happens when I run the script is that a little too often it randomly chooses the same image once, twice, sometimes up to five times in a row, which gives the effect that the movie is either stuck or not working properly. I would like to find out how to modify the script so that on exitFrame it chooses randomly any of the other 16 images but not the current image. At first I thought if the script read random(16) or random(17-1) that might work, but it doesn't.

       

      Any thoughts would be welcome.

       

      Dallas

        • 1. Re: Choose random image script
          Sean_Wilson Adobe Community Professional

          I would approach this by creating a list of bitmap member names and then selecting one of these when required and displaying it. Something like the following (behavior) script attached to a single sprite that will serve to display the randomly chosen image:

          property spriteNum
          property my
          property myMemberName
          property myMemberNameList
          property myTimeout
          
          on beginSprite me
            my = sprite(spriteNum)
            myMemberName = my.member.name
            myMemberNameList = []
            -- one option for creating a list of memberNames:
            nLibs = _movie.castLib.count
            repeat with cc = 1 to nLibs
              nMembers = castLib(cc).member.count
              repeat with mm = 1 to nMembers
                tMember = member(mm, cc)
                if tMember.type = #bitmap then myMemberNameList.append(tMember.name)
              end repeat
            end repeat
            -- alternatively, pre-populate it "manually":
            --myMemberNameList = ["image1", "image2", "image3", ...]
            myTimeout = timeout().new("mSwapImage", 2000, #mSwapImage, me)
          end
          
          on endSprite me
            if ilk(myTimeout, #timeout) then myTimeout.forget()
            myTimeout = VOID
          end
          
          on mSwapImage me
            -- select random member name:
            nItems = count(myMemberNameList)
            tIndex = random(nItems)
            tName  = myMemberNameList[tIndex]
            -- or:
            --tName  = myMemberNameList[ random( count(myMemberNameList) ) ]
            
            -- check selected name against existing member name (so the same member isn't re-displayed):
            if tName = myMemberName then
              -- if they're the same then re-select
              me.mSwapImage()
              exit
            end if
            
            -- swap the image displayed:
            my.member    = member(tName)
            myMemberName = tname
          end
          
          1 person found this helpful
          • 2. Re: Choose random image script
            DallasBrown Level 1

            Hey Sean,

             

            Thank you for the very insightful answer. I'm an inexperienced user of Director, so when I tried to apply your script it didn't work and that's almost certainly because I'm doing something wrong. My understanding of your solution is that I put a single cast member sprite onto the score and apply the script to it. When the playhead reaches this sprite, it should stop there, show that cast member and then every two seconds call another cast member in random sequence to appear on the score. If this is not the correct interpretation, could I  ask you to let me know how you understand it?

             

            I have a single sprite sitting at frame 5 of channel 1. I have applied the script to that sprite but the playhead does not stop at that frame (and it doesn't stop either if I put the script into the behavior channel). Here's the way I've changed your script:

             

            property spriteNum

            property my

            property myMemberName

            property myMemberNameList

            property myTimeout

             

            on beginSprite me

              my = sprite(1)

              myMemberName = my.member.name

              myMemberNameList = ["Orange1", "Orange2", "Orange3", "Orange4", "Orange5", "Orange6", "Orange7", "Orange8", "Orange9", "Orange10", "Orange11", "Orange12", "Orange13", "Orange14", "Orange15", "Orange16", "Orange17"]

              myTimeout = timeout().new("mSwapImage", 2000, #mSwapImage, me)

            end

             

            on endSprite me

              if ilk(myTimeout, #timeout) then myTimeout.forget()

              myTimeout = VOID

            end

             

            on mSwapImage me

              -- select random member name:

              nItems = count(myMemberNameList)

              tIndex = random(nItems)

              tName  = myMemberNameList[tIndex]

              -- or:

              --tName  = myMemberNameList[ random( count(myMemberNameList) ) ]

             

              -- check selected name against existing member name (so the same member isn't re-displayed):

              if tName = myMemberName then

                -- if they're the same then re-select

                me.mSwapImage()

                exit

              end if

             

              -- swap the image displayed:

              my.member    = member(tName)

              myMemberName = tname

            end

             

            So my questions are:

             

            1. Are there some variables that I need to add to this script to make it work? For example, does the line

             

            tIndex = random(nItems)

             

            have to read instead

             

            tIndex = random(17Items)

             

            because my member list contains 17 elements?

             

            Or does

             

            myMemberName = my.member.name

             

            require a real member name (such as Orange1) after the = sign, and if so what is the correct syntax?

             

            If I must change some of your generic instructions to actual variables to make the script work, can you tell me which elements in your script need this?

             

            2. I assume the number 2000 in the script refers to ticks, or something similar, amountiing to 2 seconds. If I want to change the turnover rate to, say, 1 second or 3 seconds, would I change this number to 1000 or to 3000?

             

            3. Once the script is accurate for my project, how do I set up the score so the script will work properly? Do I put only one cast member sprite onto the score to which I apply the script, or all cast members on the score, each as a separate sprite and each with the behavior script applied? If the latter, then do I align them horizontally on one channel, each covering a single frame or vertically each on a separate channel, again each covering a single frame? Or do they need to cover multiple frames? Or do I put the behavior script into the, ummm, behavior channel?

             

            I hope you have enough time and patience to answer these painfully newbie questions.

             

            Dallas

            • 3. Re: Choose random image script
              Sean_Wilson Adobe Community Professional

              Start by adding a script to the frameScript channel that pauses the playhead on that frame. The script I provided earlier should then do the rest.

              • 4. Re: Choose random image script
                DallasBrown Level 1

                Outstanding. Works now. Thank you for your help.

                • 5. Re: Choose random image script
                  DallasBrown Level 1

                  Hi Sean,

                   

                  This randomizing project of mine is becoming more complex than I first realized and I now have another question that I hope you might be able to help me with.

                   

                  The project breaks down into class, category, group and member. There are 9 classes (and perhaps a possibility in the future that further classes could be added). The structure and naming convention within each class is identical down to the member level. For example, Cat. 1 of Class 2 and Cat 1 of Class 7 both have the same names. Group 3 of Cat. 1 of Class 2 and Group 3 of Cat 1 of Class 6 both have the same names. However, the members of these groups do not have the same names.

                   

                  In each class there are 4 categories, each with a different name. Each category contains a number of groups (each group has a different name). Each group contains a number of members. Each member in categories 1 and 2 is a bitmap image. The total number of bitmap members in the project is 21.

                   

                  To give a fictionalized example (this project is not about clothing, but I'm using it as an example):

                   

                  Class 1 = shirts

                  Class 2 = hats

                  Class 3 = pants

                  Class 4 = socks

                  Class 5 = shoes

                  etc.

                   

                  Category 1 = British

                  Category 2 = Japanese

                  Category 3 = Turkish

                  Category 4 = Brazilian

                   

                  Group 1 = Cotton

                  Group 2 = Synthetic

                  Group 3 = Speed

                  Group 4 = Sound

                   

                  and so forth.

                   

                  In Class 1 (shirts) there are British, Japanese, Turkish and Brazilian shirts and these could be made of either cotton or synthetic. In addition, there are a variety of speeds and sounds from which to choose when displaying the shirts.

                   

                  In my project:

                   

                  Cat. 1 has 7 groups and each group varies in the number of members with either 7, 14 or 21 (but no other alternative to these numbers).

                  Cat. 2 has 13 groups and each group has 7 members.

                  Cat. 3 does not randomize images but lets me choose any one of 12 different speeds for randomization in half-second increments.

                  Cat. 4 does not randomize images but lets me choose any one of 6 different sounds. Whenever the randomized image changes, this triggers the sound. The sound is not randomized but is consistent once the choice for what KIND of sound has been made. In short: tick, tick,tick, or tock, tock, tock but not tick, tock. It is possible that in the future more sounds could be added.

                   

                  Navigation should allow me to start with any of the four categories. For randomization to work, I must choose within either Cat. 1 or Cat. 2 (but not both) and I must make a choice of some sort within Cat. 3 (speed of randomization) while Cat. 4 is entirely optional (sound on or off, but not required to be on and, if it's to be on then choice for which kind of sound).

                   

                  There are also an additional two buttons for sound on or off than run the length of the score within any class, meaning that while randomization is occuring anywhere within a class, I can choose to turn the sound on if it's currently off or off if it's currently on. Turning it on while randomization is taking place will also probably require a choice of which kind of sound (meaning a floating window I suppose) so as not to exit the frame where randomization is taking place.

                   

                  Now for my question: should I create one massive behaviour script for all classes that includes a series of nested if/else statements for each category, group and member, or should I create 4 large behavior scripts, one for each category with if/else instructions for its groups and members, or should I script at the group level, which will result in 180 separate behaviour scripts for image randomization plus additional scripts for speed and sound?

                   

                  Further, I understand how to apply at the group level the script you gave me earlier (and it works beautifully), but if you think it would be more efficient now to work at the category or class level, I'm not sure how to script the if/else statements. I'm assuming that in whichever of these solutions turns out to be best, it is applied to a single sprite which will serve to display the randomly-chosen image. Bear in mind that the script you gave me randomizes the image change every two seconds. Now I'm looking to have a choice of it changing within a range of once every half-second to once every 5 seconds (e.g. 0.5 sceonds, 1 second, 1.5 seconds, 2 seconds, etc.).

                   

                  An ancillary question: could this be done a different way? A friend suggested using subroutines but wasn't able to tell me how to do this because he doesn't know anything about Lingo. I was wondering whether the if/then syntax structure is Lingo's way of doing subroutines.

                   

                  Your thoughts would be most welcome. If you think you can't help me with this, do you have any suggestions as to where I might turn for help (apart from the nuthouse at this point)?

                   

                  Cheers,

                   

                  Dallas