• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

remove array when touch a movieClip (hitTestObject)

Contributor ,
Jul 21, 2014 Jul 21, 2014

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

TOPICS
ActionScript

Views

799

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Jul 23, 2014 Jul 23, 2014

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

  } 

Votes

Translate

Translate
LEGEND ,
Jul 21, 2014 Jul 21, 2014

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 21, 2014 Jul 21, 2014

Copy link to clipboard

Copied

Ok, but I do so I've got this error : error 1120: Access of undefined property

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2014 Jul 21, 2014

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 21, 2014 Jul 21, 2014

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 23, 2014 Jul 23, 2014

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 21, 2014 Jul 21, 2014

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2014 Jul 21, 2014

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 21, 2014 Jul 21, 2014

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; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2014 Jul 22, 2014

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 22, 2014 Jul 22, 2014

Copy link to clipboard

Copied

All right.

So, I've got this movieClip (the black rectangle) on my stage with the instance tree_mc

1.png

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2014 Jul 23, 2014

Copy link to clipboard

Copied

Show where you have assigned the Main.as file as your document class file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2014 Jul 23, 2014

Copy link to clipboard

Copied

2.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2014 Jul 23, 2014

Copy link to clipboard

Copied

LATEST

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

  } 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines