Search code examples
actionscript-3if-statementflash-cs4

ActionScript 3 if condition error


I have the following code in my game:

var type:Number = Math.random() * 1000;
var lifeCoinLevel:Number = 1;
var lives:Number = 2;
var maxLives:Number = 3;
var coins:Array = new Array();

if(lifeCoinLevel > 0 && lives < maxLives && (type >= 255 && type <= 345)) {
    coins[1] = new LifeCoin();
}

coins[1].x = Math.random() * 350 + 145;

I got this error when run the game:

TypeError: Error #1010: A term is undefined and has no properties.
at MoneyCatcher_fla::MainTimeline/CreateNewCoin()
at Function/http ://adobe.com/AS3/2006/builtin::apply()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

It seems like the if condition not met, but I don't know why. Any thoughts?


Solution

  • I think that you should do like this :

    ...
    
    if(lifeCoinLevel > 0 && lives < maxLives && (type >= 255 && type <= 345)) {
        coins[1] = new LifeCoin();
        coins[1].x = Math.random() * 350 + 145;
    }
    

    because if the condition is not true, the coins[1] is undefined and then you get an error.