View Full Version : Simple Classes Question
jeancnicolas
June 22nd, 2009, 05:27 AM
Hi .. Im just getting in to start using classes ..
I have a very simple question::
So if I create a rollOver and rollOff class package - to change the alpha value of some buttons ..
how do 'connect' them to my buttons?? .. if I was simply hard-coding them in I would::
myButton.addEventListener(MouseEvent.MOUSE_OVER,my Function);
But this will be tedious with 10 buttons for the on and off states!
Great -- simple it must be .. thanks for help ../
.ral:cr
June 22nd, 2009, 05:39 AM
you put them inside the button class
jeancnicolas
June 22nd, 2009, 07:00 AM
as in::
var button1:myButton = new myButton();
addChild(button1);
the instance of myButton in the library is checked as 'myButton' class path for export .. linking to the appropriate 'myButton.as' class package?
si?
thanks
BoppreH
June 22nd, 2009, 08:08 AM
This would be an option. The other would be iterating through all the buttons and putting the event listener on them.
To iterate through them, you can either list them in a array or place them inside the same DisplayObjectContainer and then use
for (var i:int = 0; i < container.numChildren; i++) {
var child:DisplayObject = container.getChildAt(i)
child.addEventListener(MouseEvent.MOUSE_OVER, myFunction);
}
I personally prefer the former, because sometime you need more than one kind of button with this behavior and I rarely have good experiences with the linkage option.
jeancnicolas
June 22nd, 2009, 08:12 AM
Yeah - I see and agree. It makes more sense to me this way.
However! I guess, im trying to get the practice in for writing code with a better idea for class based structure!
BoppreH
June 22nd, 2009, 08:29 AM
If it's for experience (or re-usability) purposes, go for the classes. Make a MyButton class, add the event listener inside the constructor and link it to an object.
jeancnicolas
June 22nd, 2009, 08:40 AM
cool .. the event listener inside the class, of course.
Linking it to the object - you mean giving it a class path, accessed via the library?
&What about if you wanted to make a button from a *.png loaded in from outside of the swf..
thanks man
.ral:cr
June 22nd, 2009, 08:58 AM
in this case create a new mc symbol in the library that will load a photo with Loader, an dlink it to the same button class.
BoppreH
June 22nd, 2009, 09:02 AM
To link it to a movieclip you use the Linkage options in the library.
To apply this code to an external image you need to first handle the image loading, and then , when it's done, apply the event listener. I would start with a MovieClip extending class requiring an image url as parameter and adding the listener and the image to itself when loaded.
jeancnicolas
June 22nd, 2009, 09:04 AM
nice one chaps .. tis a good place to start :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.