hitTestObject is not working in actionscript class - actionscript-3

I am trying to test if the bullet defined in the .as file is touching the player defined in the .fla file.
I have this if statement in my .fla file
if (spaceBarPressed == true) {
var eb = new EnemyBullet(player)
stage.addChild(eb)
eb.x = enemy.x + 120
eb.y = enemy.y + 120
}
and this in my .as file
public function EnemyBullet(player) {
addEventListener(Event.ENTER_FRAME, update)
if (this.hitTestObject(player)) {
trace("hit")
}
}
function update(event:Event) {
this.x+=5
}
But I can't seem to get it to work.

I fixed it by sending the object player to the function EnemyBullet in the class, and stored it in a variable of type DisplayObject to reference it in the function update.
Contents of if Statment in .fla file:
if (spaceBarPressed == true) {
var eb = new EnemyBullet(player)
stage.addChild(eb)
eb.x = enemy.x + 120
eb.y = enemy.y + 120
}
Contents of .as file
package {
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.Event
public class EnemyBullet extends MovieClip {
var $thing:DisplayObject
public function EnemyBullet($testObject:DisplayObject) {
$thing = $testObject
addEventListener(Event.ENTER_FRAME, update)
}
function update(e:Event) {
this.x+=5
if ($thing.hitTestObject(this) ) {
trace("HIT")
}
}
}
}

Related

Flash AS3 Loadmovie Error #1009 - External AS file

