11 Replies Latest reply on May 15, 2006 8:12 PM by ottimoto

    _root.gotoAndPlay("label")  in function

    ottimoto Level 1
      I'm calling a function which is created on my main timeline in frame 1. Once a ceratin condition is met I ask with _root.gotoAndPlay("frame label") to jump to a certain frame label on my main timeline. But my movie jumps always to frame 1 on the main timeline. I did not use scenes, since I read they don't work. Can I not use gotoAndPlay or gotoAndStop inside a function?
      Any help would be very much appreciated. This is a assignment I have to hand in soon.
      woschd
        • 1. Re: _root.gotoAndPlay("label")  in function
          Level 7
          That should work just fine .. there must be something else that you're NOT
          telling us about that is stuffing it up.
          --
          Jeckyl


          • 2. Re: _root.gotoAndPlay("label")  in function
            Craig Grummitt Level 3
            maybe check that your label type is set to Name, and ensure there are no duplicate labels.
            • 3. Re: _root.gotoAndPlay("label")  in function
              brad_sa Level 1
              Hi,

              How are you calling the function?

              ie. onEnterFrame = function() or do you just have a function set up that you call with a button press or some other trigger?

              It might help if you paste all the code in the thread for us.

              In the target frame is there a stop(); or have you set something else up to stop it playing through back to frame1? Does it work if you use the frame number instead of the frame label?

              I know I have given you more questions than answers, but it will probably help in giving the right answer.

              cheers

              Brad
              • 4. Re: _root.gotoAndPlay("label")  in function
                ottimoto Level 1
                Thnaks guys for all the effort and thoughts. Here is my code:
                On frame label ("gameStart") I have got all my main actionscript. There is an invisible button also on frame 1 which starts the function on pressing "Space".
                ButtonScript:
                on (keyPress "<Space>") {
                playGame();
                }

                Main actionscript (sorry but it is long):
                stop();
                //create variable for start in x position
                var xStart:Number = 41;
                //create y position start variable for first 2 rows
                var yStart1:Number = 45;
                //create y position start variable for second 2 rows
                var yStart2:Number = 87;
                //create y position start variable for third 2 rows
                var yStart3:Number = 129;
                //create variable for number of rows
                var row:Number = 2;
                //create variable for number of columns
                var col:Number = 8;
                //create variable for horixontal distance between mcs
                var xDist:Number = 65;
                //create variable for vertical distance between mcs
                var yDist:Number = 20;
                //create variable for bricks depth
                var d:Number = 0;
                //create variable for puck depth
                var f:Number = 0;
                //create variable to remove yellow bricks
                var dRYel:Number = 32;
                //create variable to remove red bricks
                var dRRed:Number = 16;
                //create variable to remove green bricks
                var dRGre:Number = 0;
                //create variable between -5 and 15 for x movement (speed}
                var dx:Number = Math.random()*20-5;
                //create variable betwwen 5 and 15 for positive y movement (speed)
                var dy:Number = Math.random()*10+5;
                //define variable for score and set to 0
                var score:Number = 0;
                //since the registration points for batMC and walls are top left
                //the height/width of batMC or height/width of walls
                //have to be added or subtracted to get playing field boundaries
                //variable for top boundary
                var tb:Number = _root.topWallInst._y+_root.topWallInst._height;
                //variable for bottom boundary
                var bb:Number = _root.batInst._y+_root.batInst._height;
                //variable for left boundary
                var lb:Number = _root.leftWallInst._x+_root.leftWallInst._width;
                //variable for right boundary
                var rb:Number = _root.rightWallInst._x;
                //variable for field width
                var fw:Number = rb-lb;
                //variable for field height
                var fh:Number = bb-tb;
                //variable for puck x position
                var puckX:Number = _root.fw/2+_root.batInst._width/2-7;
                //variable for puck y position
                var puckY:Number = _root.batInst._y-14;
                //create function to build bricks
                function buildPuck() {
                //attach puck from library
                var refPuck = this.attachMovie("puckMC", "puckInst", f);
                refPuck._x = _root.puckX;
                refPuck._y = _root.puckY;
                //trace(refPuck);
                }
                function buildBlocks() {
                //loop for 2 first green rows
                //2 rows first
                for (var i = 1; i<=row; i++) {
                //now 8 columns
                for (var j = 1; j<=col; j++) {
                //increase the number for depth by one
                d++;
                //create variable and attache from library
                var refGreen = this.attachMovie("greBrickMC", "greBrickMC"+d, d);
                //position bricks
                refGreen._x = xStart+xDist*(j-1);
                refGreen._y = yStart1+yDist*(i-1);
                //trace(refGreen);
                }
                }
                //loop for 2 red rows
                //2 rows first
                for (var i = 1; i<=row; i++) {
                //now 8 columns
                for (var j = 1; j<=col; j++) {
                //increase the number for depth by one
                d++;
                //create variable and attache from library
                var refRed = this.attachMovie("redBrickMC", "redBrickMC"+d, d);
                //position bricks
                refRed._x = xStart+xDist*(j-1);
                refRed._y = yStart2+yDist*(i-1);
                //trace(refRed);
                }
                }
                //loop for 2 yellow rows
                //2 rows first
                for (var i = 1; i<=row; i++) {
                //now 8 columns
                for (var j = 1; j<=col; j++) {
                //increase the number for depth by one
                d++;
                //create variable and attache from library
                var refYel = this.attachMovie("yelBrickMC", "yelBrickMC"+d, d);
                //position bricks
                refYel._x = xStart+xDist*(j-1);
                refYel._y = yStart3+yDist*(i-1);
                //trace(refYel);
                }
                }
                }
                //build function to remove yellow bricks
                function yelBrickrem() {
                //remove yellow bricks using their correct depth
                for (var i = 1; i<=row; i++) {
                for (var j = 1; j<=col; j++) {
                //trace(dRYel);
                dRYel++;
                _root["yelBrickMC"+dRYel].removeMovieClip();
                }
                }
                }
                //build function to remove red bricks
                function redBrickrem() {
                //remove red bricks using their correct depth
                for (var i = 1; i<=row; i++) {
                for (var j = 1; j<=col; j++) {
                //trace(dRRed);
                dRRed++;
                _root["redBrickMC"+dRRed].removeMovieClip();
                }
                }
                }
                //build function to remove green bricks
                function greBrickrem() {
                //remove green bricks using their correct depth
                for (var i = 1; i<=row; i++) {
                for (var j = 1; j<=col; j++) {
                //trace(dRGre);
                dRGre++;
                _root["greBrickMC"+dRGre].removeMovieClip();
                }
                }
                }
                //call function to build blocks
                buildBlocks();
                //call function to build puck
                buildPuck();
                //declare function for puck movement
                function playGame():Void {
                _root.puckInst.onEnterFrame = function() {
                //adds variable dx to x position to move puck in x direction
                _root.puckInst._x += dx;
                //adds variable dy to y position to move puck in y direction
                _root.puckInst._y -= dy;
                //trace(dy);
                //if speed in x direction is higher than 25
                if (Math.abs(dx)>20) {
                //give dx original value (slower)
                dx = Math.random()*20-5;
                //trace(dx);
                }
                //if speed in y direction is higher than 25
                if (Math.abs(dy)>20) {
                //give dy original value (slower)
                dy = Math.random()*10+5;
                //trace(dy);
                }
                //if y coordinate of puck is smaller than top boundary
                if (_root.puckInst._y<=_root.tb) {
                //trace(this._y);
                //multiply dy with -1.05 to increase speed and reverse direction
                dy *= -1.05;
                //trace(dy);
                //change alpha of top wall to a random value between 20 and 100
                _root.topWallInst._alpha = Math.random()*80+20;
                //if y coordinate of puck is larger than bottom boundary
                } else if (_root.puckInst.hitTest(_root.batInst)) {
                //trace(this._y);
                //multiply dy with -1.1 to increase speed in y direction
                dy *= -1.1;
                } else if (_root.puckInst._y>=bb) {
                _root.puckInst.unloadMovie();
                //trace(puckInst);
                //remove yellow bricks using their correct depth
                yelBrickrem();
                redBrickrem();
                greBrickrem();
                _root.gotoAndPlay("end");
                }
                //if x coordinate of puck is smaller than left boundary
                if (_root.puckInst._x<=_root.lb) {
                //multiply dx with -1.05 to increase speed and reverse direction
                dx *= -1.05;
                //change alpha of left wall to a random value between 20 and 100
                _root.leftWallInst._alpha = Math.random()*80+20;
                //if x coordinate of puck is larger than right boundary
                } else if (_root.puckInst._x>=_root.rb-_root.puckInst._width) {
                //multiply dx with -1.05 to increase speed in x direction
                dx *= -1.05;
                //change alpha of right wall to a random value between 20 and 100
                _root.rightWallInst._alpha = Math.random()*80+20;
                }
                }
                }
                stop();

                There is only a stop(); on the attacheMovie "puckInst".

                The main concern is the line:
                _root.gotoAndPlay("end"); in the function "playGame".

                It seems when this condition is met, theplayhead still goes through the other frames until it stops again at frame 1.

                Thanks again so much for your help.

                woschd
                • 5. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                  Level 7
                  So .. where is the stop() in the last frame of the movie to stop it looping
                  back to the start?


                  • 6. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                    Craig Grummitt Level 3
                    wow that's a lot of code to wade through without seeing the context its in(ie what's on the stage). but first is it simply that you've used gotoAndPlay rather than gotoAndStop?
                    • 7. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                      ottimoto Level 1
                      There is a stop at the beginning of this main code and also at the end. Do I need also a stop in the if statement when I'm directing to the lable on frame 1? So, stop the function, whilst in the function? I tried that just now but can't get it to work???

                      I really appreciate all your help and looking through my code.

                      woschd
                      • 8. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                        ottimoto Level 1
                        gotoAndStop does the same as gotoAndPlay. It still goes through the rest of the frames until it's back to frame label 1. I think I really just did not get the stop's correct. But I would not know where to put them .....

                        woschd
                        • 9. _root.gotoAndPlay(&quot;label&quot;)  in function
                          Craig Grummitt Level 3
                          As brad_sa asked, "Does it work if you use the frame number instead of the frame label?"
                          Hmm.. this guessing game may take a while. Possible to post a link to the fla?
                          • 10. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                            ottimoto Level 1
                            Hi Craig,

                            THanks again for all the effort to help.
                            Here is a ftp link:
                            ftp.advancepress.com.au
                            username: public01
                            password: j9efia
                            Folder: files-from-advance
                            Filename: Assign02.fla

                            Website is not running so I had to upload on fto site at work. Hope you can access.
                            I tried to direct to label "end" on frame 3 but it still jumps back to frame 1.
                            I have also tried to go to frame number instead of label, but did not work either.

                            Cheers
                            woschd
                            • 11. Re: _root.gotoAndPlay(&quot;label&quot;)  in function
                              ottimoto Level 1
                              Craig,

                              I found the problem, so don't bother with downloading my file.
                              I had the enterFrame incorporated in the function, so it did continue the enterFrame coimmand first.
                              Sometimes I just have to get up, do something else, try not to thinkl about the problem and than comes the solution.

                              Thanks again very much.

                              woschd