7 Replies Latest reply on Apr 19, 2015 4:29 PM by Ned Murphy

    Referring to MovieClips using an Array

    Araly

      Hello,

      I'm currently working on making a game, but I'm having trouble with switching the movie clip based on which key was pressed. I just started the code so I'm only on the basics at the moment, and keep in mind that I am new to Flash so I apologize for any stupid mistakes.

       

      What I am trying to do is get the character (Shu) to move using WASD, however depending on which direction he goes in, I want the movie clip to change into a different picture of Shu. I'm using three different functions for this:

      -moveShu (increases Shu's x & y coords to make him move)

      //Move Shu (SF)

        function moveShu(): void {

        if (dir == "up" && SF.y > 0) {

        SF.y -= 3;

        }

        if (dir == "left" && SF.x > 0) {

        SF.x -= 3;

        }

        if (dir == "down" && SF.y < 400) {

        SF.y += 3;

        }

        if (dir == "right" && SF.x < 550) {

        SF.x += 3;

        }

        }

      -dFun (just gives a string of which direction key was pressed, e.g "left", "up")

      -uFun (stops the movement when keys are no longer being pressed)

       

      My problem is that if I'm switching the movie clip (and therefore will have a different variable name instead of SF), the moveShu function will no longer work. I considered just removing SF and replacing him with the movie clip I want, then increasing that clip's x or y coords, but then I realized the && SF.y won't work for the new movie clip.

      if (dir == "down" && SF.y < 400) {

        SF.y += 3;

        }

      I can't get rid of the SF.y<400 coords since it prevents Shu (or the movie clip I replace him with) from just running off stage, so what I'm thinking now is that I need an array where I can input all the movie clips I want to use, and do:

      if (dir == "down" && shuArray[i].y < 400) {

        shuArray[i].y += 3;

        }

      I really don't know how to refer an array's objects without stating the exact one that I want, but I used "i" as a number in previous codes I was taught in class which is why I think arrays might be my answer.

       

      Again, I'm sorry for anything obvious I overlooked, but what I need is a way to refer to an array's objects in general without having state which exact one. One last thing to note is that there will only ever be one "Shu" or character on the stage at a time, so it's not like I'll leave all the movie clips on the stage at once.

       

      Thanks for any help! ^^