We have created a program in Flash that uses an external actionscript file.
I'm trying to load/import this swf in another flash file but I'm getting a 'Error #1009: Cannot access a property or method of a null object reference'
I know this is to a reference in my external .as file but I'm not sure how to fix this
This is my code to load (and scale) the swf
var ld:Loader = new Loader();
addChild(ld);
ld.load(new URLRequest("tatton.swf"));
ld.contentLoaderInfo.addEventListener(Event.COMPLETE,function(evt) {
var mc = evt.target.content;
mc.scaleX=0.7031;
mc.scaleY=0.7031;
});
Anyone have any ideas?
Thanks
Edit: here is the full error code: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Tatton()
And below is my .as file code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
import flash.utils.clearTimeout;
import flash.utils.getDefinitionByName;
public class Tatton extends MovieClip
{
public var active_clip:MovieClip;
public var to:Number;
public function Tatton()
{
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
public function reset_timeout(e:Event = null)
{
clearTimeout(to);
// 3 mins
to = setTimeout(fire_timeout, 150 * 1000);
}
public function fire_timeout():void
{
// Reset the system (and run attractor) if we're not on the attractor already.
if ( !(active_clip is Attractor))
{
init();
}
}
public function init()
{
// Reset globals
Globals.skip_menu_anim = false;
Globals.skip_staff_menu = false;
load_movie("Attractor");
reset_timeout();
}
public function load_movie(name:String):void
{
if (active_clip is MovieClip)
{
kill_active_movie();
}
active_clip = createInstance(name);
addChild(active_clip);
active_clip.startup();
}
public function kill_active_movie():void
{
active_clip.shutdown();
removeChild(active_clip);
active_clip = null;
}
public function createInstance(className:String):Object
{
var myClass:Class = getDefinitionByName(className) as Class;
var instance:Object = new myClass();
return instance;
}
}
}
try this
public function Tatton() {
addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
}
private function stageAvailable(e:Event = null) {
removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
refer to this article to understand why
You need to add the event listener after you call Loader.load because contentLoaderInfo is null until load is called.

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.
Let me show you
So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)
So I declare my class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
function brackets and so on...
(I'm just importing everything in an attempt to avoid errors)
I then have a class I want to inherit everythingContainer and Movieclip
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
Yet when I run this I get the error:
Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Why am I getting this error?
Any help would be greatly appreciated!
I haven't got the full code to run yet so there may still be other obvious bugs laying about.
everythingContainer
Full code:
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
var speed: Number = 4;
var wpressed: Boolean = false;
var apressed: Boolean = false;
var spressed: Boolean = false;
var dpressed: Boolean = false;
var xprev:int = 0;
var yprev:int = 0;
public function everythingContainer() {
// constructor code
trace ('Container started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Container init removed');
}
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
}
public function containerEveryFrame (Event): void {
if (stage.contains(Main.player)) {
xprev = this.x;
yprev = this.y;
checkPlayerMovement();
}
}
// check the set keypress variables
public function checkPlayerMovement () : void {
if (wpressed) {
this.y -= this.speed;
}
if (spressed) {
this.y += this.speed;
}
if (apressed){
this.x -= this.speed;
}
if (dpressed) {
this.x += this.speed;
}
}
//assign key presses to variables
public function onKeyPress (event:KeyboardEvent):void {
//up
if (event.keyCode == 87){
wpressed = true;
}
//down
if (event.keyCode == 83) {
spressed = true;
}
//left
if (event.keyCode == 65){
apressed = true;
}
//right
if (event.keyCode == 68) {
dpressed = true;
}
}
//reset key press variables
public function onKeyRelease (event:KeyboardEvent) : void {
//up
if (event.keyCode == 87){
wpressed = false;
}
//down
if (event.keyCode == 83) {
spressed = false;
}
//left
if (event.keyCode == 65){
apressed = false;
}
//right
if (event.keyCode == 68) {
dpressed = false;
}
}
}
}
Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)
package code
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.Rectangle;
import flashx.textLayout.container.ContainerController;
public class Main extends MovieClip
{
//public static var main
public static var player:PC;
//public static var firstenemy: WolfEnemy;
public static var MainContainer:everythingContainer;
public function Main()
{
// constructor code
trace('main started');
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
function init(eventInfo:Event = null):void
{
if (eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Main init removed');
}
MainContainer = new everythingContainer ;
MainContainer.x = stage.stageWidth / 2;
MainContainer.y = stage.stageHeight / 2;
stage.addChild(MainContainer);
player = new PC();
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
stage.addChild(player);
//firstenemy = new WolfEnemy();
//firstenemy.x = 100;
//firstenemy.y = 100;
//stage.addChild(firstenemy);
stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
}
// check if an enemy hits the player.
/*public function enemycollison(): void {
if(firstenemy.hitTestObject(player)){
trace ('hit enemy');
player.health--;
firstenemy.kill();
}
}*/
// manage events that need to haapen globally for every frame
public function everyFrameMain(Event):void
{
/*if (stage.contains(firstenemy)){
enemycollison();
} */
//root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);
}
// finish and close game
public function endgame():void
{
}
}
}
and finally my tree class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
public function Tree1()
{
// constructor code
trace ('Tree1 started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Tree1 init removed');
}
this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
}
public function tree1EveryFrame (Event): void {
playercollision();
}
public function playercollision(): void {
if(this.hitTestObject(Main.player)){
trace ('hit wall');
Main.player.x = Main.player.xprev;
Main.player.y = Main.player.yprev;
}
}
}
}
Probably due to linkage errors inside Fla-file.
RMB on library-item: Tree1
Export for ActionScript + Export in frame 1 + "class: code.Tree1"
File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings
Added an example project in Flash CS6 found at url:
https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444
that is available for ~40 days.
Project structure in that file:
"/MyProject/DeepInheritage.fla"
"/MyProject/src/code/Foobar.as"
"/MyProject/src/code/Tree1.as"
Foobar.as which extends MovieClip
Tree1 library item which extends Foobar
That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.
code files:
package code {
import flash.display.MovieClip;
public class Foobar extends MovieClip {
public function Foobar() {
trace("foobar ctor()");
}
}
}
package code {
import flash.display.MovieClip;
public class Tree1 extends Foobar {
public function Tree1() {
trace("Tree1 ctor()");
}
}
}

