7 Replies - 912 Views - Last Post: 10 September 2009 - 08:19 AM

#1 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

How to detect objects within Class

Post icon  Posted 03 September 2009 - 01:53 PM

Hey Guys,

I'm going crazy here - I have a huge file (it's only about 45% done) and I want to use AS3 the correct way by placing these things into classes in their own .as documents - but when I do the DOM breaks apart. Can someone help me, just give me a simple example if you don't want to use my actual code:

import flash.events.Event;
import flash.events.MouseEvent;
import caurina.transitions.Tweener;	
	
	/////////////////////////////////////////////
	//										 //
	//		 INITIAL VARIABLES			   //
	//										 //
	/////////////////////////////////////////////

	// DASHBOARD STATES
	//**********************************************
	
		var shipState = "off";
		var railState = "off";
		var roadState = "off";
		var airState = "off";
		var pipelineState = "off";
		var militaryState = "off";
		
	// LOCATION INFORMAION
	//**********************************************
	
		this.cityInfo_mc.alpha = 0; // Hides MC from Stage
		this.cityInfo_mc.cityIcon_rail.visible = false;
		this.cityInfo_mc.cityIcon_road.visible = false;
		this.cityInfo_mc.cityIcon_ship.visible = false;
		this.cityInfo_mc.cityIcon_air.visible = false;
		this.cityInfo_mc.cityIcon_oil.visible = false;
		this.cityInfo_mc.cityIcon_military.visible = false;
		
		
		var location0_cityName = "Barrow";
		var location0_cityContent = "Bla Bla";
		

	// Set initial transparency of routes
	//*************************************************

	rivers_mc.alpha = .25;
	pipeline_mc.alpha = 0;
	roads_mc.alpha = 0;
	rail_mc.alpha = 0;
	air_mc.alpha = 0;
	
	// NAVIGATION HOVER COLOR
	//************************************************
		var hover = "0xF1E960";
		var normal = "0xFFFFFF";
		
		 function changeColor(object, color){
			var colorchange:ColorTransform = new ColorTransform();
			colorchange.color = color;
			object.transform.colorTransform = colorchange;
		} 
			
	//attaching mouse events to the button on stage
	//*************************************************
	
		for(var i:int=0; i < 6; i++) {	
			dashboard["dash"+i].addEventListener(MouseEvent.CLICK, userClick);
			dashboard["dash"+i].addEventListener(MouseEvent.MOUSE_OUT, userOut);
			dashboard["dash"+i].addEventListener(MouseEvent.MOUSE_OVER, userOver);		
		}	
		
		for(var l:int=0; l < 11; l++) {	
			this["location"+l].addEventListener(MouseEvent.CLICK, locationclick);
			//this["location"+l].addEventListener(MouseEvent.MOUSE_OUT, locationOut);
			//this["location"+l].addEventListener(MouseEvent.MOUSE_OVER, locationOver);		
		}	
		
		this.cityInfo_mc.closeButton.addEventListener(MouseEvent.CLICK, cityClick);
	
	// Set initial transparency of dashboard buttons
	//*************************************************
		dashboard.dash0.alpha = .50;
		dashboard.dash1.alpha = .50;
		dashboard.dash2.alpha = .50;
		dashboard.dash3.alpha = .50;
		dashboard.dash4.alpha = .50;
		dashboard.dash5.alpha = .50;
		
	/////////////////////////////////////////////
	//										 //
	//		 DASHBOARD MOUSE CONTROL		 //
	//										 //
	/////////////////////////////////////////////
	
	
		// MOUSE OVER FUNCTIONS
		//*************************************************
		
			 function userOver(event:MouseEvent):void {
				
				// RAIL
				if(event.target.name == "dash0") {
					Tweener.addTween(rail_mc, {alpha: 1, time: 2});
					Tweener.addTween(this.dashboard.dash0, {alpha: 1, time:2});			
				}
				
				// ROADS
				if(event.target.name == "dash1") {
					Tweener.addTween(roads_mc, {alpha: 1, time:2});
					Tweener.addTween(this.dashboard.dash1, {alpha: 1, time:1});
				}
				
				// SHIPPING
				if(event.target.name == "dash2") {
					Tweener.addTween(rivers_mc, {alpha: 1, time:2});
					Tweener.addTween(this.dashboard.dash2, {alpha: 1, time:1});
				}
				
				// AIRPORTS
				if(event.target.name == "dash3") {
					Tweener.addTween(air_mc, {alpha: 1, time:2});
					Tweener.addTween(this.dashboard.dash3, {alpha: 1, time:1});
				}
				
				// PIPELINE
				if(event.target.name == "dash4") {
					Tweener.addTween(pipeline_mc, {alpha: 1, time: 2});
					Tweener.addTween(this.dashboard.dash4, {alpha: 1, time:1});
				}
				
				// MILITARY
				if(event.target.name == "dash5") {
					Tweener.addTween(this.dashboard.dash5, {alpha: 1, time:1});
				}
			}
			
		// MOUSE OUT FUNCTIONS
		//*************************************************
			
			 function userOut(event:MouseEvent):void {
				
				// RAIL
				if(event.target.name == "dash0") {
					Tweener.addTween(rail_mc, {alpha: 0, time: 2});
					Tweener.addTween(this.dashboard.dash0, {alpha: .5, time:2});
				}
				
				// ROAD
				if(event.target.name == "dash1") {
					Tweener.addTween(roads_mc, {alpha: 0, time: 2});
					Tweener.addTween(this.dashboard.dash1, {alpha: .5, time:2});
				}
					
				// SHIPPING
				if(event.target.name == "dash2") {
					Tweener.addTween(rivers_mc, {alpha: .25, time:2});
					Tweener.addTween(this.dashboard.dash2, {alpha: .5, time:2});
				}
				
				// AIRPORTS
				if(event.target.name == "dash3") {
					Tweener.addTween(air_mc, {alpha: 0, time:2});
					Tweener.addTween(this.dashboard.dash3, {alpha: .5, time:2});
				}
				
				// PIPELINE
				if(event.target.name == "dash4") {
					Tweener.addTween(pipeline_mc, {alpha: 0, time: 2});
					Tweener.addTween(this.dashboard.dash4, {alpha: .5, time:2});
				}
				
				// MILITARY
				if(event.target.name == "dash5") {
					Tweener.addTween(this.dashboard.dash5, {alpha: .5, time:2});
				}
			}
			
		// MOUSE CLICK FUNCTIONS
		//*************************************************
				
			 function userClick(event:MouseEvent):void {
				
				// RAIL
				if(event.target.name == "dash0") {		
					if(railState == "on") {
					   railState = "off";
					   Tweener.addTween(rail_mc, {alpha: .15, time:2});
					   changeColor(this.dashboard.dash0, normal);
					   this.dashboard.dash0.addEventListener(MouseEvent.MOUSE_OUT, userOut);
					}
					else {
					   railState = "on";
					   this.dashboard.dash0.removeEventListener(MouseEvent.MOUSE_OUT, userOut);
					   changeColor(this.dashboard.dash0, hover);
					   Tweener.addTween(rail_mc, {alpha: 1, time:2});
					}
				}		
				
				// ROAD
				if(event.target.name == "dash1") {		
					if(roadState == "on") {
					   roadState = "off";
					   Tweener.addTween(roads_mc, {alpha: .15, time:2});
					   changeColor(this.dashboard.dash1, normal);
					   this.dashboard.dash1.addEventListener(MouseEvent.MOUSE_OUT, userOut);
					}
					else {
					   roadState = "on";
					   this.dashboard.dash1.removeEventListener(MouseEvent.MOUSE_OUT, userOut);
					   changeColor(this.dashboard.dash1, hover);
					   Tweener.addTween(roads_mc, {alpha: 1, time:2});
					}
				}		
				
				// SHIPPING
				if(event.target.name == "dash2") {		
					if(shipState == "on") {
					   shipState = "off";
					   Tweener.addTween(rivers_mc, {alpha: .10, time:2});
					   changeColor(this.dashboard.dash2, normal);
					   this.dashboard.dash2.addEventListener(MouseEvent.MOUSE_OUT, userOut);
					}
					else {
					   shipState = "on";
					   this.dashboard.dash2.removeEventListener(MouseEvent.MOUSE_OUT, userOut);
					   changeColor(this.dashboard.dash2, hover);
					   Tweener.addTween(rivers_mc, {alpha: 1, time:2});
					}
				}
				
				// Airports
				if(event.target.name == "dash3") {		
					if(airState == "on") {
					   airState = "off";
					   Tweener.addTween(air_mc, {alpha: .15, time:2});
					   changeColor(this.dashboard.dash3, normal);
					   this.dashboard.dash3.addEventListener(MouseEvent.MOUSE_OUT, userOut);
					}
					else {
					   airState = "on";
					   this.dashboard.dash3.removeEventListener(MouseEvent.MOUSE_OUT, userOut);
					   changeColor(this.dashboard.dash3, hover);
					   Tweener.addTween(air_mc, {alpha: 1, time:2});
					}
				}		
				
				// PIPELINE
				if(event.target.name == "dash4") {		
					if(shipState == "on") {
					   shipState = "off";
					   Tweener.addTween(pipeline_mc, {alpha: .15, time:2});
					   changeColor(this.dashboard.dash4, normal);
					   this.dashboard.dash4.addEventListener(MouseEvent.MOUSE_OUT, userOut);
					}
					else {
					   shipState = "on";
					   this.dashboard.dash4.removeEventListener(MouseEvent.MOUSE_OUT, userOut);
					   changeColor(this.dashboard.dash4, hover);
					   Tweener.addTween(pipeline_mc, {alpha: 1, time:2});
					}
				}		
				
				
				
			}
		
	/////////////////////////////////////////////
	//										 //
	//		 LOCATIONS MOUSE CONTROL		 //
	//										 //
	/////////////////////////////////////////////
	
		// MOUSE OVER CONTOLS
		//*******************************************
		
			function locationclick(event:MouseEvent):void {
				if(event.target.name == "location0") {
					Tweener.addTween(cityInfo_mc, {alpha: 1, time:2});
					this.cityInfo_mc.cityName_txt.text = location0_cityName;
					this.cityInfo_mc.cityContent_txt.text = location0_cityContent;
					this.cityInfo_mc.cityIcon_road.visible = true;
					this.cityInfo_mc.cityIcon_ship.visible = true;
					this.cityInfo_mc.cityIcon_air.visible = true;
				}
			}
			
	/////////////////////////////////////////////
	//										 //
	//			CITY MOUSE CONTROL		   //
	//										 //
	/////////////////////////////////////////////
			
		// MOUSE CLICK CONTOLS
		//*******************************************
		
			function cityClick(event:MouseEvent):void {
				if(event.target.name == "closeButton") {
					Tweener.addTween(cityInfo_mc, {alpha: 0, time:1});
				}
			}



