Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 9th, 2011, 05:09 PM
TheInXorable TheInXorable is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 4 TheInXorable User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 9 sec
Reputation Power: 0
ActionScript 3 - 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...

Reply With Quote
  #2  
Old September 10th, 2011, 05:35 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 9 m 55 sec
Reputation Power: 3163
Facebook MySpace
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?

Reply With Quote
  #3  
Old September 10th, 2011, 10:34 AM
TheInXorable TheInXorable is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 4 TheInXorable User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 9 sec
Reputation 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:

Quote:
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.

Reply With Quote
  #4  
Old September 10th, 2011, 10:39 AM
TheInXorable TheInXorable is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 4 TheInXorable User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 9 sec
Reputation Power: 0
Wait, I got it. Nevermind, thanks again!

Reply With Quote
  #5  
Old September 10th, 2011, 12:17 PM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 9 m 55 sec
Reputation Power: 3163
Facebook MySpace
No problem, glad to be of service ^_^

Reply With Quote
  #6  
Old September 10th, 2011, 01:23 PM
TheInXorable TheInXorable is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 4 TheInXorable User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 9 sec
Reputation 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?

Reply With Quote
  #7  
Old September 11th, 2011, 04:25 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 9 m 55 sec
Reputation Power: 3163
Facebook MySpace
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - DuplicateMovieClip to randomly generate walls

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap