10 Replies Latest reply on Feb 20, 2011 4:12 PM by rockwar

    AS2 Timeline Function Parameters

    rockwar Level 1


      Hi all

      I am a newby to Flash AS2 and I am working on my first game.

      The majority of the game I have been able to put together from tutorials and a book I bought.

      The problem I have is that in the game the hero thows melons and when they hit an enemy they explode.


      Now,  I've able to make the melon act the way I want when I add the AS  directly to the movieclip but I want to transfer this over to the main  timeline where I have created an array for the melons so that the hero  throws the melons constantly.


      This is the AS that woks on the movieclip-

       

      onClipEvent (load) {
           var statee;
           statee = "idle";
           maxJump = -15;
           grav = 1;
           gravity = 1.5;
           rotate = true;

       

      }
      onClipEvent (enterFrame) {
           if (Key.isDown(Key.SPACE)) {
               statee = "position";
           }
           if (statee == "position") {
               this._x = _root.hero._x;
               this._y = _root.hero._y;
               statee = "fire";
               grav = maxJump;
               rotate = true;
           }
           if (statee == "fire") {
               grav += gravity;
               this._y += grav;
               this._x += 10;
               this.gotoAndStop(1);
               rotate = true;
           }

       

          if (this.hitTest(_root.enemy)) {
               statee = "hit";
           }
           if (statee == "hit") {
               rotate = false;
               this.gotoAndStop(2);
               this._x = _root.enemy._x;
               this._y = _root.enemy._y;
           }

       

          if (rotate)
           {
               this._rotation += 25;
           }
           if (statee == "idle")
           {
               this._y = _y-550;
           }
           if (this._y>420 || this._x>550) {
               this.gotoAndStop(1);
               statee = "idle";
           }

       

      }

       

      I need to have this movement on my main time line with the array, but I cant figure out where to put it.

       

      //melons
      var melonSpeed=10;
      var melonReady=true;
      var melonDelay=100;
      var melonAmount=0;
      var melonArray=[];

       


      function createMelons(){
           var melonMc=this.attachMovie("melon","melon"+melonAmount, 1000+melonAmount);
          melonAmount++;
          melonMc._x= _root.hero._x;
           melonMc._y= _root.hero._y;
           melonArray.push(melonMc);
          
          
      }
      function moveMelons(){
           if(melonReady && Key.isDown(Key.SPACE)){
               melonReady=false;
               currentTime=getTimer();
               createMelons();
              
           }else{
              
           if(currentTime+melonDelay<=getTimer()){
               melonReady=true;
           }
           }
           for(var i=0; i<melonArray.length; i++){
               melonArray[i]._x+=melonSpeed;
              
           }
          
           }

       

      onEnterFrame = function(){

       

          moveMelons();

       

      Sorry for the long winded post but I've been at theis for a few days and I've hit a wall, thanks for all the help and advice

      Rock

        • 1. Re: AS2 Timeline Function Parameters
          kglad Adobe Community Professional & MVP

          there are better ways to do this (eg, use one enterframe loop) but here's a direct translation that maintains all the melon loops:

           



           

          //melons
          var melonSpeed=10;
          var melonReady=true;
          var melonDelay=100;
          var melonAmount=0;
          var melonArray=[];

           


          function createMelons(){
               var melonMc=this.attachMovie("melon","melon"+melonAmount, 1000+melonAmount);
              melonAmount++;
              melonMc._x= _root.hero._x;
               melonMc._y= _root.hero._y;
               melonArray.push(melonMc);

          with(melonMc){
               statee = "idle";
               maxJump = -15;
               grav = 1;
               gravity = 1.5;
               rotate = true;

          }    

          melonMc.onEnterFrame=function(){

          if (Key.isDown(Key.SPACE)) {
                   statee = "position";
               }
               if (statee == "position") {
                   this._x = _root.hero._x;
                   this._y = _root.hero._y;
                   statee = "fire";
                   grav = maxJump;
                   rotate = true;
               }
               if (statee == "fire") {
                   grav += gravity;
                   this._y += grav;
                   this._x += 10;
                   this.gotoAndStop(1);
                   rotate = true;
               }

           

              if (this.hitTest(_root.enemy)) {
                   statee = "hit";
               }
               if (statee == "hit") {
                   rotate = false;
                   this.gotoAndStop(2);
                   this._x = _root.enemy._x;
                   this._y = _root.enemy._y;
               }

           

              if (rotate)
               {
                   this._rotation += 25;
               }
               if (statee == "idle")
               {
                   this._y = _y-550;
               }
               if (this._y>420 || this._x>550) {
                   this.gotoAndStop(1);
                   statee = "idle";
               }

          }    
          }
          function moveMelons(){
               if(melonReady && Key.isDown(Key.SPACE)){
                   melonReady=false;
                   currentTime=getTimer();
                   createMelons();
                  
               }else{
                  
               if(currentTime+melonDelay<=getTimer()){
                   melonReady=true;
               }
               }
               for(var i=0; i<melonArray.length; i++){
                   melonArray[i]._x+=melonSpeed;
                  
               }
              
               }

           

          onEnterFrame = function(){

           

              moveMelons();

           

          Sorry for the long winded post but I've been at theis for a few days and I've hit a wall, thanks for all the help and advice

          Rock

          • 2. Re: AS2 Timeline Function Parameters
            rockwar Level 1

            Hi kglad

            thanks for your reply.

            I've tried this but it's not working, I've tried moving it around but I'm still lost. I am aiming to have all my code on the main time line. The AS I have on the melon movieclip moves the way I want it to, i.e. it is thrown up in the air and then it arcs downwards, but when added to the main time line the melons just fire in a straight line, and they keep going off screen. On the melon movieclip, as stated it arcs because of the gravity I gave it, and when it reaches slightly off the playing area it resets itself.

             

            I know that on the section-

             

               for(var i=0; i<melonArray.length; i++){
                      melonArray[i]._x+=melonSpeed;

             

             

            melonArray[i]._x+=melonSpeed; controls the melons going either forward or backwards, do I need to add all of the necessary code also here so that it rotates through the air etc.

             

            Thanks again for all your help, I hope I'm making sense I've confused myself already a few times with the explanation And could you please give a brief explanation of how it works, ive tried reading a number of tutorials but I'm a little confused.

             

            Rock

            • 3. Re: AS2 Timeline Function Parameters
              kglad Adobe Community Professional & MVP

              what movieclip is that code attached to?

              • 4. Re: AS2 Timeline Function Parameters
                rockwar Level 1

                It's not on a movieclip its on the main timeline layer1

                 

                 

                   for(var i=0; i<melonArray.length; i++){
                          melonArray[i]._x+=melonSpeed;

                 

                 

                All of the below is on a movieclip I made before I had my main playing area but now I want to add all of the movement and hittest to the main time line where I've made my array for the melons

                 

                onClipEvent (load) {
                    var statee;
                    statee = "idle";
                    maxJump = -15;
                    grav = 1;
                    gravity = 1.5;
                    rotate = true;

                 

                }
                onClipEvent (enterFrame) {
                    if (Key.isDown(Key.SPACE)) {
                        statee = "position";
                    }
                    if (statee == "position") {
                        this._x = _root.hero._x;
                        this._y = _root.hero._y;
                        statee = "fire";
                        grav = maxJump;
                        rotate = true;
                    }
                    if (statee == "fire") {
                        grav += gravity;
                        this._y += grav;
                        this._x += 10;
                        this.gotoAndStop(1);
                        rotate = true;
                    }

                 

                    if (this.hitTest(_root.enemy)) {
                        statee = "hit";
                       
                    }
                    if (statee == "hit") {
                        rotate = false;
                        this.gotoAndStop(2);
                        this._x = _root.enemy._x;
                        this._y = _root.enemy._y;
                       
                    }

                 

                    if (rotate)
                    {
                        this._rotation += 25;
                    }
                    if (statee == "idle")
                    {
                        this._y = _y-550;
                    }
                    if (this._y>420 || this._x>550) {
                        this.gotoAndStop(1);
                        statee = "idle";
                    }

                 

                }


                Thanks-Rock

                • 5. Re: AS2 Timeline Function Parameters
                  kglad Adobe Community Professional & MVP

                  what movieclip had those two onClipEvent events attached?

                  • 6. Re: AS2 Timeline Function Parameters
                    rockwar Level 1

                    They were attached to my melon movie clip. I done this at the beginning to try and figure out the correct physics for the melon goin through the air and to check how the hittest would work, and it all works reasonably well melon flys through air hits enemy and plays frame 2 of the melon MC, which is a small animation of the melon exploding. I need to transfer all of this over to my main time line to work with the array I created for continous shooting of melons, but I cant figure out where it goes in so that it works exactly the same as the AS I had on the melon MC.

                    Thanks for this kglad

                    Rock

                    • 7. Re: AS2 Timeline Function Parameters
                      kglad Adobe Community Professional & MVP

                      if that code was attached to each melon, then all the melons go to the right and down from hero when spacebar is pressed?  that doesn't seem right.

                      • 8. Re: AS2 Timeline Function Parameters
                        rockwar Level 1

                        Hi kglad, yes I have my hero running up a slight hill and as he throws the melon it goes up and then comes down in front of him hitting on-coming enemies, The problem I had with the AS when it was on the melon MC was that I could only fire 1 melon at a time so through reading various tutorials I made  an array on the main timeline of my game to fire multiple melons but they just fire in a straight line and they dont have a hittest, I need the setting I created on the melon MC to work on the main time line.

                        Thanks-Rock

                        • 9. Re: AS2 Timeline Function Parameters
                          kglad Adobe Community Professional & MVP

                          i don't see any easy way to fix your code.  it's sort of a mess.  and i don't think the code will do what you want.  i think you'll be better off reworking that code.

                           

                          use one enterframe loop.  inside that loop call various functions to

                           

                          1. check keypresses and call appropriate functons to take appropriate actions,

                          2.  update each melons position and

                          3.  check various melon conditions like enemy hittests and off-stage movement calling appropriate functions.

                          1 person found this helpful
                          • 10. Re: AS2 Timeline Function Parameters
                            rockwar Level 1

                            there are a few problems will rework and get back.

                            Thanks-Rock