L.P.
L.P.

Reputation: 83

Undefined index in Array

I have my grid-array in my flash game that has a set sized index in it, and through some code it sometimes checks outside of the Array index. Is there a way I can check to see if a index slot is undefined/existent

Gives me a TypeError #1010

public function fnPopulate(X:int, Y:int, Grid:Array){
    if (Grid[ X + 1 ][ Y + 1 ] != null || Grid[ X + 1 ][ Y + 1 ] != undefined ) {
    return(true);
    } 

    return(false);
}

Any advise is appreciated, Thank you in advance.

P.S. Is there a reason why it keeps deleting my greeting?

Upvotes: 0

Views: 182

Answers (2)

liupeixin
liupeixin

Reputation: 738

you need to check the first dimensional of array.
and obj!= null, obj!= undefined means if(obj){....}

public function fnPopulate(X:int, Y:int, Grid:Array)
{
    if (Grid[ X + 1 ] && Grid[ X + 1 ][ Y + 1 ]) 
    {
        return(true);
    } 

    return(false);
}

Upvotes: 1

wharfie
wharfie

Reputation: 11

How about

if ( (X<Grid.length) && (Y<Grid[X].length) ) return(true) else return(false);

Upvotes: 1

Related Questions