3 Replies Latest reply on Jul 12, 2007 5:48 AM by Newsgroup_User

    Populating scene with Action Script

    Nejkodebejko1
      Hello,

      I`m new to Action script and flash. I would like to know how could i populate (instead of copy and paste) the scene with a small button ex. 10 x 10 px. Let me explain what i am trying to do here. It is a simple game that includes 4000 buttons, 50 rows by 80 buttons. Every button has states, not available, available and in proccess. Visitors come on the page and start clicking, the one with the most clicks wins.

      So instead of doing this with the painfull copy and paste, than giving the button an unique id, i was wondering if there is a way to do this with action script, generating 50 rows, 80 buttons per row and giving each one a unique id with j++ or something.

      Thank you very much and sorry for my english.
        • 1. Re: Populating scene with Action Script
          Level 7
          Sure, no problem actually. I'd use two pieces of code. The first would be a
          class, extending MovieClip, and attached to your button clip in the library.
          Then a little for loop on the main timeline to attach the buttons to the
          stage and you're all set.

          For the class:

          class com.yourdomain.Sample extends MovieClip
          {
          var myID:Number;

          function Sample(){}

          function set id(newID:Number):Void
          {
          myID = newID;
          this.onRelease = function(){
          trace(this.myID);
          }
          }

          }

          Of course, you'll need to change the com.yourdomain to your own class path.
          Then just set your clip in the library to export for ActionScript and set
          the class to: com.yourdomain.Sample

          On the main timeline then, write a little createGrid function like so:

          function buildGrid(tot, sx, sy, pc, cs, rs){
          var startX = sx;
          var currX = startX;
          var startY = sy;
          var perCol = pc;
          var colSpace = cs;
          var rowSpace = rs;
          //use tot + 1 because we're going from 1 not 0 in the for loop below
          var total = tot + 1;

          for(var i=1; i < total; i++){
          var clip = this.attachMovie("btn", "btn" + i, i+10, {_x:currX,
          _y:startY});
          clip.id = i;
          currX += colSpace;
          if(i % perCol == 0){
          startY += rowSpace;
          currX = startX;
          }
          }
          }

          And call it:

          buildGrid(100, 20, 20, 10, 12, 12);

          This will make a grid of 100 instances of the clip named 'btn' (the one with
          your class attached), starting at 20,20 with 10 per row and 12 pixels
          between centers... When you click any of them they will trace their
          appropriate index 1 - 100

          HTH


          --
          Dave -
          Head Developer
          http://www.blurredistinction.com
          Adobe Community Expert
          http://www.adobe.com/communities/experts/


          • 2. Re: Populating scene with Action Script
            Nejkodebejko1 Level 1
            Hello Dave,

            Thank you very much. You're a life saver.

            I got it working, but im still not sure how i did it :) .

            Now i have a additional request. How could i connect every populatet button with XML => PHP => MYSQL. Dont worry explaining about php or mysql. I would just like to know how could a button read from XML, and depending on the setting in its xml row to be colured ex. red for not available and blue for still available.

            So the big question is checking the xml for the state of the button, available or unavailable, and then reacting to that state in flash movie.

            Hope you can help,

            thanks again,

            sincerely,

            Nejc Palir
            • 3. Re: Populating scene with Action Script
              Level 7
              >>how could a button read from XML, and depending on the setting in its xml
              >>row to be colured ex. red for not available and blue for still available.

              Assuming you can read and parse the XML, you could read your button states
              into an array. Then, when you build the grid check the array for the state
              of the current button.

              Your state array might just be a bunch of integers:
              stateArray = [0,1,1,0,1,.......];

              In the loop, right after where you set the clip's id, you could check the
              state:
              if(stateArray[i-1]){
              //stateArray is 1 for this clip - set it to red or something
              }

              I put i-1 since the for loop in my example counts from 1 and array indexes
              begin at 0.


              HTH


              --
              Dave -
              Head Developer
              http://www.blurredistinction.com
              Adobe Community Expert
              http://www.adobe.com/communities/experts/