Is This A Good Question/Topic? 0
  • +

Replies To: How to detect objects within Class

#2 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: How to detect objects within Class

Posted 04 September 2009 - 02:37 PM

Maybe I'm not explaining it clearly, but all I am looking to do is have an external file say:

test.as
test.text = "testing";



Then in my flash movie I have a movie text box called test - and that doesn't change the text, even if I've imported the class. Can someone help me out here.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#3 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to detect objects within Class

Posted 05 September 2009 - 04:20 AM

Here's a brief example:

I create an fla. I put a textfield onto the stage and in the properties panel for this field I set it to "dynamic text" and it the name "myField".

I convert the textfield to a movieclip symbol and give that the instance name myClip.

I then write a basic document class and name it TextChange.as.

package {
  import flash.display.MovieClip;

  public class TextChange extends MovieClip {
	public function TextChange() {
	  this.myClip.myField.text = "hello world";
	};
  };
};



I set the document class for the fla as TextChange and export :)

If I wanted classes other than the DocumentClass to have access to this text field I would need to make the movieclip available for actionscript (rightclick it in the library, choose linkage, check export for actionscript, give it the classname FieldMC) and then pass it to the other classes as a parameter when they are instantiated or after through some setter function:

e.g. within your document class:
var otherClass:OtherClass = new OtherClass(this.myClip);



