View Full Version : cant load MC for button ROLL_OVER
zdvdla
07-14-2007, 07:56 AM
I created a MC that I want to play on top of buttons when they are rolled over .
I add the EL and the EH to them that part works fine. I would like however to call a new instance of a MC to play over them as they are rolled over.
var pictLdr:Loader = new Loader();
var pictURL:String = "egg.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
this.addChild(pictLdr);
this works for a .jpg
how can I modify this for a movie clip?
I tried
var ballLdrisplayObject = ball_mc;
this.addChild(ballLdr);
I get this error
1120: Access of undefined property ball_mc.
Im testing with a simple anim to get it and have not had any success.
I have spent hours and still have no clue...
any help would be awesome..
thanks
Mazoonist
07-14-2007, 02:15 PM
Right click your movie clip in the library and choose "Linkage"
Give it a class name. Flash will warn you there is currently no class with that name. Just click OK, Flash will create the class for you. But now you have a class name that you can use to add new instances with Actionscript. And doing that is as easy as this:
var myClip:ClassName = new ClassName(); //where ClassName is the name you used
addChild(myClip);
zdvdla
07-14-2007, 06:26 PM
wow! Thanks! works just like that...
question though..Am I to understand that when adding the Child, I am really adding an instance of the Class rather than the MovieClip.
The stage is playing the Class, not my particular movie?
So make a custom Class for each MovieClip I may want to add?
zdvdla
07-14-2007, 07:58 PM
so how would i load a specific MovieClip without using new?
this of course creates a new instance over and over again.
I simply want the MovieClip to play (loop)...
I created a simple ball animation called ball_mc. set the class to ballClass in the linkage props.
this works but keeps creating new clips over and over...
var myClip:ballClass = new ballClass();
addChild(myClip);
myClip.x = 250;
myClip.y = 250;
I want to add ball_mc only once and have it loop.
I can't get how to set that. I tried:
var myClip:ballClass = ball_mc();
addChild(myClip);
myClip.x = 250;
myClip.y = 250;
I get these errors..........
1046: Type was not found or was not a compile-time constant: ballClass.
1180: Call to a possibly undefined method ball_mc.
I must not have a grip on a simple concept somewhere...
Mazoonist
07-15-2007, 02:32 AM
I'll put it this way: Creating a new instance of the MovieClip using Actionscript is basically the same as dragging one out from the library and adding it to the stage. Using var to set the new instance equal to the variable name you pick is the equivalent of setting an instance name in the properties panel.
MovieClips in the library are actually templates... they are like the master copy for making more copies. They are actually exactly like a Class. A custom MovieClip in the library is exactly like a Class that extends MovieClip. When you drag a MovieClip out of the library, you are essentially instantiating an object of a class. Not just an instance of the MovieClip class, but an instance of that particular (custom) MovieClip.
When you edit a MovieClip, you are editing the template. All instances on the stage will change to reflect whatever edits you make. This is true whether you edit by double clicking the master clip in the library, or you double click one of the instances on the stage. The only difference in the two ways of editing is that one is "edit in place" and the other is not. Edit in place lets you see the edits you're making in the context of the overall stage. But that is the only difference! Either way you're editing the "master" copy (or, what I call the "rubber stamp," below).
To see this in action, create a new FLA file, draw a rectangle, convert it to a movie clip. Then drag out two more copies of it (more if you like). Double click on of them on the stage to go into it's edit mode. Change the movie clip by drawing a circle over the top of the existing rectangle. You will see all the instances of the clip on stage also change to reflect the editing you're doing. So you can see that when you edit a clip, you're editing the master copy, whether you do it by double clicking the clip in the library or one of it's instances.
Now back to the Actionscript: To add two instances of the same MovieClip, you would just assign different variable names:
var myClip1:Square = new Square();
var myClip2:Square = new Square();
myClip1.x = 100;
myClip1.y = 100;
myClip2.x = 200;
myClip2.y = 200;
addChild(myClip1);
addChild(myClip2);
This is the equivalent of dragging two instances of Square out of the library and giving them instance names of myClip1 and myClip2.
Think of the clip in the library as a "mold" or even a rubber stamp. The rubber stamp stamps out a list of methods and properties. Each copy (instance) gets it's own list of methods and properties. So in the above, since the two clips came from the same rubber stamp, they each have their own list (and this is true whether they're added by drag and drop or created with code!). Included on that list, each gets it's own property of x and y. So you can address the x and y of each one individually, which you must do if you don't want them stacked up on top of each other.
Trouble is, we call something on the stage a MovieClip, and then we call a master copy in the library a MovieClip also. The one on the stage is a movie clip instance, and the one in the library is the rubber stamp master copy.
Different instances of the same master MovieClip can be different from each other, but only insofar as their properties allow them to be. But the properties allow quite a range of differences, including size, rotation, alpha, etc etc.... You know what the properties are.
Hint: If you need another MovieClip that's almost exactly like an exisiting one, but it goes beyond what you can achieve using their properties, then you can duplicate the first clip. Right click it in the library, and choose "Duplicate."
zdvdla
07-15-2007, 05:25 PM
that makes sense..thanks for taking the time to explain that..
now I get it.
Ive been reading the help files, and I need a help file to understand them.
Is there a better reference somewhere else?
thanks again for your time.
Mazoonist
07-15-2007, 08:32 PM
Hopefully you were able to make your file do what you wanted it to. I wasn't quite clear on whatever rollover effect you were trying to achieve. I would need to take a peek at your file.
Flash's help files are notoriously not helpful, I know. But in this latest version they are way better. I like the "all classes" listing. For the most part, it is indeed a reference, and not a "how to." I rely a lot on books.
zdvdla
07-16-2007, 03:48 AM
Ive been trying all day and cant get the MC to stop playing.
I have a MC that I want to play over buttons when they are rolled over.
Class sparkle.
I want the MC to stop when the user rolls out.
I dont understand why Im getting this error or what the best way to achieve my desired result.
//Btn_Home_mc the instance of a particular button
Btn_Home_mc.buttonMode = true;
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OVER, HomeSpark);
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OUT, HomeSparkClear);
function HomeSpark(event:MouseEvent):void
{
if (spark == null)
{
var spark:sparkle = new sparkle;
addChild(spark);
spark.x = 256.1;
spark.y = 276.7;
}
}
function HomeSparkClear(event:MouseEvent):void
{
removeChild(spark);
}
ERROR
ReferenceError: Error #1065: Variable spark is not defined.
I tried every possible variation of moving and calling but dont get it..
please help...
thanks
Mazoonist
07-16-2007, 11:33 AM
What you're bumping up against is fairly simple. Notice that you define the variable "spark" and make it equal to a new sparkle, but you do it inside the function using the var keyword. This causes a situation where the variable is only known inside that function, but to the rest of the code it doesn't exist. So when the other function says, removeChild(spark) it doesn't work, because it has no reference to it.
Here's the fix:
//Btn_Home_mc the instance of a particular button
Btn_Home_mc.buttonMode = true;
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OVER, HomeSpark);
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OUT, HomeSparkClear);
//declared outside of any functions, now the rest of the code "knows" about it
var spark:sparkle;
function HomeSpark(event:MouseEvent):void {
if (spark == null) {
//var spark:sparkle = new sparkle; <--don't do this
//do this instead:
spark = new sparkle();
addChild(spark);
spark.x = 256.1;
spark.y = 276.7;
}
}
function HomeSparkClear(event:MouseEvent):void {
removeChild(spark);
}
Here's how it works (what they call variable "scope" that is): wherever you define a variable using the var keyword, whatever set of curly braces you're in, the rest of the nested curly braces will know that variable:
{
// I don't know about foo
{
//I don't know about foo
{
var foo:Number = 5;
{
//I know about foo
{
//I know about foo
{
//I know about foo .... etc, etc
}
}
}
}
}
}
Naturally, if you define foo outside of all the curly braces, the whole thing will know that variable, as will any other functions. You define a variable whenever you use the var keyword. Sometimes it's handy to define a variable, then give it a value later:
var foo:Number;
foo = 5;
or in your case:
var spark:sparkle;
spark = new sparkle();
Mazoonist
07-16-2007, 12:00 PM
In the future, if you wrap your code with AS tags, it's much easier to read, and preserves the indentation. Just type [ as ] in front of it and [ /as ] after it. Do it without the spaces, though. I added spaces so it wouldn't activate.
Mazoonist
07-16-2007, 10:39 PM
I was just looking over that code block, and really, this is much better:
//Btn_Home_mc the instance of a particular button Btn_Home_mc.buttonMode = true;
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OVER, HomeSpark);
Btn_Home_mc.addEventListener(MouseEvent.ROLL_OUT, HomeSparkClear);
var spark:sparkle = new sparkle();
function HomeSpark(event:MouseEvent):void {
addChild(spark);
spark.x = 256.1;
spark.y = 276.7;
}
function HomeSparkClear(event:MouseEvent):void {
removeChild(spark);
}
This way, the spark object only gets created once, and the Mouse events add the object and remove it again. No need to check anymore if the object is null, since there's only one object.
You should also think of a way to get rid of the hard-coded numbers for x and y. One way would be to add the spark as a child of the Button. Then the x and y coordinates would be measured from the perspective of the button instead of the stage. You can also refer to the button by using event.target instead, which would make your function reusable:
function HomeSpark(event:MouseEvent):void {
event.target.addChild(spark);
spark.x = 0;
spark.y = 0
}
zdvdla
07-31-2007, 09:23 AM
I built a site using flash and the client now wants me to add a shopping cart.
The instructions seem fairly straight forward but I dont know how to apply it to this site PayPal gives you the code, says to paste in into your page. Should I be using DW to do this? The code is HTML but Im not sure where to paste it.
I have a scrolling tile list that is programmed to listen for a CLICK and display a larger picture of the item and a brief description of it.
I am thinking that I can pull the name from the SOURCE clicked in the tile list and have a handler place the correct btn on the page.
I haven't come across anything on the web that tells me how to place HTML directly into a flash doc.
I could just export the swf and bring it into DW, but I really want to know that I did this all in Flash.
Any ideas?
Thanks...
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.