Ciubhran
02-15-2009, 05:36 PM
Just wanted to make sure of something...
In AS2 you added MCs to _root. What is the equivalent to this in AS3?
I thought it was the "stage" variable, but I am no longer sure.
Tried reading up about it but people keep telling me different things.
All I want is to add a few MCs to a common, main displaying entity without having any problems.
Right now I have used stage, and it works "ok", until I am trying to do collision detection.
I send a reference of the stage property to the constructor of a new MC, so I can add things to the "stage". The object (the new MC) that I just created by pressing a key (during run-time), will move around the screen. This MC is supposed to eventually collide with another MC on the screen, that I have named 'enemy', but once the moving object is created, this condition fails...
if(this.hitTestObject(Stg.enemy)) {
trace("blah");
}
... and I get an error saying there is no propery called "enemy" on the stage. But I have added the "enemy" MC in the main .fla-file, onto the "stage" manually, so it isn't created on run-time.
Here is the error:
Property enemy not found on flash.display.Stage and there is no default value.
Here is my Enemy class:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Enemy extends MovieClip {
public function Enemy() {
this.x = 50;
this.y = 50;
}
}
}
And the Projectile that runs across the screen:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Projectile extends MovieClip {
private var LifeTime;
private var Stg;
private var Speed;
public function Projectile(x_:Number, y_:Number, rot:Number, duration:Number, spd:Number, stg:Stage) {
this.rotation = rot;
this.Stg = stg;
this.x = x_;
this.y = y_;
this.LifeTime = duration;
this.Speed = spd;
Stg.addChild(this);
addEventListener(Event.ENTER_FRAME, Move);
}
public function Move(e:Event) {
if(this.LifeTime <= 0) removeProjectile();
this.x += Speed * Math.cos((this.rotation - 90) * (Math.PI / 180));
this.y += Speed * Math.sin((this.rotation - 90) * (Math.PI / 180));
if( this.hitTestObject(Stg.enemy) ) {
trace("hit!");
}
this.LifeTime--;
}
public function removeProjectile() {
removeEventListener(Event.ENTER_FRAME, Move);
Stg.removeChild(this);
}
}
}
In AS2 you added MCs to _root. What is the equivalent to this in AS3?
I thought it was the "stage" variable, but I am no longer sure.
Tried reading up about it but people keep telling me different things.
All I want is to add a few MCs to a common, main displaying entity without having any problems.
Right now I have used stage, and it works "ok", until I am trying to do collision detection.
I send a reference of the stage property to the constructor of a new MC, so I can add things to the "stage". The object (the new MC) that I just created by pressing a key (during run-time), will move around the screen. This MC is supposed to eventually collide with another MC on the screen, that I have named 'enemy', but once the moving object is created, this condition fails...
if(this.hitTestObject(Stg.enemy)) {
trace("blah");
}
... and I get an error saying there is no propery called "enemy" on the stage. But I have added the "enemy" MC in the main .fla-file, onto the "stage" manually, so it isn't created on run-time.
Here is the error:
Property enemy not found on flash.display.Stage and there is no default value.
Here is my Enemy class:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Enemy extends MovieClip {
public function Enemy() {
this.x = 50;
this.y = 50;
}
}
}
And the Projectile that runs across the screen:
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Projectile extends MovieClip {
private var LifeTime;
private var Stg;
private var Speed;
public function Projectile(x_:Number, y_:Number, rot:Number, duration:Number, spd:Number, stg:Stage) {
this.rotation = rot;
this.Stg = stg;
this.x = x_;
this.y = y_;
this.LifeTime = duration;
this.Speed = spd;
Stg.addChild(this);
addEventListener(Event.ENTER_FRAME, Move);
}
public function Move(e:Event) {
if(this.LifeTime <= 0) removeProjectile();
this.x += Speed * Math.cos((this.rotation - 90) * (Math.PI / 180));
this.y += Speed * Math.sin((this.rotation - 90) * (Math.PI / 180));
if( this.hitTestObject(Stg.enemy) ) {
trace("hit!");
}
this.LifeTime--;
}
public function removeProjectile() {
removeEventListener(Event.ENTER_FRAME, Move);
Stg.removeChild(this);
}
}
}