8 Replies Latest reply on Jun 30, 2009 7:51 PM by Raidation

    gotoAndStop() not working properly?

    Raidation

      I think the topic title serves as the question.

       

      So yeah, basically, gotoAndStop() doesn't work. I am using Flash CS3.

       

       

      My code is simple:

       

       

       

      on(release) {

           _root.gotoAndStop("menu");

      }

       

       

       

      It's simple, right?

       

      I put that code into a button. The menu frame is three frames behind the frame that I put the button on. If I replace the "menu" with "game", it works. The game frame is one frame behind the frame the button is on.

       

      What happens is instead of going to the menu frame, it flickers to the menu frame, then reloads the lose frame, which is where the button is. I know that it reloads because I have a dynamic text field that throws a random insult when you enter that frame.

       

      I can assure you. I have already double checked and triple checked that the frame label is correct. And the menu frame has stop() in it. I'm not that stupid, alright?

       

      Any help would be appreciated.


      Thanks,

      ~Raidation

        • 1. Re: gotoAndStop() not working properly?
          pennsyfan

          Your code is embeded on the button level and it is trying to stop at the root level of the button time line as I understand it.

           

          A couple of things here.

           

          As a practice I put all my code in a layer called "actions". This way I can always find my code in one spot and don't have to keep deep diving into multple objects to find the code that I want to edit. I can edit the actions of the up, over, down etc for any button all in one spot.

           

          try this code in the main time line of your swf file

           

          myBtn,onRelease = function (){

               gotoAndStop("menu")

          }

           

          This will only work if you created a KeyFrame for the menu section of your swf file.

           

          You did create a KeyFrame when you placed the menu frame label right?

           

          Hope this helps.

           

          Jim

          • 2. Re: gotoAndStop() not working properly?
            pennsyfan Level 1

            Opps this

             

            gotoAndStop("menu")

             

            Should be

             

            gotoAndStop("menu");

             

            sorry I missed the closing semi colon

             

            Jim

            • 3. Re: gotoAndStop() not working properly?
              Ned Murphy Adobe Community Professional & MVP

              Alright but you must have made some small error because the code will work, and if you say you see it flickering at that frame it is working, but something is causing it to move away from that frame... that code will not cause it to move away once it's there.  There's no telling whatever causes it to move from this end and your description though.

              • 4. Re: gotoAndStop() not working properly?
                Raidation Level 1

                No, sorry. It doesn't work.

                 

                I said the game flickered to the menu for a half-second, then reloaded the lose frame, which means the game DOES go to the menu for a brief moment.

                 

                [Edit]

                You beat me.

                 

                I can post the source without the awesome graphics and stuff. Wait...

                • 5. Re: gotoAndStop() not working properly?
                  Raidation Level 1

                  Actually, it's something wrong with the game code.

                  Sorry.

                   

                  I removed the game code for the demo and the menu button worked perfectly.

                   

                  I'll post the game code.

                   

                  Wait.

                  • 6. Re: gotoAndStop() not working properly?
                    Raidation Level 1

                    Here it is:

                     

                    Frame Code:

                     

                    stop();

                    var numbullets = 0;

                    var stupidvariableconfusion = 0;

                    var score = 0;

                    var numlships = 0;

                    var gr = 1;

                    var numpowerups = 0;

                     

                    makeAlien1 = function() {

                         var ud = random(2);

                         stupidvariableconfusion ++;

                         mc = _root.attachMovie("mc_alien1","mc_alien1"+stupidvariableconfusion,_root.getNextHighestDep th());

                         mc._y = ud*480;

                         mc._x = 640;

                         mc.timer = random(76);

                         if(mc._y == 480) {

                              mc._rotation = Math.floor(Math.random() * ( 331 - 270 )) + 30;

                              mc.onEnterFrame = function() {

                                   this._x -= Math.cos(this._rotation*Math.PI/180)*4;

                                   this._y -= Math.sin(this._rotation*Math.PI/180)*4;

                                   this.timer --;

                                   if(this.timer == 0) {

                                        numbullets ++;

                                        mc = _root.attachMovie("mc_ebullet","mc_ebullet"+numbullets,_root.getNextHighestDepth());

                                        mc._x = this._x;

                                        mc._y = this._y;

                                        mc.speed = -16;

                                        mc.reversed = false;

                                   }

                                   for(i = 1; i <= numbullets; i ++) {

                                        var emc = _root["mc_ebullet"+i];

                                        if(this.hitTest(emc) && emc.reversed) {

                                             this.gotoAndPlay(2);

                                             emc.removeMovieClip();

                                             score += 5;

                                             if(random(6) == 1) {

                                                  numpowerups ++;

                                                  randnum = random(3);

                                                  if(randnum == 0) {

                                                       mc = _root.attachMovie("mc_bronze","mc_bronze"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 1) {

                                                       mc = _root.attachMovie("mc_silver","mc_silver"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 2) {

                                                       mc = _root.attachMovie("mc_gold","mc_gold"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                             }

                                        }

                                   }

                                   if(this._y < 0) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                         if(mc._y == 0) {

                              mc._rotation = Math.floor(Math.random() * ( 91 - 30 )) + 270;

                              mc.onEnterFrame = function() {

                                   this._x -= Math.cos(this._rotation*Math.PI/180)*4;

                                   this._y -= Math.sin(this._rotation*Math.PI/180)*4;

                                   this.timer --;

                                   if(this.timer == 0) {

                                        numbullets ++;

                                        mc = _root.attachMovie("mc_ebullet","mc_ebullet"+numbullets,_root.getNextHighestDepth());

                                        mc._x = this._x;

                                        mc._y = this._y;

                                        mc.speed = -16;

                                        mc.reversed = false;

                                   }

                                   for(i = 1; i <= numbullets; i ++) {

                                        var emc = _root["mc_ebullet"+i];

                                        if(this.hitTest(emc) && emc.reversed) {

                                             this.gotoAndPlay(2);

                                             emc.removeMovieClip();

                                             score += 5;

                                             if(random(5) == 1) {

                                                  numpowerups ++;

                                                  randnum = random(3);

                                                  if(randnum == 0) {

                                                       mc = _root.attachMovie("mc_bronze","mc_bronze"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 1) {

                                                       mc = _root.attachMovie("mc_silver","mc_silver"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 2) {

                                                       mc = _root.attachMovie("mc_gold","mc_gold"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                             }

                                        }

                                   }

                                   if(this._y > 480) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                    }

                    addscore = function() {

                         score ++;

                    }

                    makeAlien2 = function() {

                         if(score >= 75) {

                              mc = _root.attachMovie("mc_alien2","mc_alien2"+stupidvariableconfusion,_root.getNextHighestDep th());

                              mc._x = 640;

                              mc._y = Math.floor(Math.random()*(450-30))+30;

                              mc.timer = random(41);

                              mc.target = Math.floor(Math.random()*(450-30))+30;

                              mc.numbullets = 0;

                              mc._rotation = Math.floor(Math.random() * ( 90 )) + 315;

                              mc.onEnterFrame = function() {

                                   this._x -= Math.cos(this._rotation*Math.PI/180)*4;

                                   this._y -= Math.sin(this._rotation*Math.PI/180)*4;

                                   this.timer --;

                                   if(this.timer == 0 && this.numbullets < 3) {

                                        this.timer = 10;

                                        numbullets ++;

                                        this.numbullets ++;

                                        emc = _root.attachMovie("mc_ebullet","mc_ebullet"+_root.numbullets,_root.getNextHighestDepth()) ;

                                        emc._x = this._x;

                                        emc._y = this._y;

                                        emc._rotation = this._rotation+Math.floor(Math.random()*60)-30;

                                        emc.speed = -16;

                                        emc.reversed = false;

                                   }     

                                   for(i = 1; i <= numbullets; i ++) {

                                        var emc = _root["mc_ebullet"+i];

                                        if(this.hitTest(emc) && emc.reversed) {

                                             this.gotoAndPlay(2);

                                             emc.removeMovieClip();

                                             _root.score += 5;

                                             if(random(5) == 1) {

                                                  numpowerups ++;

                                                  randnum = random(3);

                                                  if(randnum == 0) {

                                                       mc = _root.attachMovie("mc_bronze","mc_bronze"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }     

                                                  if(randnum == 1) {

                                                       mc = _root.attachMovie("mc_silver","mc_silver"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 2) {

                                                       mc = _root.attachMovie("mc_gold","mc_gold"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                             }

                                        }

                                   }

                                   if(this._x < 0) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                    }

                    makeAlien3 = function() {

                         if(score >= 140) {

                              mc = _root.attachMovie("mc_alien3","mc_alien3"+stupidvariableconfusion,_root.getNextHighestDep th());

                              mc._x = 640;

                              mc._y = random(461)+20;

                              mc.onEnterFrame = function() {

                                   this._x -= 12;

                                   if(this.hitTest(_root.ship)) {

                                        _root.ship.stunned = true;

                                        _root.ship.stuntimer = 72;

                                   }

                                   for(i = 1; i <= numlships; i ++) {

                                        emc = _root["mc_lship"+i];

                                        if(this.hitTest(emc)) {

                                             emc.gotoAndPlay(2);

                                        }

                                   }

                                   if(this._y < 0) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                    }

                    makeAlien4 = function() {

                         if(score >= 200) {

                              mc = _root.attachMovie("mc_alien4","mc_alien4"+stupidvariableconfusion,_root.getNextHighestDep th());

                              mc._x = random(321)+320;

                              mc._y = 0;

                              mc.timer = random(61);

                              mc.target = random(321)+320;

                              mc.angle = Math.atan2(480,Math.abs(mc._x-mc.target));

                              mc.onEnterFrame = function() {

                                   this._y += Math.sin(this.angle)*4;

                                   this.timer --;

                                   if(this.timer == 0) {

                                        numbullets ++;

                                        emc = _root.attachMovie("mc_ebullet","mc_ebullet"+numbullets,_root.getNextHighestDepth());

                                        emc._x = this._x;

                                        emc._y = this._y;

                                        emc.dx = this._x-_root.ship._x;

                                        emc.dy = this._y-_root.ship._y;

                                        emc.angle = Math.atan2(emc.dy,emc.dx);

                                        emc._rotation = emc.angle*180/Math.PI;

                                        emc.speed = -16;

                                   }

                                   for(i = 1; i <= numbullets; i ++) {

                                        var emc = _root["mc_ebullet"+i];

                                        if(this.hitTest(emc) && emc.reversed) {

                                             this.gotoAndPlay(2);

                                             emc.removeMovieClip();

                                             _root.score += 5;

                                             if(random(5) == 1) {

                                                  numpowerups ++;

                                                  randnum = random(3);

                                                  if(randnum == 0) {

                                                       mc = _root.attachMovie("mc_bronze","mc_bronze"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }     

                                                  if(randnum == 1) {

                                                       mc = _root.attachMovie("mc_silver","mc_silver"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                                  if(randnum == 2) {

                                                       mc = _root.attachMovie("mc_gold","mc_gold"+numpowerups,_root.getNextHighestDepth());

                                                       mc._x = this._x;

                                                       mc._y = this._y;

                                                  }

                                             }

                                        }

                                   }

                                   if(this._y > 480) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                    }

                    this.onEnterFrame = function() {

                         if(numlships < 4 && score/100 >= gr) {

                              randnum = random(4)+1;

                              mc = _root.attachMovie("mc_lship"+randnum,"mc_lship"+numlships,_root.getNextHighestDepth());

                              mc._x = 44.5;

                              mc._y = random(441)+20;

                              mc.ty = 0;

                              mc.by = 480;

                              mc.dir = random(2);

                              numlships ++;

                              gr ++;

                              mc.onEnterFrame = function() {

                                   with(this) {

                                        if(dir == 1) {

                                             if(ty < _y) {

                                                  var dis = (_y-ty)/24;

                                                  _y -= dis;

                                             }

                                        }

                                        if(dir == 0) {

                                             if(by > _y) {

                                                  var dis = (by-_y)/24;

                                                  _y += dis;

                                             }

                                        }

                                        if(Math.round(_y) <= ty+21) {

                                             dir = 0;

                                        }

                                        if(Math.round(_y) >= by-21) {

                                             dir = 1;

                                        }

                                        for(i = 0; i <= _root.numbullets; i ++) {

                                             emc = _root["mc_ebullet"+i];

                                             if(this.hitTest(emc) && _visible) {

                                                  emc.removeMovieClip();

                                                  gotoAndPlay(2);

                                                  _root.numlships --;

                                             }     

                                        }

                                   }

                              }

                         }

                         if(numlships == 0) {

                              clearInterval(m1);

                              clearInterval(m2);

                              clearInterval(m3);

                              clearInterval(m4);

                              clearInterval(as);

                              _root.gotoAndStop("lose");

                         }

                    }

                     

                     

                    m1 = setInterval(makeAlien1,3000);

                    m2 = setInterval(makeAlien2,5500);

                    m3 = setInterval(makeAlien3,7000);

                    m4 = setInterval(makeAlien4,11000);

                    as = setInterval(addscore,1000);

                    Yeah. I know. It's messy.

                     

                    Code for ship:

                     

                    onClipEvent(load) {

                         var dx;

                         var dy;

                         var speed = 12;

                         var numshields = 0;

                         var stunned = false;

                         var stuntimer = -1;

                    }

                    onClipEvent(enterFrame) {

                         if(!stunned) {

                              dx = _root._xmouse-_x;

                              dy = _root._ymouse-_y;

                              distance = Math.sqrt(dx*dx+dy*dy);

                              angle = Math.atan2(dy,dx);

                              _rotation = angle*180/Math.PI;

                              if(_root._ymouse > _y) {

                                   var dis = (_root._ymouse-_y)/speed;

                                   _y += dis;

                              }

                              if(_root._ymouse < _y) {

                                   var dis = (_y-_root._ymouse)/speed;

                                   _y -= dis;

                              }

                         }

                         stuntimer --;

                         if(stuntimer == 0) {

                              stunned = false;

                         }

                    }

                    onClipEvent(mouseDown) {

                         if(!stunned) {

                              numshields ++;

                              mc = _root.attachMovie("mc_shield","mc_shield"+numshields,_root.getNextHighestDepth());

                              mc._x = Math.cos(angle)*48+_x;

                              mc._y = Math.sin(angle)*48+_y;

                              mc.angle = angle;

                              mc.shoot = false;

                              mc.onEnterFrame = function() {

                                   if(this.shoot) {

                                        this._x += Math.cos(this.angle)*12;

                                        this._y += Math.sin(this.angle)*12;

                                   }else{

                                        this._rotation = _root.ship._rotation;

                                        this._x = Math.cos(angle)*48+_x;

                                        this._y = Math.sin(angle)*48+_y;

                                        this.angle = angle;

                                   }

                                   for(i = 1; i <= _root.numbullets; i ++) {

                                        if(this.hitTest(_root["mc_ebullet"+i])) {

                                             _root["mc_ebullet"+i]._rotation = this._rotation-180;

                                             _root["mc_ebullet"+i].reversed = true;

                                        }

                                   }

                                   for(i = 1; i <= _root.numpowerups;i ++) {

                                        pub = _root["mc_bronze"+i];

                                        pus = _root["mc_silver"+i];

                                        pug = _root["mc_gold"+i];

                                        if(this.hitTest(pub)) {

                                             _root.score += 5;

                                             pub.removeMovieClip();

                                        }

                                        if(this.hitTest(pus)) {

                                             _root.score += 10;

                                             pus.removeMovieClip();

                                        }

                                        if(this.hitTest(pug)) {

                                             _root.score += 25;

                                             pug.removeMovieClip();

                                        }

                                   }

                                   if(this._x > 640) {

                                        this.removeMovieClip();

                                   }

                              }

                         }

                    }

                    onClipEvent(mouseUp) {

                         mc = _root["mc_shield"+numshields];

                         mc.shoot = true;

                    }

                    Still messy.

                     

                    Code for all the lships.

                     

                    onClipEvent(load) {

                         var ty = 0;

                         var by = 480;

                         var dir = 0;

                         _root.numlships ++;

                    }

                    onClipEvent(enterFrame) {

                         if(dir == 1) {

                              if(ty < _y) {

                                   var dis = (_y-ty)/24;

                                   _y -= dis;

                              }

                         }

                         if(dir == 0) {

                              if(by > _y) {

                                   var dis = (by-_y)/24;

                                   _y += dis;

                              }

                         }

                         if(Math.round(_y) <= ty+21) {

                              dir = 0;

                         }

                         if(Math.round(_y) >= by-21) {

                              dir = 1;

                         }

                         for(i = 0; i <= _root.numbullets; i ++) {

                              emc = _root["mc_ebullet"+i];

                              if(this.hitTest(emc) && _visible) {

                                   emc.removeMovieClip();

                                   gotoAndPlay(2);

                                   _root.numlships --;

                              }

                         }

                    }

                    It's all the same.

                     

                     

                    I know it's going to take some time to read through the code.

                    But I would really appreciate help.

                     

                     

                     

                     

                     

                     

                     

                     

                    • 7. Re: gotoAndStop() not working properly?
                      Ned Murphy Adobe Community Professional & MVP

                      You'll need to comb thru that code and check any parts of it that would cause it to move to another frame--you say it moves to the lose frame, so maybe you know which frame that is, which may make it easier for you to find it.

                      • 8. Re: gotoAndStop() not working properly?
                        Raidation Level 1

                        Thank you! It works now!

                         

                        All I had to do was add one simple line of code:

                         

                        delete this.onEnterFrame;

                         

                         

                        *smashes head on laptop*