PDA

View Full Version : [AS3] What is the equivalent to _root in AS3?


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);
}
}

}

Ciubhran
02-15-2009, 05:56 PM
Seems to me like the two objects are for some reason on different "stages" or something, since they can't find eachother.

EightySeven
02-16-2009, 02:04 AM
what is ur file set up? is enemy part of a another movie clip that is on the stage? is "this" on the stage or another movieclip?

Ciubhran
02-16-2009, 02:29 AM
what is ur file set up? is enemy part of a another movie clip that is on the stage? is "this" on the stage or another movieclip?

All my files lay in the same folder.

Enemy is not part of any MovieClip, it is a simple class that only has a constructor that changes its coordinates. And it is displayed on the screen as a pink block.

"this" in the example below is in the Move-function of the Projectile class (Move propagates the projectile across the screen, so it must also have the collision detection function).

EightySeven
02-16-2009, 01:05 PM
well you shouldn't have the collision in your bullet class, i believe collision should be done in the main class. and used as if (projectile[i].hitTestObject(enemy[enemyID])){
do some collision stuff;
}

but in your case youre getting errors because it thinks your tryign to access enemy from within the projectile movieclip, where it doesn't exist

you might try

this.hitTestObject(MovieClip(parent).enemy)
or
this.hitTestObject(parent.enemy)

you have to move up a directory to find the enemy movieclip since it doesn't exits inside the this movieclip

bluemagica
02-16-2009, 02:12 PM
Umm, you are adding the enemy manually? did you give it an instance name? cause the Linkage/class name is used for defining the object, not manipulating an instance of it!

Also instead of datatyping stg to Stage, datatype it to MovieClip, cause the functions you are trying to access belong to a movieclip (in as3/cs3 stage itself is a movieclip, and each sub-movieclip behaves as a "stage" under its parent!)

Ciubhran
02-16-2009, 02:17 PM
I have added it manually (and given it an instance name) and automatically, but it still gives the same error, that it cant find the "enemy".

I am gonna try to change Stg to a MovieClip and see what happens.

Ciubhran
02-16-2009, 02:24 PM
well you shouldn't have the collision in your bullet class, i believe collision should be done in the main class. and used as if (projectile[i].hitTestObject(enemy[enemyID])){
do some collision stuff;
}

but in your case youre getting errors because it thinks your tryign to access enemy from within the projectile movieclip, where it doesn't exist

you might try

this.hitTestObject(MovieClip(parent).enemy)
or
this.hitTestObject(parent.enemy)

you have to move up a directory to find the enemy movieclip since it doesn't exits inside the this movieclip

But this.Stg is a reference to the main stage, which is where 'enemy' should be.

Ciubhran
02-16-2009, 02:48 PM
Umm, you are adding the enemy manually? did you give it an instance name? cause the Linkage/class name is used for defining the object, not manipulating an instance of it!

Also instead of datatyping stg to Stage, datatype it to MovieClip, cause the functions you are trying to access belong to a movieclip (in as3/cs3 stage itself is a movieclip, and each sub-movieclip behaves as a "stage" under its parent!)

I get an error when trying to store "stage" in a MovieClip variable:

Type Coercion failed: cannot convert flash.display::Stage@2e86b51 to flash.display.MovieClip.

I think I have some flaws in my design. So if anyone that understand what I am trying to do, wants to help out, please share on how you would solve the design issue behind all this.

I have no "main class" right now, I just have a few classes. Some that are directly applied to the stage, and the other classes are dependant on the prior class. I haven't made a "main class" myself, and am unsure on how I would go about making one.

bluemagica
02-16-2009, 04:46 PM
show your file

Ciubhran
02-17-2009, 10:22 AM
Link to the file:

http://www.2shared.com/file/4896869/3a1aea6e/src.html

At the bottom of the page there is a small line of text that says "Save file to your PC", click the link to the right of it to download.

I've included my entire project. The version I have uploaded is stable, without any errors, because I removed all the collision detection attempts.

bluemagica
02-17-2009, 04:22 PM
sorry i couldn't open your fla, maybe it got corrupted!

Ciubhran
02-17-2009, 04:42 PM
http://www.sendspace.com/file/7vasbd

New link.

I tried opening the .fla and it worked fine for me.

The .fla is created with Adobe Flash CS4 Professional on a PC with Windows Vista installed.

bluemagica
02-18-2009, 04:01 AM
Sorry then, i don't have cs4, you have to export it for cs3!