PDA

View Full Version : loadMovie AS3 style



GrndMasterFlash
July 27th, 2007, 12:53 PM
i just started using AS3 and was trying to do a loadMovie, but that method is not supported, can some one tell me how to do a loadMovie in AS3?:b:

GrndMasterFlash
July 27th, 2007, 01:59 PM
for anyone interested this is how you do it:}

function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("thevid.swf"));
}
my_mc.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

thanks to all who at least thought about it:beer:

PinoyGee
December 30th, 2008, 07:15 AM
is there a way to replace to main movie, like loadmovie level 0?
he is always loading it on top of it.

ryosaeb4
April 24th, 2009, 06:01 AM
for anyone interested this is how you do it:}

function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("thevid.swf"));
}
my_mc.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

thanks to all who at least thought about it:beer:


Hi GrndMasterFlash,

How I can pass the name of my movieclip pressed to my function?




a_mc.addEventListener(MouseEvent.MOUSE_DOWN, newvid);
b_mc.addEventListener(MouseEvent.MOUSE_DOWN, newvid);
c_mc.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest(movieclipPressed + ".swf"));
}



I wish pass the name of my movieclip on the stage (a_mc, b_mc, c_mc) to my UrlRequest.

How I can do it in AS 3.0?

thanks

Navee
May 15th, 2009, 04:09 PM
@ryosaeb4

1.) Shorten your method of adding event listeners to multiple objects by using a numeric
naming convention rather and alphabetical naming system. You will see why.

2.) You should create and define custom methods to do the job for you. These methods may
also be used in future projects, adapted in current projects to refactor your code to the
best of practices or create a class package once you have defined enough custom methods
and feel comfortable with using them.

3.) Learn to use your event handler and the syntax that helps you trace the desired output
rather using string cases such as "you clicked me" because it helps you become more
efficient with returning the expected data from the events that you create.

4.) I enjoy helping others and I am in the process of completing a great
"Actionscript 3.0 Creative Programming" book. I owe Kirupa and the loving flash
community for inspiring me during the times AS was racking my brain. So I'm sharing
valuable trade secrets and skipping the basic 101 stuff...and it is also custom trend
amongst the flash community to give back.

I also use a capital KEYWORD naming convention style to define the listeners because it makes the code truly readable when you review it again month later, and it makes sense.

Before we begin: I recommend creating an additional new layer for your custom methods actions
over the layer that will hold your main actions as such

Layer (top) name it "actions - custom methods"
Layer (bottom) name it "actions - main"
Layer etc. the objects you have on the stage (name them mc1, mc2 and mc3 instead of a_mc, b_mc and c_mc)

You will also notice my preferred style of code comment breaks by adding the "|" to separate the method name with param hints. I do this because when I want to use them I just return to the custom methods section and copy the entire method after the "|" and paste it where needed, and assign the values.

It is quicker than writing it all out again when needed and it makes reading the code
easier rather searching through the head section for the keyword function...reading the comment break and end is much better (to me at least!)



// Select "actions - custom methods" layer > Open Actions Panel
// Create the custom method for your movieclip buttons

///////////////////////////////////////////////////// | ADD_Buttons(int, number, "mc_string", func);
function ADD_Buttons($num:int, $iteration:Number, $movieclip:String, $func:Function):void {
for(var i = $num; i<$iteration+1; i++) {
this[$movieclip+i].buttonMode = true;
this[$movieclip+i].visible = true;
this[$movieclip+i].addEventListener(MouseEvent.CLICK, $func);
}
}
///////////////////////////////////////////////////// | REMOVE_Buttons(int, number, "mc_string", func);
function REMOVE_Buttons($num:int, $iteration:Number, $movieclip:String, $func:Function):void {
for(var i = $num; i<$iteration+1; i++) {
this[$movieclip+i].removeEventListener(MouseEvent.CLICK, $func);
}
}
Ok we are finished here. Comment out the buttonMode if you don't desire the hand cursor
The visible property is there for a reason. Because you are using a for loop statement
without needing to write it, and if you only wanted to show a particular movieclip or less
than the amount on the stage; you can handle it by assigning values in the method constructor. I also chose CLICK instead of MOUSE_DOWN. It is good practice to also provide MOUSE_UP when you use MOUSE_DOWN, and you only normally use it when dragging and dropping.

It is a bad habit some programmers from AS2 have adapted with the method onPress (movieclip type) or press (button type).

After all, pressing means you will have to release and that counts as a click...so don't press (MOUSE_DOWN) unless you intend to "do something" when you release (MOUSE_UP) for best practices in coding.

Close this section and go to your "actions - main" layer and open the Actions Panel. If you are wondering why I do this...my basic flash application layer infrastructure looks
like this:

