Solved

AttachMovie in as3

Posted on 2008-10-25
8
1,280 Views
Last Modified: 2013-11-11
Hi,

I am trying to convert an as2 script to as3. Having never dabbled in as2 i am having trouble with the attachMovie and how to convert this to as3. I know it should be addChild but it is the extra parameters that are confusing me.

Can anyone help? The as2 code is below.

Thanks
for (i=0; i<snowflakes; i++) {
		
t = attachMovie("snow", "snow"+i, i);
 
}

Open in new window

0
Comment
Question by:Hulfy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 9

Expert Comment

by:asaivan
ID: 22803794
If the name of the movieclip is "snow" then:

for (i=0; i<snowflakes; i++)
{
 
this.addChild(new snow())              
 
}

The extra parameters in AS2 are indicating that the name of the movieclip is "snow" (1st arg), the instance name should be "snow0", "snow1", etc. (2nd arg), and the level should be one higher than the last one (3rd arg).

In AS3 you don't have to worry about levels anymore.  As far as using instance names goes for this type of operation, I've often found it easier to put "snow" into an array, and then just manipulate the index of the array if need be, not worrying about assigning a unique instance name to each new movieclip instance e.g.:
var snowArray:Array = new Array();
 
for (i=0; i<snowflakes; i++) 
{
   snowArray.push( new snow() );
   this.addChild( snowArray[i] );            
 
}

Open in new window

0
 

Author Comment

by:Hulfy
ID: 22806484
this is the original script, it is the looping and adding of movieclips that I cannot get.


Thanks,

H
init = function () {
	width = 300;
	// pixels 
	height = 200;
	// pixels 
	max_snowsize = 10;
	// pixels 
	snowflakes = 50;
	// quantity 
	for (i=0; i<snowflakes; i++) {
		t = attachMovie("snow", "snow"+i, i);
		t._alpha = 20+Math.random()*60;
		t._x = -(width/2)+Math.random()*(1.5*width);
		t._y = -(height/2)+Math.random()*(1.5*height);
		t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
		t.k = 1+Math.random()*2;
		t.wind = -1.5+Math.random()*(1.4*3);
		t.onEnterFrame = mover;
	}
};
mover = function() {
	this._y += this.k;
	this._x += this.wind;
	if (this._y>height+10) {
		this._y = -20;
	}
	if (this._x>width+20) {
		this._x = -(width/2)+Math.random()*(1.5*width);
		this._y = -20;
	} else if (this._x<-20) {
		this._x = -(width/2)+Math.random()*(1.5*width);
		this._y = -20;
	}
}
init();

Open in new window

0
 
LVL 9

Accepted Solution

by:
asaivan earned 500 total points
ID: 22809058
It's something like this:



for (i=0; i<snowflakes; i++) 
{
   snowArray.push( new snow() );
   this.addChild( snowArray[i] );   
   snowArray[i].alpha = 20+Math.random()*60;
   ...
   snowArray[i].addEventListener(Event.ENTER_FRAME, mover);
 
}
 
private function mover (event:Event):void
{
    this.y += this.k;
   ...
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:asaivan
ID: 22809061
The ENTER_FRAME event may not work right in this case though.  I can't remember the different scopes of 'this' in AS2.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 22812756
hulfy, you're duplicating posts.
see  your other post.
0
 
LVL 9

Expert Comment

by:asaivan
ID: 23162451
I would like to have some recompense for the time spent on this question.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I convert a CS4 FLA file to CS6? 1 882
Flash being Disabled - help! 20 343
Flash builder 4.13 list scroller does not work 1 53
embedding a media live encoder stream 3 158
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question