The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> Flash Help
|
ActionScript 3 - DuplicateMovieClip to randomly generate walls
Discuss DuplicateMovieClip to randomly generate walls in the Flash Help forum on Dev Shed. DuplicateMovieClip to randomly generate walls Flash Help forum discussing all products originally created by Macromedia including DreamWeaver, Contribute, Flash, Fireworks, Freehand, Director, Authorware and HomeSite. Adobe bought Macromedia in 2005.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 9th, 2011, 05:09 PM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 4
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...
|

September 10th, 2011, 05:35 AM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
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?
|

September 10th, 2011, 10:34 AM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 4
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.
|

September 10th, 2011, 10:39 AM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 4
Time spent in forums: 21 m 9 sec
Reputation Power: 0
|
|
|
Wait, I got it. Nevermind, thanks again!
|

September 10th, 2011, 12:17 PM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
|
No problem, glad to be of service ^_^
|

September 10th, 2011, 01:23 PM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 4
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?
|

September 11th, 2011, 04:25 AM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|