1 Reply Latest reply on Aug 19, 2013 11:15 AM by elainecc

    How to check if a variable(s) is in an array? (Pokemon Game)

    mrgrymm Level 1

      I have a sprite Ash that is animated on keydown events. Also for keydown I have a background image that moves while Ash stays in place to give the illusion of movement. Each time an arrow key is pressed a variable X or Y is increased or decresed by 1. This is to give an X,Y coordinate.


      How do I place multiple out of bound coordinates into an array and have them check against the var X and Y? If the coordinates match then the background image PC will not move but Ash will continue to walk in place (yes I want it to walk into walls just like the original game).


      Here's my project https://app.box.com/s/4w1ls5xviyte5se6feqg

        • 1. Re: How to check if a variable(s) is in an array? (Pokemon Game)
          elainecc Adobe Employee

          Hi, mygrymm-


          There are lots of inefficient ways to do this having to do with traversing arrays, etc., but probably the best thing for you to do is to create a two dimensional associative array, or an Object of Objects, in JavaScript parlance.  That way, you can look up the X in the parent object and the Y in that object to see if it exists.  Here's a really brief sample that I haven't tested, but should get you going in the right direction.


          Let's say my boundary is 4, 5:


          var xArray = new Object();

          xArray["4"] = new Object();

          xArray["4"]["5"] = true;


          Then, when I test....

          var currX = somehow.get.the.object.x.value;

          var currY = somehow.get.the.object.y.value;

          if (xArray[currX.toString()][currY.toString()] == true) {

             console.log("boundary hit!");



          Hope that helps,



          1 person found this helpful