I'm working in Action Script 3.0 and I have a question in DispatchEvent class.
following code is standalone class.
I want to dispatch event to 'main' class and 'sub' class when event occured.
I'm stuck on this issue. please help me.
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.sub;
public class main extends MovieClip
{
public static const BTN_CLICKED:String = "btn_Clicked";
public function main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
var flashVars:Object = {};
removeEventListener(Event.ADDED_TO_STAGE, init);
if(parent != null && parent.parent != null)
{
flashVars = parent. parent.loaderInfo.parameters;
}
else
{
flashVars = this.root.loaderInfo.parameters;
}
//entry point
var subClass:sub = new sub;
subClass.init();
btn.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(BTN_CLICKED, onbtnClicked, false, 0, true);
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
}
public function onbtnClicked(e:Event)
{
trace("clicked");
}
}
}
and below is 'sub' class.
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.main;
public class sub extends MovieClip
{
public function sub():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//entry point
trace("sub class loaded");
}
}
}
yes, there's nothing in 'sub' class... how can I get dispatch event in sub class?
Related
I have a problem with getQualifiedDefinitionNames, when I compile with AIR 20 I get
Main
gameBg_png$c19135a2672bad8837da970f47c7278f-30390368
and when I compile with Apach Flex 4.15.0 or Adobe Animate CC it returnes everything as expected!
Main
Main__gamebg
how to fix it with AIR, that it returned Main__gamebg class?
my sample code:
package{
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
[Embed(source="../assets/gameBg.png")]
public const _gamebg:Class;
public function Main() {
super();
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
var definitions:*;
if (this.loaderInfo.applicationDomain.hasOwnProperty("getQualifiedDefinitionNames")) {
definitions = this.loaderInfo.applicationDomain["getQualifiedDefinitionNames"]();
for (var i:int = 0; i < definitions.length; i++) {
trace(definitions[i])
}
}
}
}
}
http://forum.starling-framework.org/topic/getqualifieddefinitionnames-porblem?replies=5#post-90611
this trick works.
///////////////
// SomeImage.as
///////////////
[Embed(source="someimage.png")]
public class SomeImage extends Bitmap{
public function get dimensions(): String{
return width + "x" + height;}
}
/////////////
// MyClass.as
/////////////
public class MyClas{
public function foo(): void{
// Instantiate the bound class to get the embedded image
var someImage:SomeImage = new SomeImage();
// ... do whatever you'd like with someImage
trace("Dimensions: " + someImage.dimensions);
}
}
I have this class which should take a button from stage (xBtn).
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx extends MovieClip {
private var xBtn : MovieClip;
public function STXbutonx() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null) : void {
trace("int!"); //this is never called
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
and in Document class I called like this :
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx();
}
}
Why my init function is never called?
Thank you!
Because you never add it to the stage.
Change your Document class to
public function Main() {
xx = new STXbutonx();
addChild(xx);
}
Main holds the reference to the stage, adding a child to Main will add the child to the stage. Thus the event listener in STXbutonx will fire.
xBtn = stage.getChildByName('ics') as MovieClip; // now I have acces of undefined property stage...
You don't have access to the stage because STXButon is not on the stage, it is not an DisplayObject. To get around this, do this:
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx{
private var xBtn : MovieClip;
private var stage : Stage;
public function STXbutonx(stage:Stage) {
this.stage = stage;
init();
}
private function init() : void {
trace("int!");
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
And of course
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx(stage);
}
}
I have the simplest code ever. Main class:
package
{
import field.Field;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var field:Field = new Field();
addChild(field);
field.test();
}
}
}
and a Field class:
package field
{
import flash.display.Sprite;
public class Field extends Sprite
{
public function Field()
{
super();
}
public function test():void
{
}
}
}
test method is presented.
But when I try to compile I get this:
Main.as(26): col: 10 Error: Call to a possibly undefined method test.
field.test();
How could this be happening?
field is your package, that's why you can not do field.test(). So you have to choose another name of your Field instance. You can do like this :
var _field:Field = new Field();
addChild(_field);
_field.test();
Hope that can help.
I've searched few docs and I couldn't find about dispatching event to another classes.
I'm trying to make like this.
'main' class has button.
'main','sub' class gonna trace "button is clicked" when button is clicked
I can trace that in 'main' class with dispatch event.
but 'sub' class is problem.
how can I dispatch event like that into 'sub' class?
Main class
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.sub;
public class main extends MovieClip
{
public static const BTN_CLICKED:String = "btn_Clicked";
public function main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
var flashVars:Object = {};
removeEventListener(Event.ADDED_TO_STAGE, init);
if(parent != null && parent.parent != null)
{
flashVars = parent. parent.loaderInfo.parameters;
}
else
{
flashVars = this.root.loaderInfo.parameters;
}
//entry point
var subClass:sub = new sub;
subClass.init();
btn.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(BTN_CLICKED, onbtnClicked, false, 0, true);
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
}
public function onbtnClicked(e:Event)
{
trace("clicked");
}
}
}
Sub Class
package com
{
import flash.events.*;
import flash.display.MovieClip;
import com.main;
public class sub extends MovieClip
{
public function sub():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//entry point
trace("sub class loaded");
}
}
}
You should relocate var subClass:sub to the main set of main class definitions, beside public static const BTN_CLICKED:String = "btn_Clicked";. Then, you can do subClass.dispatchEvent(...).
public class main extends MovieClip {
private var subClass:sub;
public function main() {
...
subClass=new sub();
addChild(subClass); // not just "init()", it's wrong
...
}
public function onClick(e:MouseEvent)
{
dispatchEvent(new Event(BTN_CLICKED));
subClass.dispatchEvent(new Event(BTN_CLICKED));
}
}
I did the following
package classes
{
// imports removed
public class Main extends MovieClip {
// vars removed
public function Main():void {
trace('--> Main Function started ...');
/**************************************
Preloader
**************************************/
var preLoader:PreLoader = new PreLoader(); // init
stage.addChild(preLoader); // to Stage
stage.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoader.preloaderProgress); // calling the function
doThis();
}
}
With the PreLoader Class looking like this:
package classes {
// imports removed
public class PreLoader extends MovieClip {
public var totalBytes:uint;
public var loadedBytes:uint;
public function PreLoader() {
trace('--> PRELOADER Started ...');
this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
this.loaderBar.scaleX = 0;
}
private function addedHandler(e:Event):void {
trace('Adding Preloader ...');
totalBytes = this.stage.loaderInfo.bytesTotal;
trace('PL: Total ' + totalBytes + ' / ' + loadedBytes);
this.removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
preloaderProgress(e);
}
public function preloaderProgress(e:Event):void {
trace('Progress...');
loadedBytes = this.stage.loaderInfo.bytesLoaded;
this.loaderBar.scaleX = loadedBytes / totalBytes;
this.loaderBar.alpha = 1 - this.loaderBar.scaleX;
if (totalBytes == loadedBytes) {
trace('Removing PreLoader ...');
this.parent.removeChild(this);
}
}
}
}
The Problem: The Preloader is not displayed BEFORE the inital SWF-file has finished loading. Even all the tracing outputs start when the main movie is finished - i did some profiling on 'slow connections'.
If you wonder: doThis() is loading data from a xml-file, from here everything is fine, tracing outputs are at the right time (but too late at all :D)
Thank you in advance!
Edit: Maybe the question is: Is there a way to determine when the main movie is starting to load?
Make sure that you don't have any content other than the preloader on stage in the first frame. Otherwise all that content will have to load before the preloader starts up.
If you are working in FlashIDE you have to put your preloader into the first frame. If you prefer to use another IDE you should use Frame metatag. Example:
Main.as
package com.sample.preloader
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author Author
*/
[Frame(factoryClass="com.sample.preloader.Preloader")]
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
}
}
Preloader.as
package com.sample.preloader
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
/**
* ...
* #author Author
*/
public class Preloader extends MovieClip
{
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO show loader
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
// TODO update loader
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO hide loader
startup();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("com.sample.preloader.Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}