Ultrashock Forums > Flash > ActionScript
AS3 - Access a MC by it's class name

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - Access a MC by it's class name
Old 2008-07-26

I have a movieclip on the stage with the class name "Yellow". How do I access it in my Document Class? This is what I have so far
Code:
package 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import gs.TweenMax;
	import gs.easing.*
	
	public class Wheel extends MovieClip
	{		
		var snd1:Sound = new Sound1();
		var snd2:Sound = new Sound2();
		var snd3:Sound = new Sound3();
		var snd4:Sound = new Sound4();
		
		public function Wheel()
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN, down);
			this.addEventListener(MouseEvent.MOUSE_UP, up);
			this.buttonMode = true;
		}
			
		private function down(event:MouseEvent):void
		{
			TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:.4, blurX:8, blurY:8, inner:true}});
			if (Yellow)
				{
				  snd1.play();
				}
			if (Orange)
				{
				  snd2.play();
				}
		}
		
		private function up(event:MouseEvent):void
		{
			TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:0, blurX:8, blurY:8, inner:true}});
		}
	}
	
}
postbit arrow 10 comments | 1499 views postbit arrow Reply: with Quote   
co2
Registered User
co2 is offline
seperator
Posts: 153
2002-02-06
Age: 36
co2 lives in United States
co2's Avatar
seperator

Ultrashock Member Comments:
haptic haptic is offline 2008-07-26 #2 Old  
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;
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 2008-07-26 #3 Old  
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:
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.MouseEvent;
  5.     import flash.media.Sound;
  6.     import gs.TweenMax;
  7.     import gs.easing.*
  8.    
  9.     public class Wheel extends MovieClip
  10.     {      
  11.         var snd1:Sound = new Sound1();
  12.         var snd2:Sound = new Sound2();
  13.         var snd3:Sound = new Sound3();
  14.         var snd4:Sound = new Sound4();
  15.         var yellow_mc:Yellow;
  16.         var orange_mc:Orange;
  17.        
  18.         public function Wheel()
  19.         {
  20.             this.addEventListener(MouseEvent.MOUSE_DOWN, down);
  21.             this.addEventListener(MouseEvent.MOUSE_UP, up);
  22.             this.buttonMode = true;
  23.         }
  24.            
  25.         private function down(event:MouseEvent):void
  26.         {
  27.             TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:.4, blurX:8, blurY:8, inner:true}});
  28.             if (event.target==Yellow)
  29.                 {
  30.                   snd1.play();
  31.                 }
  32.             if (event.target==Orange)
  33.                 {
  34.                   snd2.play();
  35.                 }
  36.         }
  37.        
  38.         private function up(event:MouseEvent):void
  39.         {
  40.             TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:0, blurX:8, blurY:8, inner:true}});
  41.         }
  42.     }
  43.    
  44. }
Reply With Quote  
haptic haptic is offline 2008-07-26 #4 Old  
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}});
}
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-07-26 #5 Old  
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
to this...

Code:
event.target==yellow_mc
Also, have you assigned the Yellow and Orange classes to those MovieClip symbols in the SWF library?
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 2008-07-27 #6 Old  
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:
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.MouseEvent;
  5.     import flash.media.Sound;
  6.     import gs.TweenMax;
  7.     import gs.easing.*
  8.    
  9.     public class Wheel extends MovieClip
  10.     {      
  11.         var snd1:Sound = new Sound1();
  12.         var snd2:Sound = new Sound2();
  13.         var snd3:Sound = new Sound3();
  14.         var snd4:Sound = new Sound4();
  15.         var yellow_mc:Yellow;
  16.         var orange_mc:Orange;
  17.         public function Wheel()
  18.         {
  19.             this.addEventListener(MouseEvent.MOUSE_DOWN, down);
  20.             this.addEventListener(MouseEvent.MOUSE_UP, up);
  21.             this.buttonMode = true;
  22.         }
  23.            
  24.         private function down(event:MouseEvent):void
  25.         {
  26.             TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:.4, blurX:8, blurY:8, inner:true}});
  27.             if (event.target==yellow_mc)
  28.                 {
  29.                   snd1.play();
  30.                 }
  31.             if (event.target==orange_mc)
  32.                 {
  33.                   snd2.play();
  34.                 }
  35.         }
  36.        
  37.         private function up(event:MouseEvent):void
  38.         {
  39.             TweenMax.to(this, .15, {glowFilter:{color:0x000000, alpha:0, blurX:8, blurY:8, inner:true}});
  40.         }
  41.     }
  42.    
  43. }
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 2008-07-27 #7 Old  
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?
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-07-27 #8 Old  
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?
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 2008-07-27 #9 Old  
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
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2008-07-27 #10 Old  
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;
				}
			}
		}

	}
}
Another question: If that class of yours is a Document class why are you setting buttonMode to true? That will effect the whole SWF.
Reply With Quote  
co2's Avatar co2 co2 is offline co2 lives in United States 2008-07-27 #11 Old  
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.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: