-1

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);
}
Lev
  • 5
  • 4

1 Answers1

1

Wow, you got this capitalization of the first letter completely backwards.

Your instances/variables should be with lowercase. Your class names should be with upper case.

So no

var Coin:coin = new coin();

But

var coin:Coin = new Coin();

When you do this if(Coin.hitTestObject(char)) you are basically looking for a method on your Array instance called hitTestObject. The array doesn't have this method. You need to specify an index for it(Coin[1].hitTestObject(...)); so it uses the coin instances (and you should do it in a for-cycle, for every coin). Now I can only assume your 'mario' and 'coin' classes extend some display object so they actually have the hitTestObject method.

Fygo
  • 4,555
  • 6
  • 33
  • 47