AS3 Accessing classes and variables

I am new to AS3 and I try to make a simple flash game.
My problem concerns accessing a specific array outside of its class.
I succeeded accessing some variables and function but I am quite stuck on this one.
There are 3 classes : Game which is the main class tied to the flash file, Level1 which spawn background element and enemies, and finally the Enemy class.
The Game class instantiate the Level1 class which spawn enemies (with Enemy class) and push them to an array.
When the enemy get hit, a method in the Enemy class remove it from the display list and then tries to remove it from the array located in the Level1 Class, wich fails and throw :
1119: Access of possibly undefined property level1 through a reference with static type Class.
Another problem is some time the bullets stop in the middle of screen, I havn't been able to track down this bug as well.
Any way, this is my first code related post and if it's too messy, tell me and I'll try to make it more readable.
Sorry for any inconveniance and thank you for your help
-Yaniv
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.*;
import flash.geom.Point;
public class Game extends MovieClip
{
public var player:Player;
public var level1:Level1;
public var bullet:Bullet;
private var bullets_arr:Array;
var fire_on : Boolean;
var fire_counter : int;
public function Game()
{
level1=new Level1(this.stage);
player = new Player ;
addChild(player);
player.y = 600;
bullets_arr = [];
addEventListener(Event.ENTER_FRAME,Main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
function mouseDownHandler($e:MouseEvent):void
{
fire_on = true;
}
function mouseUpHandler($e:MouseEvent):void
{
fire_on = false;
fire_counter = 0;
}
function fire():void
{
bullet = new Bullet ;
addChild(bullet);
bullet.x = player.x;
bullet.y = player.y - 32;
bullets_arr.push(bullet);
}
public function Main(e: Event):void
{
player.x = mouseX;
if (bullets_arr)
{
for (var m:int = 0; m < bullets_arr.length; m++)
{
bullets_arr[m].y -= 20;
if(level1.enemies_arr)
{
for (var n:int = 0; n < level1.enemies_arr.length; n++)
{
if (bullets_arr[m])
{
if (level1.enemies_arr[n])
{
if (level1.enemies_arr[n].hitTestObject(bullets_arr[m]))
{
if(bullets_arr[m].parent)
{
bullets_arr[m].parent.removeChild(bullets_arr[m]);
bullets_arr.splice(bullets_arr[m],1);
level1.enemies_arr[n].DoDamage(10);
}
}
}
}
}
}
}
}
if(fire_on)
{
fire_counter++;
if(fire_counter == 01)
{
fire();
}
else if(fire_counter >5)
{
fire_counter =0;
}
}
}
}
}
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Level1 extends MovieClip{
var i:int;
var j:int;
var frame :int;
public var enemy:Enemy;
public var enemies_arr:Array;
public function Level1(target:Stage)
{
frame = 0;
enemies_arr = [];
for (var i:int = 0; i < 3; i++)
{
for (var j:int = 0; j < 3; j++)
{
enemy = new Enemy;
enemy.x = j*100 + 260;
enemy.y = i*40 - 150;
target.addChild(enemy);
enemies_arr.push(enemy);
}
}
}
}
}
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
var Health : int;
var splash:Splash;
function Enemy()
{
Health =30;
}
public function DoDamage(Damage:int)
{
Health -= Damage;
if (Health <= 0)
{
Die();
}
}
public function Die()
{
if(this.parent)
{
this.parent.removeChild(this);
//HERE IS THE ERROR
Game.level1.enemies_arr.splice(this,1);
}
}
}
}
The syntacitical problem you're running into is that you're trying to get level1 from the class Game, when level1 is an instance variable, not a static variable. As an instance variable, level1 is a completely different variable for each instance of game, so if you simply say Game.level1, the compiler wonders, "Which Game?"
To change this, you could simply change level1 into a static variable, by changing this:
public var level1:Level1;
to this:
public static var level1:Level1;
That way the variable would be the same across all instances, and you shouldn't have any trouble accessing it on this line:
Game.level1.enemies_arr.splice(this,1);
I will say though that there could be issues here with certain design principles (it may be that you should use callbacks or signals or something for modularity), but the quick-and-easy fix is to just add the word static to level1's declaration.
You should call level1 on the Game instance.
In a simple way, you could define the Game as Singleton
public class Game extends MovieClip {
private static var _instance:Game;
public static function getInstance():Game {
if (_instance == null) {
_instance = new Game();
}
return _instance ;
}
}
So the Die function will be like this
public function Die()
{
if(this.parent)
{
this.parent.removeChild(this);
//HERE IS THE ERROR
Game.getInstance().level1.enemies_arr.splice(this,1);
}
}

