0

I am trying to create a matching game where one object in the array hitBoxes is matched to one object in the array hitBoxes2. I have tried to convert the instance name into a string and then used the substring method to match the LAST number in the instance name, if its a match they win. Right now I'm getting the error

TypeError: Error #1009: Cannot access a property or method of a null object reference. at MethodInfo-499()

I'm wondering if anyone can help me. Thanks!

            var left:String;
            var correct:MovieClip = new Correct;
            var isClicked:Boolean = false;
            var leftClicked:int = 0;

            p3.nextPage.buttonMode = true;
            p3.nextPage.addEventListener(MouseEvent.CLICK, nextPage);

            function nextPage(MouseEvent):void{
                removeChild(p3);
            }

            var hitBoxes:Array = [p3.a1, p3.a2, p3.a3, p3.a4, p3.a5, p3.a6, p3.a7, p3.a8];
            var hitBoxes2:Array = [p3.b1, p3.b2, p3.b3, p3.b4, p3.b5, p3.b6, p3.b7, p3.b8];


            for (var h:int = 0; h < hitBoxes.length; h++){
                hitBoxes[h].buttonMode = true;
                hitBoxes[h].addEventListener(MouseEvent.CLICK, matchingLeft);
            }

            for (var h2:int = 0; h2 < hitBoxes2.length; h2++){
                hitBoxes2[h2].buttonMode = true;
                hitBoxes2[h2].addEventListener(MouseEvent.CLICK, matchingRight);
            }

            function matchingLeft(e:MouseEvent):void{
                var left = String(e.currentTarget.name);
                isClicked = true;
                trace(left);
            }

            function matchingRight(e:MouseEvent):void{
                var right:String = String(e.currentTarget.name);
                trace(right);
                if(isClicked == true && left.substring(3,3) == right.substring(3,3)){
                    trace("matched");
                }

            }

1 Answer 1

2

According to your code variable "left" is null at matchingRight method, because matchingLeft uses its local variable with name "left", and top-level "left" still has its default value.

also String.substring method is used incorrectly:

var name:String="p3.a1";
trace(name.substring(3, 3)); // this will always output empty string ""
trace(name.substring(4, 5)); // this will output "1" string

in conclusion I'd advise to use array indices (integers) instead of strings when calculating "matched" condition, substring operation and string comparison are CPU intensive.

Sign up to request clarification or add additional context in comments.

Comments

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.