Skip to main content
Known Participant
May 4, 2013
Answered

Error1010: A term is undefined and has no properties

  • May 4, 2013
  • 3 replies
  • 2749 views

This error has been driving me crazy! The debug won't tell me what term I'm having trouble with. Below is the code, it is a class that defines the movement and behavior of these items I'm creating on stage. I'm getting the error twice, once for the avoidMe function and the other for the checkCollision function. AvoidMe makes these items move away from a movieclip the user controls on stage, and the checkCollision function checks if the movieclip hits any of these items and removes them off the stage. Help me!

package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.geom.Rectangle;

          public class itemBehavior extends MovieClip

          {

                    var aRandomNumber:Number = randomNumber(1,3);

                    //Rectangle boundaries

                    var xLocation:Number = 0;

                    var yLocation:Number = 0;

                    var widthOfRect:Number = 450;

                    var heightOfRect:Number = 450;

                    var rectBoundary:Rectangle = new Rectangle(xLocation,yLocation,widthOfRect,heightOfRect);

                    var itemSpeed:Number = 5;

                    var minDistanceBetweenItems:Number = 20;

                    public function itemBehavior():void

                    {

                              //Have each item have a random position on the stage

                              this.x = Math.round(Math.random() * 500) + 20;

                              this.y = Math.round(Math.random() * 500) + 20;

                              //Have each item have a different size

                              this.scaleX = scaleX * aRandomNumber;

                              this.scaleY = scaleY * aRandomNumber;

                              //Add listener to move items away from cat

                              addEventListener(Event.ENTER_FRAME,avoidCat);

                              //Add listener to remove items from stage when they hit the cat

                              addEventListener(Event.ENTER_FRAME,checkCollision);

                    }

                    function randomNumber(min:Number, max:Number):Number

                    {

                              return Math.floor(Math.random() * (1 + max - min) + min);

                    }

                    function avoidCat(event:Event):void

                    {

                              // Calculate the distance between the cat and the items

                              var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;

                              var distanceY:Number = MovieClip(parent).cat_mc.y - this.y;

                              var distanceXY:Number = Math.sqrt(distanceX*distanceX+distanceY*distanceY);

                              // Check if distance is small enough

                              if (distanceXY <= minDistanceBetweenItems)

                              {

                                        // Calculate direction between cat and the items

                                        var currDirection:Number = Math.atan2(distanceY,distanceX);

                                        // Move items to opposite direction

                                        this.x -=  itemSpeed * Math.cos(currDirection);

                                        this.y -=  itemSpeed * Math.sin(currDirection);

                              }

                              //Restrict items in rectangle boundaries

                              if (this.x < xLocation)

                              {

                                        this.x = widthOfRect;

                              }

                              if (this.x > widthOfRect)

                              {

                                        this.x = xLocation;

                              }

                              if (this.y < yLocation)

                              {

                                        this.y = heightOfRect;

                              }

                              if (this.y > heightOfRect)

                              {

                                        this.y = yLocation;

                              }

                    }

                    //Function to check for collision and remove item

                    function checkCollision(event:Event):void

                    {

                              if (MovieClip(parent).cat_mc.hitTestObject(this))

                              {

                                        parent.removeChild(this);

                                        removeEventListener(Event.ENTER_FRAME,checkCollision);

                              }

                    }

          }

}

This topic has been closed for replies.
Correct answer User Unknow

Ok, this realy simple bug on your side.

Take look here:

if (MovieClip(parent).cat_mc.hitTestObject(this))
      {
          parent.removeChild(this);
          removeEventListener(Event.ENTER_FRAME,checkCollision);
       }

You remove self object and than remove listener ENTER_FRAME. But you made 2 mistake. First - you remove only checkCollision but you have also avoidCat. And second - you must firstly remove all listeners and only then remove object. On your code right now you remove object but listener in the memory try to execute function checkCollision and avoidCat. And it executed but can't find object.

Here is solution:

1) remove listeners

2) remove object

Simply make code as below and you will fix all issues.

if (MovieClip(parent).cat_mc.hitTestObject(this))

            {

                removeEventListener(Event.ENTER_FRAME,checkCollision);

                removeEventListener(Event.ENTER_FRAME,avoidCat);

                parent.removeChild(this);

            }

3 replies

User Unknow
User UnknowCorrect answer
Legend
May 5, 2013

Ok, this realy simple bug on your side.

Take look here:

if (MovieClip(parent).cat_mc.hitTestObject(this))
      {
          parent.removeChild(this);
          removeEventListener(Event.ENTER_FRAME,checkCollision);
       }

You remove self object and than remove listener ENTER_FRAME. But you made 2 mistake. First - you remove only checkCollision but you have also avoidCat. And second - you must firstly remove all listeners and only then remove object. On your code right now you remove object but listener in the memory try to execute function checkCollision and avoidCat. And it executed but can't find object.

Here is solution:

1) remove listeners

