4 Replies Latest reply on Oct 21, 2009 7:32 AM by Rothrock

    swapdepths problem

    robin1232 Level 1

      Im trying to create an rpg game that swaps the depths whenever the _y is above a certain number

      this would allow the character to go behind and before an "obstacle"

      now I have this code:

       

          if (this._y < 98){
              swapped = 0;
          }

       

      if (swapped < 1){
              this.swapDepths(_root.obstacle);
              swapped = 1;
          }

       

      but as you can see, it keeps swapping back to 0 if the _y is less then 98, I only need it to swap once, so it doesnt glitch.

      can anyone help me with this?

        • 1. Re: swapdepths problem
          webqaflash

          It is because o, your first condition is  this._y < 98, this will not changed if swapped or not.

           

          Whnever you call the script this._y is always less than 98 then execute swapped = 0;

           

              if (this._y < 98){
                  swapped = 0;
              }

           

          if (swapped < 1){
                  this.swapDepths(_root.obstacle);
                  swapped = 1;
              }

          • 2. Re: swapdepths problem
            Rothrock Level 5

            There is a good trick for this process and the best part about it you can have multiple items all stacked up. Your current system will get very complex if you have multiple characters and/or obstacles.

             

            You will need to have a list of all the clips you want to order -- which included both the character(s) and the obstacle(s) then on every frame (or whenever something moves depending upon your structure) you swapDepths to 20 times the _y value. I chose 20 because Flash records location to twips (twentieth of a pixel). It is possible to get a little flicker if someone positions things exactly equally, but in practice I have never found this to be a problem.

            • 3. Re: swapdepths problem
              robin1232 Level 1

              can you explain that a little bit more? maybe even give some code? cause I have no idea what you mean

              • 4. Re: swapdepths problem
                Rothrock Level 5

                First I would put all the action stuff inside a movieclip called arena or something instead of on the _root. You are likely to want to do things like have scores and instructions that won't be swapped around.

                 

                Next I'm guessing that you put code directly on to your clips. Again I woulnd't do that. It isn't good practice and makes updating and changing the code really difficult. Here is a great article about it: http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

                 

                So lets say I have hero and obstacle0 and they are inside the clip arena. BTW, it works best if arena has the registration point in the upper right and the other things have theirs centered bottom. If that doesn't work for other reasons you could introduce some kind of delta factor....

                 

                stop();

                 

                var theItems:Array=["hero","obstacle0"];

                 

                arena.onEnterFrame=stackEm

                 

                function stackEm(){

                for(var i=0;i<theItems.length;i++){

                var curItem:MovieClip=arena[theItems[i]]

                curItem.swapDepths(Math.ceil(curItem._y*20))

                }

                }

                 

                The nice thing about this approach is that it is easy to add obstacles or heros or what have you. If you don't have a lot of other stuff inside of the arena clip you could also forget about the array and just do something like:

                 

                for(var a in arena){

                if(typeof(arena[a]=="movieclip"){

                arena[a].swapDepths(Math.ceil(arena[a]._y*20));

                }

                }