Alternatively you could subclass FieldMC and implement it as a singleton, effectively making the instance of FieldMC global.

Hope that helps.

This post has been edited by SoLi: 05 September 2009 - 04:46 AM

Was This Post Helpful? 1
  • +
  • -

#4 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: How to detect objects within Class

Posted 08 September 2009 - 08:32 AM

Then what am I doing wrong - here is my class:

package script {
	
  import flash.display.MovieClip;
  
  public class elementTransparency {
	  
		// Set initial transparency of elements
		//*************************************************
		
		rivers_mc.alpha = .25;
		pipeline_mc.alpha = 0;
		roads_mc.alpha = 0;
		rail_mc.alpha = 0;
		air_mc.alpha = 0;
		militaryAreas.alpha = 0;
		this.cityInfo_mc.detailView_btn.visible = false;
		this.shippingPaths_mc.alpha = 0;
		this.ser_kodiak.alpha = 0;
		this.ser_naknek.alpha = 0;
		this.ser_dillingham.alpha = 0;
		this.ser_bethel.alpha = 0;
		this.ser_nome.alpha = 0;
		this.ser_dutchHarbor.alpha = 0;
		this.ser_kotzebue.alpha = 0;
		this.ser_barrow.alpha = 0;
		this.ser_nenana.alpha = 0;
		
		dashboard.dash0.alpha = .50;
		dashboard.dash1.alpha = .50;
		dashboard.dash2.alpha = .50;
		dashboard.dash3.alpha = .50;
		dashboard.dash4.alpha = .50;
		dashboard.dash5.alpha = .50;
		
  }
}



