21 Replies Latest reply on Mar 13, 2014 6:56 AM by kglad

    Side scroller game AS2

    zoner9

      I'm making a side scrolling shooter game for a school project and i'm having some difficulties making a movie clip move. I named the movie clip 'lasers' and when the space key is pressed, they're supposed to enter the clip and move from left to right.

       

      At the moment, I can make the movie clip appear in the place that i want it to, but they don't move - I'm not sure what im doing wrong

       

      this is the script i have in place to create the lasers:

       

      if(Key.isDown(Key.SPACE))

        {

         var lasers = _root.attachMovie("lasers", "lasers" +_root.getNextHighestDepth(), _root.getNextHighestDepth());

         lasers._x = _x + 10;

         lasers._y = _y - 20;

         _root.shooting = true;

        }

       

      Within the lasers movie clip, I defined _root.shooting = true as follows (I declared speed as a variable within an onClipEvent handler):

       

      if(_root.shooting = true)

        {

         _x = _x + speed;

        }

       

      What can i do to fix this?

        • 1. Re: Side scroller game AS2
          kglad Adobe Community Professional & MVP

          this code

           

          if(_root.shooting = true)

            {

             _x = _x + speed;

            }

           

          needs to be in a loop (like onEnterFrame) so it repeatedly executes.  you may think you've already done that with your onclipevent but you then probably have a scope issue which you should eliminate by attaching no code to objects.  all your code should be attached to the main timeline (with the exception of stop() and possibly goto's).

          • 2. Re: Side scroller game AS2
            zoner9 Level 1

            I changed the code, so that it's within an onEnterFrame loop like this:

             

            function onEnterFrame()

            {

              if(_root.shooting = true)

              {

               _x = _x + speed;

              }

            }

             

            but i'm still having the same problem, where the movie clip isn't moving

             

            I also tried adding the code to the main timeline, but this caused the game to not load at all

            • 3. Re: Side scroller game AS2
              kglad Adobe Community Professional & MVP

              you need to use something like:

               

              lasers._x=lasers._x+speed;

              • 4. Re: Side scroller game AS2
                zoner9 Level 1

                I changed the _x = _x + speed; to lasers._x = lasers._x + speed; but this still doesn't solve it.

                 

                would it help if I paste the full code for you to look at?

                • 5. Re: Side scroller game AS2
                  kglad Adobe Community Professional & MVP

                  are you creating more than one lasers?  if so, you have another problem:  they're all named the same and you're not storing them in an array. 

                   

                  fix that by giving them different names or, even better, store their references in an array.

                  • 6. Re: Side scroller game AS2
                    zoner9 Level 1

                    I think that i'm creating more than one, but only as duplicates of the same movie clip.

                     

                    how would i go about storing the values in an array?

                    • 7. Re: Side scroller game AS2
                      kglad Adobe Community Professional & MVP

                      you would use something like:

                       

                      var lasersA:Array=[];

                      .

                      .

                      .

                      var lasers = _root.attachMovie("lasers", "lasers" +_root.getNextHighestDepth(), _root.getNextHighestDepth());

                      lasersA.push(lasers);

                      .

                      .

                      .

                      function onEnterFrame()

                      {

                        if(_root.shooting = true)

                        {

                      for(var i:Number=lasersA.length-1;i>=0;i--){

                        lasersA[i]._x = lasersA[i]._x + speed;

                      if(lasersA[i].x>Stage.width){

                      lasersA[i].removeMovieClip();

                      lasersA.splice(i,1);

                      }

                      }

                        }

                      }

                      • 8. Re: Side scroller game AS2
                        prodigalmaster Level 2

                        Hi.

                         

                        function onEnterFrame()

                        {

                          if(_root.shooting = true)

                          {

                           _x = _x + speed;

                          }

                        }

                         

                        won't ever work because the operator '=' is an assignment not an equality checker. Try to change it to the '==' equality checker like:

                         

                        function onEnterFrame()

                        {

                          if(_root.shooting == true)

                          {

                           _x = _x + speed;

                          }

                        }

                        • 9. Re: Side scroller game AS2
                          zoner9 Level 1

                          I made the changes to my code that both of you suggested, but still no luck - any other suggestions I could try?

                          • 10. Re: Side scroller game AS2
                            kglad Adobe Community Professional & MVP

                            show the code you're using.

                            • 11. Re: Side scroller game AS2
                              zoner9 Level 1

                              On the lasers movie clip:

                               

                              onClipEvent(load)

                              //loads the script when on current clip in timeline

                              {

                              var speed;

                              //declares speed as a variable

                              _root.shooting == false

                              //declaring shooting as false

                              {

                                unloadMovie(this);

                                //removes the lasers movie clip from the screen

                              }

                              _root.shooting == true;

                              //declaring shooting is true as variable

                               

                              function onLoad()

                              //function is called when the file is loaded

                              {

                                speed = 20;

                                //sets speed equal to 20

                              }

                               

                              function onEnterFrame()

                              //function is called when the file is played

                              {

                                if(_root.shooting == true)

                                //if shooting is true

                                {

                                 for(var i:Number = lasersA.length-1; i>0; i--)

                                 //for i is equal to lasers array value minus 1, and greater than 0, decrease i by 1

                                 {

                                  lasersA[i]._x = lasersA[i]._x + speeed;

                                  //changes the x position of the lasers to previous value plus speed

                                  if(lasersA[i]._x > 600)

                                  //if the x position of lasers is greater than 600

                                  {

                                   lasersA[i].removeMovieClip();

                                   //removes the laser from screen

                                   lasersA.splice(i,1);

                                   //removes the values from the array

                                  }

                                 }

                                }

                              }

                              }

                               

                              On the player movie clip:

                              onClipEvent(load)
                              //loads the script when on current clip in timeline
                              {
                              var velocity;
                              //declares velocity as a variable

                              function onLoad()
                              //function is called when the file is loaded
                              {
                                velocity = 10;
                                //sets velocity variable to 10
                              }

                              function onEnterFrame()
                              //when the file is played
                              {
                                if(Key.isDown(Key.RIGHT))
                                //if the right arrow key is pressed
                                {
                                 _x = _x + velocity;
                                 //x position is equal to previous x position plus velocity
                                }
                                if(Key.isDown(Key.LEFT))
                                //if the left arrow key is pressed
                                {
                                 _x = _x - velocity;
                                 //x position is equal to previous x position minus velocity
                                }
                                if(Key.isDown(Key.UP))
                                //if the up arrow key is pressed
                                {
                                 _y = _y - velocity;
                                 /*y position is equal to previous y position minus velocity
                                 this is because up is negative y direction in flash */
                                }
                                if(Key.isDown(Key.DOWN))
                                //if the down arrow key is pressed
                                {
                                 _y = _y + velocity;
                                 //y position is equal to previous y position plus velocity
                                }
                                if(Key.isDown(Key.SPACE))
                                //if space key is pressed and shooting is false
                                {
                                 var lasersA:Array=[];
                                 //creates an array to store values for instances of lasers
                                 var lasers = _root.attachMovie("lasers", "lasers" +_root.getNextHighestDepth(), _root.getNextHighestDepth());
                                 /*local variable 'lasers' attaches the lasers movieclip to the
                                   root timeline, within the attach movie function the first "lasers"
                                   is the linkage ID, the second is the instance name, which is unique
                                   to the number of lasers on screen, this is followed by the depth,
                                   which is also unique to each laser of screen*/
                                 lasersA.push(lasers);
                                 //allows new values to be added to an array
                                 lasers._x = _x + 10;
                                 //sets the initial x position of the lasers equal to the ships x + 10
                                 lasers._y = _y - 20;
                                 //sets the initial y position of the lasers equal to the ships y - 20
                                 _root.shooting == true
                                 //shooting is true
                                }
                              }
                              }

                              • 12. Re: Side scroller game AS2
                                kglad Adobe Community Professional & MVP

                                again,   all your code should be attached to the main timeline (with the exception of stop() and possibly goto's).

                                 

                                remove all those onClipEvent's.

                                 

                                and lasersA needs to be declared and initialized once and not re-declared and re-initialized repeatedly.

                                • 13. Re: Side scroller game AS2
                                  zoner9 Level 1

                                  I placed all of the code onto the main timeline, removed the onClipEvents, and declared the array outside of the loops - however when i test my file, all i can see is a white background, nothing else.

                                   

                                  Have i done something wrong?

                                  • 14. Re: Side scroller game AS2
                                    kglad Adobe Community Professional & MVP

                                    copy and paste your updated code.

                                    • 15. Re: Side scroller game AS2
                                      zoner9 Level 1

                                      player movie clip:

                                       

                                      var velocity;
                                      //declares velocity as a variable
                                      var lasersA:Array=[];
                                      //creates an array to store values for instances of lasers

                                      function onLoad()
                                      //function is called when the file is loaded
                                      {
                                      velocity = 10;
                                      //sets velocity variable to 10
                                      }

                                      function onEnterFrame()
                                      //when the file is played
                                      {
                                      if(Key.isDown(Key.RIGHT))
                                      //if the right arrow key is pressed
                                      {
                                        _x = _x + velocity;
                                        //x position is equal to previous x position plus velocity
                                      }
                                      if(Key.isDown(Key.LEFT))
                                      //if the left arrow key is pressed
                                      {
                                        _x = _x - velocity;
                                        //x position is equal to previous x position minus velocity
                                      }
                                      if(Key.isDown(Key.UP))
                                      //if the up arrow key is pressed
                                      {
                                        _y = _y - velocity;
                                        /*y position is equal to previous y position minus velocity
                                        this is because up is negative y direction in flash */
                                      }
                                      if(Key.isDown(Key.DOWN))
                                      //if the down arrow key is pressed
                                      {
                                        _y = _y + velocity;
                                        //y position is equal to previous y position plus velocity
                                      }
                                      if(Key.isDown(Key.SPACE))
                                      //if space key is pressed and shooting is false
                                      {
                                        var lasers = _root.attachMovie("lasers", "lasers" +_root.getNextHighestDepth(), _root.getNextHighestDepth());
                                        /*local variable 'lasers' attaches the lasers movieclip to the
                                          root timeline, within the attach movie function the first "lasers"
                                          is the linkage ID, the second is the instance name, which is unique
                                          to the number of lasers on screen, this is followed by the depth,
                                          which is also unique to each laser of screen*/
                                        lasersA.push(lasers);
                                        //allows new values to be added to an array
                                        lasers._x = _x + 10;
                                        //sets the initial x position of the lasers equal to the ships x + 10
                                        lasers._y = _y - 20;
                                        //sets the initial y position of the lasers equal to the ships y - 20
                                        _root.shooting == true
                                        //shooting is true
                                      }
                                      }

                                       

                                      lasers movie clip:

                                       

                                      var speed;
                                      //declares speed as a variable
                                      _root.shooting == false
                                      //declaring shooting as false
                                      {
                                      unloadMovie(this);
                                      //removes the lasers movie clip from the screen
                                      }
                                      _root.shooting == true;
                                      //declaring shooting is true as variable

                                      function onLoad()
                                      //function is called when the file is loaded
                                      {
                                      speed = 20;
                                      //sets speed equal to 20
                                      }

                                      function onEnterFrame()
                                      //function is called when the file is played
                                      {
                                      if(_root.shooting == true)
                                      //if shooting is true
                                      {
                                        for(var i:Number = lasersA.length-1; i>0; i--)
                                        //for i is equal to lasers array value minus 1, and greater than 0, decrease i by 1
                                        {
                                         lasersA[i]._x = lasersA[i]._x + speeed;
                                         //changes the x position of the lasers to previous value plus speed
                                         if(lasersA[i]._x > 600)
                                         //if the x position of lasers is greater than 600
                                         {
                                          lasersA[i].removeMovieClip();
                                          //removes the laser from screen
                                          lasersA.splice(i,1);
                                          //removes the values from the array
                                         }
                                        }
                                      }
                                      }

                                      • 16. Re: Side scroller game AS2
                                        kglad Adobe Community Professional & MVP

                                        remove all your code.

                                         

                                        name your player movieclip (eg, player) and paste the following code into you main timeline:

                                         

                                        var velocity=10;

                                        var lasersA:Array = [];

                                        var speed=20;

                                         

                                        function onEnterFrame() {

                                            if (Key.isDown(Key.RIGHT)) {

                                                player._x += velocity;

                                            }

                                            if (Key.isDown(Key.LEFT)) {

                                                player._x -= velocity;

                                            }

                                            if (Key.isDown(Key.UP)) {

                                                player._y -= velocity;

                                            }

                                            if (Key.isDown(Key.DOWN)) {

                                                player_y += _velocity;

                                            }

                                            if (Key.isDown(Key.SPACE)) {

                                                var lasers = _root.attachMovie("lasers", "lasers" + _root.getNextHighestDepth(), _root.getNextHighestDepth());

                                                lasersA.push(lasers);

                                                lasers._x = _x + 10;

                                                lasers._y = _y - 20;

                                            }

                                            for (var i:Number = lasersA.length - 1; i > 0; i--) {

                                                lasersA[i]._x += speeed;

                                                if (lasersA[i]._x > 600) {

                                                    lasersA[i].removeMovieClip();

                                                    lasersA.splice(i,1);

                                                }

                                            }

                                        }

                                        • 17. Re: Side scroller game AS2
                                          zoner9 Level 1

                                          I copied this code into the main timeline, but the file still isn't loading for me - im not sure why this is happening

                                          • 18. Re: Side scroller game AS2
                                            kglad Adobe Community Professional & MVP

                                            what do you mean by, "it's not loading"?

                                            • 19. Re: Side scroller game AS2
                                              zoner9 Level 1

                                              when i test the file, all i can see is a window with a white background, nothing else - when i right click, the top option is greyed out and says "Movie Not Loaded..." and it doesn't progress from there regardless of the amount of time i leave to run

                                               

                                              This has only been happening since moving the code from the movie clips to the main timeline

                                              • 20. Re: Side scroller game AS2
                                                zoner9 Level 1

                                                I've managed to fix the issue, by creating an external class file, and moving my code into that, it all works fine now

                                                 

                                                Thanks for the help though, I learnt quite a bit about AS2 from this

                                                • 21. Re: Side scroller game AS2
                                                  kglad Adobe Community Professional & MVP

                                                  you're welcome.