Question

AS3 - How to Communicate Between Classes

Asked by: tayimagery

I can write custom classes, but only know how to create functions within those classes that affect instances of the symbol to which the class is linked. Here's an example:

My class for a gallery thumbnail includes functions for rolling over and off the thumbnail (causing an animation of the thumbnail itself). This class is attached.As you can see, I only know how to talk to "this" - not another movie clip (one that' defined by a different class or code on the timeline).

I can certainly add a click listener to the class, but I don't know what to write in the function in order to tell my separate movie clip of the full-size images to go to the image corresponding to the selected thumbnail. Consequently, I have to instead create a separate instance name for that thumbnail in the FLA timeline - where I know how to communicate between clips. The result is having a mc symbol in the library - linked to an AS class, and also on the timeline with a separate instance name listening for CLICK, in order to move the full image clip.

I do not have a document class named, and have some idea that this may be part of my ignorance.  

package
{
	import flash.display.*;
	import flash.events.*;
 
	
	public class NewsSub2 extends MovieClip
	{		
				
		public function NewsSub2()
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, over);
			this.addEventListener(MouseEvent.MOUSE_OUT, out);
//want to add this:
this.addEventListener(MouseEvent.CLICK, nav);
			
//now go see the CLICK function below:
 
			this.stop();
			this.buttonMode = true;
		}
		
		
		private function over(event:MouseEvent):void
		{
			if(this.currentFrame == 1)
			{
				this.gotoAndPlay(1);
			}
			
			
			if(this.currentFrame == 10)
			{
				this.gotoAndStop(10);
			}
						
			if(this.currentFrame == 11)
			{
				this.gotoAndPlay(9);
			}
			
			if(this.currentFrame == 12)
			{
				this.gotoAndPlay(8);
			}
			
			if(this.currentFrame == 13)
			{
				this.gotoAndPlay(7);
			}
			if(this.currentFrame == 14)
			{
				this.gotoAndPlay(6);
			}
			if(this.currentFrame == 15)
			{
				this.gotoAndPlay(5);
			}
			if(this.currentFrame == 16)
			{
				this.gotoAndPlay(4);
			}
			if(this.currentFrame == 17)
			{
				this.gotoAndPlay(3);
			}
			if(this.currentFrame == 18)
			{
				this.gotoAndPlay(2);
			}
			if(this.currentFrame == 19)
			{
				this.gotoAndPlay(1);
			}
 
			
		}
		
		private function out(event:MouseEvent):void
		{
			if(this.currentFrame == 1)
			{
				this.gotoAndStop(1);
			}
			if(this.currentFrame == 2)
			{
				this.gotoAndPlay(18);
			}
			if(this.currentFrame == 3)
			{
				this.gotoAndPlay(17);
			}
			if(this.currentFrame == 4)
			{
				this.gotoAndPlay(16);
			}
			if(this.currentFrame == 5)
			{
				this.gotoAndPlay(15);
			}
			if(this.currentFrame == 6)
			{
				this.gotoAndPlay(14);
			}
			if(this.currentFrame == 7)
			{
				this.gotoAndPlay(13);
			}
			if(this.currentFrame == 8)
			{
				this.gotoAndPlay(12);
			}
			if(this.currentFrame == 9)
			{
				this.gotoAndPlay(11);
			}
			
			
						
			if(this.currentFrame == 10)
			{
				this.gotoAndPlay(10);
			}
			
			if(this.currentFrame == 19)
			{
				this.gotoAndStop(1);
			}
			
		}
	
	private function nav(event:MouseEvent):void
		{
			???SEPARATE CLIP???.gotoAndPlay("Selected Thumbnail");
// Naming the separate clip results in RunTime Error 1009 or 1010
		}
		
	}
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-12-04 at 08:36:37ID23957306
Tags

ActionScript 3.0

,

FireFox/IE/Safari

,

http://www.tayimagery.com

Topics

Web Authoring

,

Adobe Flash

,

ActionScript

