Posts Tagged ‘ class ’

access stage from class without adding..

Hi,

Here I have given the easiest way to access stage from a class file without using ADDED_TO_STAGE,

We can access the stage by passing a reference of stage in a class file…

Here i have given you a sample code to access….

in AccessStageByReference.as


package{

	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class AccessStageByReference {
		
		private static var initialized:Boolean = false;		
		
		public function initialize(stage:Stage) {
			if (!initialized) {
				// assign listeners for key presses and deactivation of the player
				stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
				stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
				trace(stage)
				
				initialized = true;
			}
		}
		
		function keyPressed(_event:KeyboardEvent):void{
			trace("KeyPressed")
		}
		
		function keyReleased(_event:KeyboardEvent):void{
			trace("keyReleased")
		}
		
		function clearKeys(_event:KeyboardEvent):void{
			trace("clearKeys")
		}
	
	}
}

you can send a reference for stage into the class by using initialize method..

jujst code like below in the .fla file

var aa:AccessStageByReference = new AccessStageByReference()
aa.initialize(stage)

Access stage from class file in AS3

hi friends.

Keyboard events on stage are not working… why?

cant access stage from a class file… why?..

How can i change framerate from a class file..?..

I got this error “Error #2012: Stage class cannot be instantiated.” why?…

If I access stage it given the result “Null” – Why?…

I heard a lots of questions like below from our friends.. its all about accessing stage form a class file…

Don’t Worry I have given you the solutions to access stage from a class.

Why this happened ( Why cant access from all class files)..?…

hi really good Questions is this.. So I found your basically a AS2 developer right?…

yeah this question raised mostly by AS2 developers basically.Because we have the Stage variable at globally in AS2. But in AS3 we don’t have this.. :(.

stage is not a global variable. It’s a property that is common to all DisplayObjects, but it is null until the object is added to the display list.

All DisplayObject (flash.display.DisplayObject) instances have stage and root properties. These properties, however, are null unless the display object is attached to the stage or a display list that is attached to the stage. The stage property, when accessible, will always reference the stage object. The root property references different objects depending on the display object:

  • For the stage, root always references the stage
  • For the main timeline of the SWF and all display objects within it, root references the main timeline
  • If an object is added directly to the stage from any timeline, the root property for it and its children references the stage
  • For display objects in loaded SWF files, root references the main timeline of that SWF file
  • For loaded bitmap images, root references the Bitmap instance of the image loaded
  • Loader objects used to load external SWFs and images follow the rules of all other display objects within the SWF it is being used

The solution to clear the problem

To access the stage from class, first we need to add this displayobject like sprite, movieclip, shape. after that we can get the stage properties from displayobjects.

Save a c;ass file like below as ExampleForStageAccess.as.


package{
 import flash.display.*;
 import flash.events.*

 public class ExampleForStageAccess extends MovieClip{

 public var __stage:Stage;

 public function ExampleForStageAccess():void{

 // ITS IMPORTANT ONE ---------------------
 addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
 }

 private function stageAddHandler(e:Event):void {
 __stage = this.stage;
 removeEventListener(Event.ADDED_TO_STAGE, stageAddHandler);

 trace("Frame Rate ::"+__stage.frameRate);
 trace("Width      ::"+__stage.width);
 trace("Height     ::"+__stage.height);
 __stage.scaleMode = StageScaleMode.NO_SCALE;
 __stage.align = StageAlign.TOP_LEFT;
 __stage.addEventListener(Event.ACTIVATE, activateHandler);
 __stage.addEventListener(Event.RESIZE, resizeHandler);

 }

 private function activateHandler(event:Event):void {
 trace("activateHandler: " + event);
 }

 private function resizeHandler(event:Event):void {
 trace("resizeHandler: " + event);
 trace("stageWidth: " + stage.stageWidth + " stageHeight: " + stage.stageHeight);
 }

 }
}

then code in your timeline like below..


var hereIsStageAccess:ExampleForStageAccess = new ExampleForStageAccess();
addChild(hereIsStageAccess);

If this helps to you..comment here…