Layer "actions - custom methods"
Layer "actions - common mouse events"
Layer "actions - listeners"
Layer "actions - audio"
Layer "actions - video"
Layer "actions - functions"
Layer "actions - event handlers"
Layer "actions - xml"
Layer "actions - main"

Therefore I have absolutely no question where to go when I want to edit my documents and do not have to scroll through lines of code to find anything in particular. I code normally in the main section and move/cut and paste them in their prospective layers by final project end. The result of the main layer looks like nothing but declared variables (without constructor calls so that I can use them in the other layers) and clearly readable method calls.

BTW: copy the ADD_Buttons(int, number, "mc_string", func); so you can paste it in the main actions layer. After pasting: Just double click each parameter to highlight it and type in the value. It so quick and easy.

The 2nd param $iteration is already adding 1 in the method's definition...so if you have 3 buttons set the actual number. I did that to keep you from playing the 0 index game where you have to remember to add 1 because the index number always starts with 0. Meaning you would have to insert 4 instead in order to have event listeners added to all 3 buttons.

The following exhibits how your code will look in the "actions - main" layer:


var loadit:Loader;
ADD_Buttons(1, 3, "mc", newvid);

function newvid(evt:MouseEvent) {
loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest(evt.currentTarget.name + ".swf"));
trace(evt.currentTarget.name);
}
See how quick and easy that was...now you have 3 active buttons ready in 1 simple line instead of creating one for each movieclip button. You can put 16 clips (or as many as you can fit) on stage and simply put the number of clips as the 2nd param and viola...ready to rock!!!

Now this assumes that your swf files have the same names that your buttons do: meaning mc1.swf, mc2.swf and mc3.swf

Optionall you may not want to assign your swf files to the same name as your buttons and that's okay to BUT I do suggest a numeric naming convention regardless. Here's something you don't see every day in AS: Let's say your swf files were named newvid1, newvid2 and newvid3...Heres the code:


var loadit:Loader;
ADD_Buttons(1, 3, "mc", newvid);
function newvid(evt:MouseEvent) {
var $selected = evt.currentTarget.name.substr(2,1);
loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("newvid" + $selected + ".swf"));
trace(evt.currentTarget.name);
}
What happened? Well I took the name of the movieclip button mc1, mc2 or mc3 and used the String class method substr() Remember: indexes begin with 0 ...I counted up to the index of the number in the movieclips's name. m = 0, c = 1, (the number) = 2

So we use (the number) for the first param for the length of 1 character. The substr() method in this case returns only the number of the movieclip name and assigns it to the variable $selected which in turn concats to "newvid" to select the proper swf file to load.

If you were using higher numbers like 2 digit numbers it would be substr(2,2) or 3 digits substr(2,3) and so forth. Once you get the hang of it you will do creative things like substr($var1, $var) and assign them dyamically from perhaps functions with Number return types. The reason is what if the name of your movieclip was longer than mc? what if it was button_mc? The $var1 would be assigned the value of 9 and the length may utilize buttons that enter the tens. So I create an if statement that checks if the value is less than 10 $var2 = 1 else $var2 = 2...that would complete my substr($var1, $var2) with a click from any button with any name at any length as long as they are numerically named @ their last indexes.


Cheers

Navee
May 15th, 2009, 04:24 PM
Remember we also created a REMOVE_ method as well and if this section where you intend to load swf files was a page within your site...when you select another page from your navigation menu you would call REMOVE_Buttons(1, 3, "mc", newvid); from your navigation menu's click handler function.

Why? Because you have active listeners and it is good practice to remove them from memory when not is use. More than likely you will have other listeners being utilized on other pages like perhaps a gallery...with that said in the event you navigate back to the page that has the newvid swf's you call ADD_Buttons(1, 3, "mc", newvid); and all is back to normal. That's why I use ADD_ / REMOVE_ for methods that I store event listeners in.

I takes away from the guessing game from a list of functions you have to create during programming a flash app. It is so much easier to read through your code and see that you ADDED it and then know exactly where you need to REMOVE it when you review the entire and want to condense (refactor) your code.

Optionally I just copy the whole function ready-made whether ADD_ or REMOVE_ and change the name KEYWORD accordingly--especially when it is already populated with the correct values so I don't have to remember them.

The motto: Work smarter not harder if you plan to professionally pursue programming ELSE you will burn your brains out and eventually lose interest the most fascinating side of computers. They are useless without programming them right?

Cheers

shahi_reegan
April 7th, 2010, 12:41 PM
what are the difference between as2 and as3

starsoccer10921
July 6th, 2010, 12:50 AM
what are the difference between as2 and as3

ROFL