#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    DuplicateMovieClip to randomly generate walls


    I might not even be approaching this the right way, but I'm trying to make a game where a ship flies through randomly generated tunnels. I'm very new at this, but I felt my question was a bit too advanced for the "Programming Beginners" section.

    So, what I came up with was two black vertical rectangles, ~50 px wide, ~300 px high and ~100 px apart vertically, as one movie clip. I want this clip to be duplicated infinitely, stacked to the right of eachother, and shifted by a random number between 0-10 pixels up or down. This is the code I used to create just one copy of the walls, hoping it would shift over to x=140:

    Code:
    for (var i=0; i<200; i++) {
     	duplicateMovieClip("walls", "walls_copy", 1 );
     	walls_copy._x=140;
     }
    I know I did something wrong. The errors I got were 1180: undefined method duplicateMovieClip and 1120: unidentified property walls_copy.

    Help me out? Remember, I literally picked up Flash yesterday...
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,675
    Rep Power
    3164
    Hi, duplicateMovieClip is an AS2 function which no longer exists in AS3. In AS3 you create a class and associate that with a library object. Then you create instances of that class. So if you create a new file in the same directory as your fla and call it Wall.as then inside that paste:
    Code:
    package
       {
          public class Wall extends MovieClip
             {
                public function Wall()
                   {
                   }
             }
       }
    Save that and then back inside Flash go to the library. Right click your wall MovieClip and select Properties. Then on the popup click the Advanced toggle to open the rest of the panel. Tick "Export for ActionScript" and then in the Class textfield enter the name of the class we created earlier Wall. Note that class names are case sensitive so make sure the names match exactly.

    Now if you click OK then it should close the properties box with no complaints. Sit back and go "phew" as we're mostly there ^_^

    Now back to you original code. We need to change it to use the class we just made:
    Code:
    // We create the initial var outside the loop so we can keep referring to the same reference name.
    // This is just how I do it as I think it makes life easier and tidier.
    var wall_chunk:Wall;
    var wall_height:Number;
    
    for(var i:int = 0; i < 200; i++)
       {
          // Create a new instance of the wall chunk.
          wall_chunk = new Wall();
    
          wall_chunk.x = 140;
          wall_chunk.y = wall_height;
    
          wall_height += wall_chunk.height;
    
          // Add it to the display list so it is actually visible.
          this.addChild(wall_chunk);
       }
    You should head over to Kirupas site, it has a host of great tutorials, scroll that page down a bit to see the "ActionScript Basics" section. I'd recommend you stick with AS3 and not bother with AS2, as time goes by you will find more and more things you want to do are not very easy if at all possible in AS2 where as AS3 is constantly having new things added to it. Definitely the way forward.
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0
    Thank you so much! Most of that makes sense to me, except one thing, I'm tripping over your wording when you said:

    Originally Posted by Tann San
    So if you create a new file in the same directory as your fla and call it Wall.as then inside that paste:
    Code:
    package
       {
          public class Wall extends MovieClip
             {
                public function Wall()
                   {
                   }
             }
       }
    Save that and then back inside Flash go to the library.
    I think I understand what the code does, but could you clarify a bit on where it goes exactly? I'm unsure of how to create a new .as file. Sorry for being a newb.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0
    Wait, I got it. Nevermind, thanks again!
  8. #5
  9. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,675
    Rep Power
    3164
    No problem, glad to be of service ^_^
    Quis custodiet ipsos custodes?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0
    Okay, I've moved through with a lot of stuff but I'm stuck on something. Would you help me out with another thing?

    I have this:

    Code:
    var walls_copy:Walls;
    var walls_copy_x = Walls.x;
    var walls_copy_y = Walls.y;
    
    var i = 0
    do {
    	  i++
    	  walls_copy = new Walls();
    	  
    	  walls_copy.x +=120;
    	  walls_copy.y -=10*Math.random() + 100;
    
          addChild(walls_copy);		
    		
       } while (i<200)
    I played with a lot of things, and it does what I want it to do... once. I want it to create lots and lots of Walls, each referencing the last one's position. AFAIK it only references the first one. How can I do this?
  12. #7
  13. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,675
    Rep Power
    3164
    That's why I created the wall_height variable in my example.
    Code:
    var walls_copy:Walls;
    
    for(var i:int = 0; i < 200; i++)
       {
          walls_copy = new Walls();
    	  
          walls_copy.x = i * walls_copy.width;
          walls_copy.y -= 10 * Math.random() + 100;
    
          this.addChild(walls_copy);				
       }
    I switched it back to a for loop since you were replicating a for loop by manually incrementing i and it's easier to read.
    Quis custodiet ipsos custodes?

IMN logo majestic logo threadwatch logo seochat tools logo