4 Replies Latest reply on Feb 23, 2008 10:55 AM by Blindgator

    variables in function?

    Blindgator
      This is really driving me nuts. And I think it is just the variables are local or something. I have this variable that declares the speed for a certain bomb but when I use it inside the onEnterFrame it traces undefined. Then if I trace it outside the onEnterFrame it traces correctly. Is this because its not global?

      game.bombs["s"+bombNum] = PbombS[random(PbombS.length)];
      _root["bomb"+bombNum].onEnterFrame = function() {
      this._y += 10;
      trace(_root.game.bombs["s"+bombNum]);
      }
        • 1. Re: variables in function?
          rlc5611 Level 1
          The script you posted doesn't make a lot of sense as the onEnterFrame functions looks like it has a completely different path that the game.bombs.xxxx that you are creating.

          But there is no point in trying to trace root.game.bombs["s"+bombNum] as, at the very least, bombNum will have changed or if you have created your bombs in a for loop, bombNum will have incremented what is past the last bomb created.

          So when you creat your bomb, such as game.bombs.createEmptyMovieClip(... or with attachMovie(....., assign the number of the bomb as a property of the movie clip as that clip is created. You should have something like game.bombs["s"+bombNum] .mybombnumber = bombNum. Later you can recover that variable as root.game.bombs["s"+bombNum].mybombnumber.

          game.bombs["s"+bombNum] = PbombS[random(PbombS.length)];

          What is PbombS in this script BTW?

          In your onEnterFrame function, trace this:

          trace(bombNum); See what you are actually using to try to reference your movie clip.
          • 2. Re: variables in function?
            Blindgator Level 1
            I knew I was staring something stupid right in the face. Yeah that never occured to me that the variable bombNum is being changed while the onEnterFrame 's are running.

            game = {};
            game.bombs = {};

            function makeBombsF() {
            RandX = Math.round(Math.random()*(580))+10;
            mybombNum = bombNum;
            game.bombs["s"+mybombNum] = PbombS[random(PbombS.length)];
            //random speed for bomb
            ranI = ranMB[random(ranMB.length)];
            _root.attachMovie("bomba", "bomb"+bombNum, bombNum, {_x:RandX, _y:0, _xscale:RanS, _yscale:RanS});
            _root["bomb"+bombNum].onEnterFrame = function() {
            this._y += game.bombs["s"+mybombNum];
            };
            clearInterval(makeBombi);
            bombNum++;
            makeBombi = setInterval(makeBombsF, ranI);
            }

            PbombS is an array with differant possible speeds for the bombs to go.
            See the thing is when I do get the script to work the bombs will come really fast and then as one value changes they come very slow. And really I want each bomb to have its own individual speed. Also I think im getting the object and the movieclip mixed around. Can you please give me an example of how to give each bomb its own speed.
            I really appreciate the help.
            • 3. Re: variables in function?
              rlc5611 Level 1
              Just store permanent variable in each clip as you create them. Later use them with the "this" reference and let Flash do its own housekeeping.


              function makeBombsF() {

              RandX = Math.round(Math.random()*(580))+10;

              //random speed for bomb
              ranI = ranMB[random(ranMB.length)];//???

              _root.attachMovie("bomba", "bomb"+bombNum, bombNum, {_x:RandX, _y:0, _xscale:RanS, _yscale:RanS});

              //------------------------------------------------------------------

              _root["bomb"+bombNum].mynumber = bombNum;//store the number of the clip as a variable within the new clip
              _root["bomb"+bombNum].myspeed = PbombS[Math.floor(Math.random()*PbombS.length)];//store the bomb's speed within the new clip

              //--------------------------------------------------------------------

              //it's okay to use bombNum here because bombNum still has validity at this point.

              _root["bomb"+bombNum].onEnterFrame = function() {
              this._y += this.myspeed;//myspeed is now stored in each clip so can be used by its onEnterFrame function
              • 4. Re: variables in function?
                Blindgator Level 1
                Got it Working!!!!!!!!! and I understand it now.
                MUCHOS GRACIAS