Error #1009 utilizing event listener in external class file

(New to AS3/Flash so go easy on me if I'm oblivious to something...)
Trying to utilize external class files to create a continuous scrolling background image. I got it to work by putting it in the document class file, but trying to put it in its own external class file and calling it from the document class file brings up the error in my title.
Document Class File:
package {
import flash.display.MovieClip;
import org.masteringmoneybasics.piggy._class_BG
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
new _class_BG();
}
}
}
External Class File:
package org.masteringmoneybasics.piggy {
import flash.display.*
import flash.events.Event
import flash.display.Bitmap;
import flash.display.BitmapData;
public class _class_BG{
//BG Variables
var scrollSpeed:uint = 6;
var bgLeft:Bitmap
var bgRight:Bitmap
[Embed(source="../../../assets/side_of_mountain.png")]
private var bgImage:Class;
public function _class_BG() {
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll); //<<<<<< ERROR HERE
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}
If I remove the stage. reference in the event listener, it runs without errors but the images don't appear on the stage like they are supposed to.
What am I doing wrong?
You tried to In Main class external class initialized in stage. In fact, stage not arrived. see a your _class_BG, addChild() is a little wrong. because you not check Main class, perfectly added stage.
FlashBuilder this problem must be careful. first added to stage on Main class after external class(related DisplayObject) fully loaded or initialized.
refer a following code.
In _class_BG Class Carefully addEventListener(Event.ADDED_TO_STAGE,init);
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
var sp:_class_BG = new _class_BG();
addChild(sp);
}
}
}
package {
import flash.display.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
public class _class_BG extends Sprite {
//BG Variables
private var scrollSpeed:uint = 6;
private var bgLeft:Bitmap
private var bgRight:Bitmap
[Embed(source="../asset/myTestImage.png")]
private var bgImage:Class;
public function _class_BG()
{
if(!stage)
addEventListener(Event.ADDED_TO_STAGE, init);
else
init();
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}
Only the top level Displayable has access to scene. What's more, it is read-only, which means that you cannot pass it through the original stage attribute.
The simplest way to go would be... I don't know, perhaps passing the stage to your constructor? The relavant parts of the constructor would be:
public function _class_BG(myStage : Stage) {
// SNIP
//Adds an event lsitener to the stage
myStage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
And in Main (in which you do have acces to the stage):
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
addChild(new _class_BG(stage));
}
}
You should think about some other means of building your logic, passing stage around will get hairy quickly. But it should work.
EDIT:
stage -> myStage; also, addChild in Main().

Initialize Assets after Preload

Whenever I export the .swf file of my Flash game, I am receiving "TypeError: Error #1009: Cannot access a property or method of a null object reference.", along with a Runtime Shared Library Preloading Warning for my preloader. I have my timeline organized so that the first and third frames are both empty along with a stop(); command in the Actions layer. The second frame contains a single MovieClip that contains all of my exported assets, which are going to be initialized in the third frame of the timeline. None of my assets, except for the preloader, are exported in the first frame. What changes should I make to my Document Class for it to initialize the assets in the third frame?
Document Class:
package com.gameEngine.documentClass
{
import flash.events.*;
import flash.display.*;
import flash.geom.Point;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
public class Document extends MovieClip
{
private static var _document:Document;
private var preloader:Preloader;
public var mcMain:Player;
public var restartButton:RestartButton;
public var spawnArea:SpawnArea;
public var level_1:Level_1;
public var level_2:Level_2;
public var level_3:Level_3;
public function Document()
{
addEventListener(Event.ADDED_TO_STAGE, init);
_document = this;
preloader = new Preloader(390, this.loaderInfo);
this.addChild(preloader);
preloader.addEventListener("loadComplete", loadAssets);
preloader.addEventListener("preloaderFinished", showLogo);
mcMain = new Player(this);
restartButton = new RestartButton(this);
spawnArea = new SpawnArea();
level_1 = new Level_1(this);
level_2 = new Level_2(this);
level_3 = new Level_3(this);
this.addChild(restartButton);
this.addChild(spawnArea);
this.preloader.x = 400;
this.preloader.y = 250;
restartButton.x = 822.95;
restartButton.y = 19;
spawnArea.x = 400;
spawnArea.y = 250;
trace ("Document Class Initialized");
// constructor code
}
public static function getInstance():Document
{
return _document;
}
private function loadAssets(event:Event):void
{
this.play();
}
private function showLogo(event:Event):void
{
this.removeChild(preloader);
}
public function init(event:Event)
{
if (stage.contains(spawnArea))
{
addChild(mcMain);
}
mcMain.x = spawnArea.x;
mcMain.y = spawnArea.y;
}
}
}
Preloader Class:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import flash.display.*;
import flash.events.*;
public class Preloader extends MovieClip
{
private var fullWidth:Number;
public var loaderInfo:LoaderInfo;
public function Preloader(fullWidth:Number = 0, loaderInfo:LoaderInfo = null)
{
this.fullWidth = fullWidth;
this.loaderInfo = loaderInfo;
addEventListener(Event.ENTER_FRAME, checkLoad);
}
private function checkLoad (event:Event):void
{
if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal && loaderInfo.bytesTotal != 0)
{
dispatchEvent(new Event("loadComplete"));
phaseOut();
}
updateLoader(loaderInfo.bytesLoaded / loaderInfo.bytesTotal);
}
private function updateLoader(num:Number):void
{
progressBar.width = num * fullWidth;
}
private function phaseOut():void
{
removeEventListener(Event.ENTER_FRAME, checkLoad);
progressBar.gotoAndPlay(2);
if (progressBar.currentFrame == progressBar.totalFrames)
{
phaseComplete();
}
}
private function phaseComplete() : void
{
dispatchEvent(new Event("preloaderFinished"));
}
}
}
You have a lot of race conditions going on here. Many of these events could occur at relatively random times in relation to one another . . . you have to think asynchronously. That is, there can be no assumption that any object exists. E.g., in Document.init(), you check is if the spawnArea exists, but it is almost guaranteed not to at that point, and you never check for it again.
Without making any specific changes, I can recommend a generic solution. For any object (objB) you want loaded after another object (objA) is loaded, have objB created in the objA's ADDED_TO_STAGE handler. A simple example would be:
var objA:Whatever;
var objB:WhateverElse;
[...]
objA = new Whatever();
objA.addEventListener(Event.ADDED_TO_STAGE, objAAddedHnd);
[...]
public function objAAddedHnd(event:Event)
{
// remove the event, if no longer needed:
objA.removeEventListener(Event.ADDED_TO_STAGE, objAAddedHnd);
objB = new WhateverElse();
objB.addEventListener(Event.ADDED_TO_STAGE, objBAddedHnd);
}
[...]
public function objBAddedHnd(event:Event)
{
// remove the event, if no longer needed:
objB.removeEventListener(Event.ADDED_TO_STAGE, objBAddedHnd);
// and so on . . .
}
At this point, it shows that you would need to plan the timeline of object creation.