I am merely trying to set the initial transparency from within a class. Here is my import code:

import script.elementTransparency;



I have it within a script folder within the root - so the folder path is such:
[code]
root/script/elementTransparency.as
[code]

You can look at the above class to see which our accessing the stage and which are accessing objects within movie clips. Let me know, because all of the objects set above are at 100% transparency.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#5 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to detect objects within Class

Posted 09 September 2009 - 02:28 PM

Hey mate.

import script.elementTransparency;



Where is this line currently placed? Is it in a separate AS file that is set as the flash document class?

Importing a class does not actually instantiate it, it only makes it available for use. From what I can see it looks as though you don't actually need to import the class at all, rather that you want to set elementTransparency as the document class.

If elementTransparency is not intended to be the document class, then the document class needs to be passed as an argument to elementTransparency so that it may have access to all those objects that are on the initial FLA stage. Here's an example, similar to your application, that might do that:

The document class:
package {
  import flash.display.MovieClip;
  import script.elementTransparency;

  public class MyClass extends MovieClip {
	public function MyClass() {
	  new ElementTransparency(this);
	};
  };
};



and a very brief elementTransparency class:

package script {
  import flash.display.MovieClip;
  public class elementTransparency {
	   // parent has an underscore because parent itself is a reserved keyword
	public function elementTransparency(_parent:MovieClip) { 
	  _parent.air_mc.alpha = 0;
	}
  }
}




Let me know if that clears things up at all, or if I've totally misunderstood your problem :)

Just a few things I noticed. While your code indeed makes use of a class, I wouldn't necessarily consider it to be a very OOP way of doing things.

Rather than having a single class that is aware of, and sets the alpha for many objects on your stage, it might be better, for example, to have a DashBoard class which each of your dashboards inherits from, the same could be said for the air, road, etc objects; they are variations on a single concept so they ought to inherit their behavior and modify only when diverging from the norm. This way you only need to set the alpha property on the superclass, and all your dashboard subclasses automatically inherit it.

This post has been edited by SoLi: 09 September 2009 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#6 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: How to detect objects within Class

Posted 09 September 2009 - 03:13 PM

Quote

Rather than having a single class that is aware of, and sets the alpha for many objects on your stage, it might be better, for example, to have a DashBoard class which each of your dashboards inherits from, the same could be said for the air, road, etc objects; they are variations on a single concept so they ought to inherit their behavior and modify only when diverging from the norm. This way you only need to set the alpha property on the superclass, and all your dashboard subclasses automatically inherit it.



Sounds perfect - I may have to take some time to get the FLASH way of using it, as that is how CSS works and makes sense to use it that way in Flash. But the syntax is where I am getting hung up.

I will try this when I get a chance. Thanks for the help.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#7 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to detect objects within Class

Posted 09 September 2009 - 04:24 PM

Personally, I really struggled to learn AS3 (and especially OOP AS3) within the Flash IDE, especially when trying to make the switch from AS2. I found that things in the flash IDE, such as the 'timeline', 'symbols' and 'instance names' were actually a whole big confusing distraction from what can be achieved purely in AS3 as a compiled language. I needed to take a big step back from Flash and AS2 before I was able to realise that AS3 was basically a whole new ball game, even though some of the syntax was somewhat similar.

I think the best way to get to grips with OOP AS3 is to write your application from the ground up in a text editor, only using the FLA file for the library of simple animations and buttons, etc, you might need along the way.

Hope that helps.

This post has been edited by SoLi: 09 September 2009 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: How to detect objects within Class

Posted 10 September 2009 - 08:19 AM

Yeah, I didn't touch the timeline once for animation thanks to tweener and I love it. Way less issues when someone wants something changed, then I don't have to move hundreds of frames to make it work.

--

Greg
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1