| Ultrashock Forums
• AS3 - Access a MC by it's class name |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
|
2008-07-26
#2 |
||
|
if you are accessing objects already placed on the stage you must give them instance name's on the stage. Then in your document class you should identify it like so: var instanceName:Yellow;
|
|
|
2008-07-26
#3 |
||
|
Thanks for the help haptic. I switched over my var to match the instance name of the movieclips but it still isn't working. Is there something wrong with my if statement? ActionScript Code:
|
|
|
|
2008-07-26
#4 |
||
|
yeah, your if statement should be changed and so should your event listeners. Your button-handling in your class should look more like this: Code:
package {
public class Wheel extends MovieClip {
var yellow_mc:Yellow;
var orange_mc:Orange;
function Wheel() {
yellow_mc.addEventListener(MouseEvent.MOUSE_DOWN,yellowDownHandler);
yellow_mc.addEventListener(MouseEvent.MOUSE_UP,upHandler);
orange_mc.addEventListener(MouseEvent.MOUSE_DOWN,orangeDownHandler);
orange_mc.addEventListener(MouseEvent.MOUSE_UP,upHandler);
}
function yellowDownHandler(e:MouseEvent):void {
snd1.play();
}
function yellowDownHandler(e:MouseEvent):void {
snd2.play();
}
function upHandler(e:MouseEvent):void {
//this is changed assuming that you wanted the clip to do a glowFilter and not the stage itself
var clip:MovieClip = e.target as MovieClip;
TweenMax.to(clip, .15, {glowFilter:{color:0x000000, alpha:0, blurX:8, blurY:8, inner:true}});
}
|
|
17 Creative Assets
|
2008-07-26
#5 |
||
|
Using a single event listener for multiple buttons is fine, in most situations it is better than setting up a listener for each button. @ c02 You are checking for the name of the classes used by the buttons, not the button names. You should change this... Code:
event.target==Yellow Code:
event.target==yellow_mc |
|
|
2008-07-27
#6 |
||
|
Thanks for your help guys. I haven't had a chance to try out haptic's code yet. I was able to change the var and confirm the class names of Yellow and Orange to be Yellow and Orange respectively. It still didn't work. ActionScript Code:
|
|
|
2008-07-27
#7 |
||
|
haptic, I tried your code and it didn't work. Nutrox, should the code above be working knowing that the movieclips on the stage are named yellow_mc and orange_mc and in their linkage their Class names are Yellow and Orange? |
|
17 Creative Assets
|
2008-07-27
#8 |
||
|
Looking at your code again you should be getting errors reported (conflict errors and errors telling you that you haven't imported the Orange and Yellow classes). Have you turned off error reporting or something? |
|
|
2008-07-27
#9 |
||
|
Error reporting is on. It has given me errors when the code is different but not with what I have above. You can check it out here: http://www.gobnm.com/index.zip
|
|
17 Creative Assets
|
2008-07-27
#10 |
||
|
You should still be getting conflict errors reported though if you have MovieClips on the timeline with the instance names "yellow_mc" and "orange_mc", plus you aren't importing those classes which should also be generating errors. Try making your class dynamic, and don't add properties to the class with the same names as the MovieClip instance names. You should be able to access the MovieClips directly that way. Code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public dynamic class Example extends MovieClip
{
public function Example()
{
buttonA.addEventListener( MouseEvent.CLICK, onButtonClick, false, 0, true );
buttonB.addEventListener( MouseEvent.CLICK, onButtonClick, false, 0, true );
}
private function onButtonClick( e:MouseEvent ):void
{
switch( e.target )
{
case buttonA:
{
// do something
trace( "buttonA was clicked" );
break;
}
case buttonB:
{
// do something
trace( "buttonB was clicked" );
break;
}
default:
{
// do nothing
break;
}
}
}
}
}
|
|
|
2008-07-27
#11 |
||
|
Thanks Nutrox. I realized that I said this was a Doc class when it isn't, so the buttonMode isn't affecting the whole swf. I am seeing that I need to go back and hit the books to get a better grasp of classes and how to use them properly. Hopefully Moock can straighten me out. Thanks for your help. |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|


10 comments
| 1499 views



17 Creative Assets
Linear Mode