Copy link to clipboard
Copied
Hi all,
I'm making an AS3 platform game.
My player can shoot bullets. If a bullet touch an enemy, it will be removed.
for (var j:int = 0; j < bulletList.length; j++) // for each bullet in the bulletList
{
if (enemies.mc.hitTestObject(bulletList
) ){ trace("Bullet and Enemy are colliding");
enemyDie(i)
bulletList
.removeSelf(); }
But now I'd like to remove the bullet if it touch a movieClip on the stage (with the instance "tree_mc"). And I can't figure out how to do so...
I've tried this :
var tree_mc;
for (var j:int = 0; j < bulletList.length; j++){
if(tree_mc.hitTestObject(bulletList
)){ trace("bullet hit tree");
}
But it's not working.
Do you know how I can do that ?
Thank you
Found the problem !
Here what I did :
tree = new Object();
tree.mc = gamelevel.tree;
for (var j:int = 0; j < bulletList.length; j++){
if(tree.mc.hitTestObject(bulletList
trace("bullet hit tree");
}
Copy link to clipboard
Copied
Do not declare the tree_mc variable (get rid of line 01). Creating a new variable is not gong to be the same tree_mc.
Copy link to clipboard
Copied
Ok, but I do so I've got this error : error 1120: Access of undefined property
Copy link to clipboard
Copied
Ok, don't. If you mean you removed that line then the problem is likely that you did not name the tree_mc that name, or it is otherwise not present when the code executes. What is the entire error message?
Copy link to clipboard
Copied
I could put
private var tree_mc:MovieClip;
at the begining of my class, but it stills make an error : Error #1009: Cannot access a property or method of a null object reference.
(it for the line if (tree_mc.hitTestObject(bulletList
Do you know why ?
Copy link to clipboard
Copied
Stephdidou wrote:
I could put
private var tree_mc:MovieClip;
If you scope your variable as private, Flash doesn't have access to put the MovieClip in there when it makes it.Make it public, and remove the untyped re-declaration of tree_mc in your function.
Copy link to clipboard
Copied
I've named the instance of the movieClip "tree_mc". (but I' haven't made a linkage for the MovieClip)
Copy link to clipboard
Copied
If the class file is not your document class file then you need to provide the class with a reference to the tree_mc object from wherever it exists. Where is the tree created?
Copy link to clipboard
Copied
The class file is "Main.as". I've only got this class for my project.
I've drag the MovieClip "tree" on the stage and put the instance name "tree_mc".
Then I've put in my Main.as : private var tree_mc:MovieClip;
Copy link to clipboard
Copied
You do not want to declare a new tree_mc object if you already have one on the stage. You need to be sure to declare the class file as your document class, otherwise anything you place on the stage manually will not be seen by the class.
Copy link to clipboard
Copied
All right.
So, I've got this movieClip (the black rectangle) on my stage with the instance tree_mc
Then in my Main.as I've put :
public function checkCollisions() {
for (var j:int = 0; j < bulletList.length; j++){
if(tree_mc.hitTestObject(bulletList
)){ trace("bullet hit tree");
}
}
But then I've got Error 1120 Access of undefined property tree_mc
So I'm adding the variable "tree_mc" :
public function checkCollisions() {
var tree_mc;
for (var j:int = 0; j < bulletList.length; j++){
if(tree_mc.hitTestObject(bulletList
)){ trace("bullet hit tree");
}
}
The game is launching without error.
But when the function checkCollisions is called I've got this error :
Error #1010: A term is undefined and has no properties. (for the line 4)
Do you know what could be the problem ?
Copy link to clipboard
Copied
Show where you have assigned the Main.as file as your document class file.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Found the problem !
Here what I did :
tree = new Object();
tree.mc = gamelevel.tree;
for (var j:int = 0; j < bulletList.length; j++){
if(tree.mc.hitTestObject(bulletList
trace("bullet hit tree");
}