Copy link to clipboard
Copied
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);
}
}
}
}
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 listen
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Here's the problem!!
var distanceX:Number = MovieClip(parent).cat_mc.x - this.x;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
so for the parent it gave me back this:
[object MovieClip]
and for the MovieClip(parent).cat_mc it gave me this:
undefined
I'm not sure why it's returning an undefined value I have a movie clip on stage in the main timeline with the instance name cat_mc
Copy link to clipboard
Copied
undifined men that MovieClip(parent) don't contain cat_mc so you see this as Error 1010
Before you addChild(itemBehavior) be sure that you added cat_mc.
Your timeline with frames animation? If yes - check every keyframe. You must be sure that all keyframes contain name of that MovieClip.
Popular mistake it's populating MovieClip to the timeline in frame 2 but calling it in frame1. If you post me your FLA I can check it.
Copy link to clipboard
Copied
Okay so here's an update: turns out I had a small syntax error with the addChild of the items connected to this class which led to this error. I am however getting another error now. This is the code below and the error is:
"TypeError: Error #1009: Cannot access a property or method of a null object reference.
at itemBehavior/avoidCat()[/Users/Shorouq/Documents/2012-2013/Spring '13/ITGM719_Scripting/ex6/itemBehavior.as:44]"
package
{
import flash.display.*;
import flash.utils.*;
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 = Math.round(Math.random() * 5);
//create the bugs by dynamically attaching them to the skaterHolder 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();
//give each one a speed
newCatItem.speedFactor = Math.random() * 8 + 14;
//increment depth for the next bug
addChild(newCatItem);
}
}
}
}
}
}
When I debug I still go to the same line as before. You mentioned that before I addChild I needed to make sure I added cat_mc, could you explain that a little more? Also, there is no timeline animation or any kind of code on the main fla file except for a background shape and the cat_mc movieclip.
Copy link to clipboard
Copied
Man
Put here updated itemBehavior also. There error on line 44 and I don't see where exactly.
Copy link to clipboard
Copied
Oops didn't realize I put up the wrong file! Thanks
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Rectangle;
public class itemBehavior extends MovieClip
{
var aRandomNumber:Number = randomNumber(1,2);
//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);
}
}
}
}
Copy link to clipboard
Copied
Damn )
There somewhere small stupid thing that don't allow to compile.
Go to yout movieclip and check if you named it cat_mc
Or better find mt email on my profile and send me fla sources. Will help you right now
Copy link to clipboard
Copied
I checked it is indeed cat_mc haha and I'm sorry but there is no email on your profile?
Copy link to clipboard
Copied
)))
get it from this image
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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);
}
}
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
That allowed the reset button to work, but it messes up both the avoidMe function, as well as the checkCollision function in another file. it is giving me this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at itemBehavior/avoidCat()[/Users/Shorouq/Documents/2012-2013/Spring '13/ITGM719_Scripting/ex6/itemBehavior.as:44]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at itemBehavior/checkCollision()[/Users/Shorouq/Documents/2012-2013/Spring '13/ITGM719_Scripting/ex6/itemBehavior.as:84]
And these two lines are the following:
var distanceX:Number = this.x - MovieClip(parent).cat_mc.x; //and
if (MovieClip(parent).cat_mc.hitTestObject(this))//respectively.
I have had problems with these lines for the longest time and each time i fix them they come up again somewhere else!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now