0

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?

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about

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

3 Comments

If true is returned it then attempts to places a object in that Index slot. So I kind of need to check if it is there or not.
Tried adding your suggestion in this way at the start of the function: if (X+1 >= 10 || +1 >= 9) {return(true);} Still errors later on in the code because sometimes I still will address non-existing index.
you could simplify this even more: return (X<Grid.length) && (Y<Grid[X].length);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.