2) remove object

Simply make code as below and you will fix all issues.

if (MovieClip(parent).cat_mc.hitTestObject(this))

            {

                removeEventListener(Event.ENTER_FRAME,checkCollision);

                removeEventListener(Event.ENTER_FRAME,avoidCat);

                parent.removeChild(this);

            }

sghneim20Author
Known Participant
May 5, 2013

Hey! I really appreciate your help throughout! I've gotten everything to work almost but I'm having one small problem. I've added a button on the stage, reset_btn, and it's supposed to remove the child of all the items, followed by adding a new on, sort of like resetting things. Here's the code I have, and it is adding new items on the stage, however it's not removing the old ones when I reset!

package

{

          import flash.display.*;

          import flash.utils.*;

          import flash.events.*;

          public class GhneimsEx6 extends MovieClip

          {

                    public function GhneimsEx6()

                    {

                              //Make a holder for the items

                              var catItemsHolder = new MovieClip();

                              addChild(catItemsHolder);

                              //Create the items inside the holder

                              var catItems:Array = ["Fish","Heart","Medicine","Yarn"];

                              makeItems(catItems);

                              var ClassReference:Class;

                              function makeItems(catItems)

                              {

                                        //Iterate through each item in the list

                                        for (var prop in catItems)

                                        {

                                                  //Create a random number of items from each

                                                  var itemsNum:Number = 5 + Math.round(Math.random() * 5);

                                                  //create the items by dynamically attaching them to the holder Movie Clip

                                                  for (var i=0; i<itemsNum; i++)

                                                  {

                                                            //this is needed to convert the string into a class reference so that we can

                                                            //create the objects from the library.

                                                            ClassReference = getDefinitionByName(catItems[prop]) as Class;

                                                            var newCatItem = new ClassReference();

                                                            //increment depth for the next item

                                                            addChild(newCatItem);

                                                  }

                                        }

                              //reset button to reset the items on the stage

                              reset_btn.addEventListener(MouseEvent.CLICK,resetItems);

                              }

 

                              function resetItems(myEvent:MouseEvent)

                              {

                                        removeChild(catItemsHolder);

                                        catItemsHolder = new MovieClip();

                                        addChild(catItemsHolder);

                                        makeItems(catItems);

                              }

                    }

          }

}

User Unknow
Legend
May 5, 2013

You adding here:

//increment depth for the next item

addChild(newCatItem);

to the base but removing from catItemsHolder )

Make this:

private var catItemsHolder:MovieClip;

public function GhneimsEx6()

     {

   

     //Make a holder for the items

     catItemsHolder = new MovieClip();

     addChild(catItemsHolder);

when you adding:

     //increment depth for the next item

     catItemsHolder.addChild(newCatItem);

this may help

User Unknow
Legend
May 4, 2013

public function itemBehavior():void {

     addEventListener(Event.ADDED_TO_STAGE, onInit);

}

private function onInit(e:Event):void{

     removeEventListener(Event.ADDED_TO_STAGE, onInit);

    

     executeApp();    

}

private function executeApp():void{
//Have each item have a random position on the stage
                              this.x = Math.round(Math.random() * 500) + 20;
                              this.y = Math.round(Math.random() * 500) + 20;


                              //Have each item have a different size
                              this.scaleX = scaleX * aRandomNumber;
                              this.scaleY = scaleY * aRandomNumber;


                              //Add listener to move items away from cat
                              addEventListener(Event.ENTER_FRAME,avoidCat);


                              //Add listener to remove items from stage when they hit the cat
                              addEventListener(Event.ENTER_FRAME,checkCollision);

}

sghneim20Author
Known Participant
May 5, 2013

Hey! Thanks for helping.. It still gives me the same error though.. and I checked permit debugging and managed to know which line the code is stuck on:

var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;

I'm almost a 100% sure it's the MovieClip(parent).cat syntax that I'm using.. I want to access the movieclip cat_mc from the parent outside this class

User Unknow
Legend
May 5, 2013

Where you adding itemBehavior class? Show previus class. I can't know it's base class.

itemBehavior it's MovieClip. But you place it somewhere ) show that code from where you call
itemBehavior

kglad
Community Expert
Community Expert
May 4, 2013

click file>publish settings>tick "permit debugging".  retest.

the problematic line number will be  in the error message.

p.s.  you may need to use an Event.ADDED_TO_STAGE event before accessing any code that references the parent.  ie, before adding your enterframe listeners.

sghneim20Author
Known Participant
May 5, 2013

Here's the problem!!

var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;

User Unknow
Legend
May 5, 2013

Problem is not there

parent - it's DisplayObject.

When you type MovieClip(parent) - this mean you try to access parent of your itemBehavior. That parent it's MovieClip? You typed yes.

That MovieClip contain another display object - cat_mc. Does it alive?

type there:

trace ( parent );

trace (MovieClip(parent).cat_mc);

var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;

and show me that trace will write to you.