Jump to content

[Action Script 3] hitTestObject is not a function

levsingh
Go to solution Solved by madknight3,

Instead of

var money:coin = new coin();var Coin:Array = new Array(money,money,money,money,money);

Try

var Coin:Array = new Array(new coin(),new coin(),new coin(),new coin(),new coin());

To make sure the problem isn't because they all reference the same coin object.

 

edit: Also, just noticed that you mentioned the "hitTestObject is not a function" error. I'm not really sure, but according to the documentation of hitTestObject it requires a DisplayObject type as a parameter. Does your class mario implement DisplayObject? Also, are you sure you can use it on an Array, or do you have to use it on items in the array?

hey guys just trying to make simple game in flash where this character runs and collects coins and then the coin count increases. this was working fine when i was dealing with just one coin and then i tried to do it with arrays and this error.


is there any other way to do it? i am new to as3 just doing it for 2 weeks. thanks



import flash.events.KeyboardEvent;

var char:mario = new mario();

addChild(char);

char.x = 300;

char.y = 720;

var money:coin = new coin();

var Coin:Array = new Array(money,money,money,money,money);

addChild(Coin[2]);

trace(Coin[2]);

for(var b:int = 0; b<5; b++)

{
addChild(Coin[b]);
Coin[b].x = 300;
Coin[b].y = 100*b;

}

stage.addEventListener(KeyboardEvent.KEY_DOWN,movement);


var a:int;
function movement(e:KeyboardEvent)
{
if(e.keyCode == 38)
{
char.y -= 5;
}

if(e.keyCode == 40)
{
char.y += 5;
}

if(e.keyCode == 37)
{
char.x -= 5;
}

if(e.keyCode == 39)
{
char.x += 5;
}

if(Coin.hitTestObject(char))
{

Coin[b].y = -5000;
a++;

}
trace("coins= " + a);
}

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of

var money:coin = new coin();var Coin:Array = new Array(money,money,money,money,money);

Try

var Coin:Array = new Array(new coin(),new coin(),new coin(),new coin(),new coin());

To make sure the problem isn't because they all reference the same coin object.

 

edit: Also, just noticed that you mentioned the "hitTestObject is not a function" error. I'm not really sure, but according to the documentation of hitTestObject it requires a DisplayObject type as a parameter. Does your class mario implement DisplayObject? Also, are you sure you can use it on an Array, or do you have to use it on items in the array?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×