Participating Experts
2
Points
100
Comments
7

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Timing animation with AS3
    Hi, I am builing a flash animation using the new AS3 tweens. I am trying to do 2 things. (i) trigger one movie when one tween has ended (ii) trigger another tween 10 second after another tweens starts. Really I need to understand how to control timing in AS3 and tween eve...
  2. Flash AS3 - can you help me associate a custom class w…
    Hello, I am a complete beginner with AS3 and OOLs but have been trying to read a lot and getting some help in developing a simple flash website. BUT! I still cant understand / find a clear explanation of how to associate my custom button class with my .fla. I have six mov...
  3. Launch a Movie Clip as a Pop Up in AS3
    I need to know how to do the pop up.I don't want it to be a javascript or any other windows pop up, I want it to launch within the flash movie as a layer over the other movie clip. It doesn't necessarily have to be moveable when it pops up, as long as it appears centred in th...
  4. AS3 Movie Clip buttons
    Helllo- For my site i have multiple Movie Clip buttons that navigate through the website by way of Actionscript 3. I need a clicked movie clip Button to stay in the over state to relay to the viewer what page that they are on. Then when another movie clip button from the ...
  5. duplicating a movie clip but wanting the content to be unique
    Hi, I have a movie clip that has several movie clips inside itself. This movie clip accesses an XML file and that contains data about pictures and text that the movie clip displays. I want TWO of these movie clips to play at the same time in my flash movie. I would like to...
  6. animating movie clips with as3
    im not sure how to phrase this, but i will post the code to clarify. i have eight instances of a movie clip that is only a graphic of a lightbulb, all with their own instance name. Its a game where you have to press a button, and the eight lights illuminate relatively fast ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: iamlfetchPosted on 2008-12-04 at 12:23:46ID: 23098978

yo access data in a class i beilve there are 3 ways of doing so:
1) defining the varible in the class as static and refering to it as className.variable
2) similarly define the varible in the class and refering to it as instance.varible
3) passing the variable to the function eg: var instance = new className(varible);

 

by: tayimageryPosted on 2008-12-04 at 12:49:31ID: 23099274

I am sure you're correct, but I wouldn't know how to actually code that.

 

by: iamlfetchPosted on 2008-12-04 at 13:41:14ID: 23099785

ahh simple really, just a matter of knowing the syntax.
below is the correct way to code it..
i have not made my self clear just say so and i will make it simpler

// to define a static variable just add static to the name.
package
{
public class exampleclass
{
public static var varible1;
public var variable2;
private var variable3;
public function exampleclass (variable3_p)
{
variable3 = variable3_p;
}
}
}
 
// so how do i set the values... simple
 
// this can be used in any class anywhere in the code
exampleclass.varible1 = example1;
 
// both 2 and 3, can only be used in the class where you the set the data and the class that is storing the variable.
var instance1 = new exampleclass();
instance1 .variable2 = example2;
var instance2 = new exampleclass(example3);
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: iamlfetchPosted on 2008-12-04 at 13:42:11ID: 23099797

oops i made a mistake...
the code should look like this

package
{
public class exampleclass
{
public static var varible1;
public var variable2;
private var variable3;
public function exampleclass (variable3_p = null)
{
variable3 = variable3_p;
}
}
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: tayimageryPosted on 2008-12-04 at 14:38:14ID: 23100280

I am starting to understand. So in order for a given object to access code within a class, you must create a var within the class and also set the value(s) of the var in the class. Then the var name is used in the function using the value(s) specified within the class or specified in another class. Then the var value can be used as applicable by being Public static, public, and private, simply setting their value.  

I don't understand variable3_p...specifically the 3_p ..?


 

 

by: iamlfetchPosted on 2008-12-04 at 15:19:54ID: 23100645

ahh thats not exatly right...

the a function can be given parameters... ie
function example(para1,para2){
}

a variable defined inside the class can be used inside the class (any functions inside that class).

public means it is allowed to be used outside the class
private means it is not allowed to be used outside the class

static means it is the same value for all instances of the class, the class doesnt even have to have an instance.

An instance is when you initiate the class, ie,

var instancename = new classname ();

 

by: shaymus22Posted on 2008-12-10 at 18:22:58ID: 23145196

Another way of doing this (if I understand what you're trying to do) would be to access variables using the parent of the classes you're using.  For example, if your document class adds to instances of a class (a circle and a square, for example) using addChild(), accessing the variables between the three things would be very easy.

if part of your document class looks like this:

var testValue:int = 5;
var circleOne:circle = new circle();
var squareOne:square = new square();
addChild( circleOne );
addChild( squareOne );

in the circle or square classes this would cause testValue to be incremented by 1, in this case causing it to go up from 5 to 6.

MovieClip(parent).testValue++;

Similarly, in either of the two classes, you can use values accessible by the parent (which in this case is the document class) like so; this code could be in the circle class:

MovieClip(parent).squareOne.x *= 2;

This would double the x-coordinate of squareOne, an instance of the square class.

If you do this you may get Error #1009, since for a few frames a child added to a MovieClip might not have a value associated with MovieClip(parent).  If you just test to make sure it's not null before you try to use MovieClip(parent) you be able to completely avoid this problem.  You can do it like this:

if( MovieClip(parent) != null )

and then put whatever code you want within the { and } of that if() statement.

I'll be happy to explain this further, if you wish; I hope this helps you out.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...