11 Replies Latest reply on Sep 15, 2010 6:50 AM by kglad

    How to pass parameters or values trough duplicate movie?

    Radu Cristian Dimitrie

      Hi,

       

      I am creating a game in Flash 8 with AS 2 and i have few questions to ask.

       

      First of all, i am stuck for 2 days at creating a Laser Beam from tower to the target.

      I searched all internet for solutions but i can't really find one, so i'll try posting questions myself..

      First i tryed to create and effectHolder into which every laser beam was created, because it seems i can create only one line in each movieClip..

       

      _root.duplicateMovie(effectHolder_mc , "effectHolder_" + this, _root.getNextHighestDepth());
      v = a = _root["effectHolder_" + this];
      ray.onEnterFrame = function()
      {
      v.clear();
      v.lineStyle(1,0xFF0000,100);
      v.moveTo(this._x, this._y);
      v.lineTo(creep._x , creep._y);
      v.text = "True"

      }

       

      This was triggered eveytime it had a target.

      With every tower that fired a laser i created it was getting more lag, so i tryed selfdestructing after few time.

       

      this._alpha -= 10;

      if (this._alpha <= 0)

      {

           this.removeMovieClip();

      }

       

      But it didn't actually work, so i tryed another solution. I tryed to give each MovieClip ("Laser") i duplicate a code that creates the laser beam.

       

      onClipEvent(load)
      {
           this.fx = from._x;
           this.fy = from._y;
           this.tx = to._x;
           this.ty = to._y;
      }
      onClipEvent(enterframe)
      {
           _root.dmg.text = "From: " + fx + "," + fy + " To: " + tx + "," + ty; //I used this on a textbox to see if parameters are transmited. But they were all 'undefined'

              this.clear();
           this.lineStyle(1,0xFF0000,100);
           this.moveTo(from._x, from._y);
           this.lineTo(to._x , to._y);

           this._alpha -= 4;
           if (this._alpha <= 0)
           {
                this.removeMovieClip();
           }
                                   
      }

       

      The problem is that i didn't know how to pass the coordonates of the tower and the target to the MovieClip so it knows where to create the line.

       

      X++;
      duplicateMovieClip(_root.Laser, "Laser" + X, X,{from._x,from._y,to._x,to._y});         
      g = _root[_root.Laser + X];
      g.fx = from._x;
      g.fy = from._y;
      g.tx = to._x;
      g.ty = to._y;

       

      Please leave any suggestions of how to send the parameters or the values to the created movie clip, or any idea of how to create that line ("laser beam") for each tower, without lagging so much.

       

      Thanks,

      Chris

        • 1. Re: How to pass parameters or values trough duplicate movie?
          kglad Adobe Community Professional & MVP

          is effectHolder_mc a multiframe movieclip?  if yes, should a duplicate be removed when its last frame plays?

           

          if no to either question, when should a duplicate of effectHolder_mc be removed?

          1 person found this helpful
          • 2. Re: How to pass parameters or values trough duplicate movie?
            Radu Cristian Dimitrie Level 1

            no, effectHolder_mc it's one frame empty movieclip..

            i tryed to remove it when Alpha was 0,  it did remove the beam, but with each tower placed lag was increased..

            this is one of my last attempts:

             

            if (d < range)
            {
            target = 1;
            creep = _root.creepArray[i];
            _rotation = Math.atan2(creep._y - this._y, creep._x - this._x) * 180 / 3.141593E+000;
            _root.createEmptyMovieClip("laserbeam" + cname, _root.getNextHighestDepth());
            v = _root["laserbeam" + cname];
            v.onEnterFrame = function()
                 {
                 v.clear();
                 v.lineStyle(3,0xFF0000,100);
                 v.moveTo(cx, cy);
                 v.lineTo(creep._x , creep._y);
                 //v._alpha -= 20
                 //if ( v._alpha <= 0)
                 //{

                      //this.removeMovieClip();
                 //}
                 }
            }

             

            If d ("Distance from tower to target) is smaller than the range it will make the current target the creep from the array andit will rotate toward it..

            Then an empty movie clip is created with "laserbeam" + cname (towername), because i wanted to create the movie clip for this tower only, but i don't know if it will create more duplicates or just one because it has same movieclip name. Removing the movie clip after the alpha is 0 won't solve the lag.

            I also noticed that with time passed the lag increases constant. I don't know what to do: to create all the laser beams for all the towers in just one movieclip or to make each selfdestruct, but i don't know where or when to place the code to remove, because it might not have time even to react, to see a visible laser beam..

             

            EDIT:

             

            I added this to remove the duplicate automatically:

             

            lasercount++;
            if (lasercount > 1)
            {
            _root.dmg.text = lasercount;
            removeMovieClip("laserbeam" + cname +(lasercount - 1));
            lasercount = 0
            }

            but it won't remove the lag created by the increased number of towers..

            • 3. Re: How to pass parameters or values trough duplicate movie?
              kglad Adobe Community Professional & MVP

              what's triggering v's _alpha to decrease?

              • 4. Re: How to pass parameters or values trough duplicate movie?
                Radu Cristian Dimitrie Level 1

                I managed to solve the problem with the laser.. works perfectly.. almost...

                The code was creating a new movieclip everytime the distance between tower and target was enough..

                But now, i changed it, so it creates for each tower a separate movieclip..

                In the past posts, i couldn't place more than 10 towers..

                Now i can put about 60, and from 60fps drops to 20fps..

                 

                I can't make the laser beam fire from movieclip of the tower because it will rotate at same time, while it targets a creep.. Now i need a solution to make it stop lagging so much.. do you have any idea for creating this laser beam [line] without creating a new movie clip for each Tower? or how to speed up the process so i don't end up with 1/3 fps or less..

                • 5. Re: How to pass parameters or values trough duplicate movie?
                  kglad Adobe Community Professional & MVP

                  if you're adding a lot of movieclips, you will cause a perfomance problem.

                   

                  how many movieclips do you need at any one time?

                  • 6. Re: How to pass parameters or values trough duplicate movie?
                    Radu Cristian Dimitrie Level 1

                    could be even hundreds..

                    the towers will stay in place until they are sold or the game is over..

                     

                    Duplicating an already existing movieclip, instead of creating a new one could reduce the lag?

                    the biggest problem seems to be with creation of the line (laser beam), because it need a new movieclip everytime..

                    i might have another idea.. about creating a movieclip with a line being its diagonal.. then  put one end at the starting of the tower and the next at the targeted creep..although i like programming, math hasn't been my best.. i am an architecture student..

                     

                    When a new symbol is created, there's an option that can be checked 'Export for ActionScript'.. will it help more if i check the box, than using it in ActionScript without it?

                    • 7. Re: How to pass parameters or values trough duplicate movie?
                      kglad Adobe Community Professional & MVP

                      if you have hundreds of movieclips you should check if enabling their cacheAsBitmap property will help performance.  you should also consider using as3 where performance is significantly better.

                      1 person found this helpful
                      • 8. Re: How to pass parameters or values trough duplicate movie?
                        Radu Cristian Dimitrie Level 1

                        Some posts on internet say that if i use cacheAsBitmap  my performance will decrease if my object is moving, rotating, etc..

                        I don't know if i can export current AS2 code to AS3.. rewriting it will cause more headache

                         

                        I did another test, but now without the lasers or the movieclips duplicated..

                        It seems the towers themselves create the lag, not the actual laserbeam..

                        Frame rate is the same as the one with lasers on..

                        So there is something in my code that needs to be cleaned..


                        But i don't know what..

                        This is the code placed inside each tower :

                         

                        onClipEvent (load)
                        {
                            title = "Laser Tower [Cost: $85]";
                            desc = "Constant attack speed with low damage and a low range";
                            type = "Ground";    
                            
                             //v = _root.createEmptyMovieClip("laserbeam" + this, _root.getNextHighestDepth());
                            //b = duplicateMovieClip(_root.LaserBeam, "laserbeam" + this, _root.getNextHighestDepth());
                            
                             creep = "";    
                             cost = 0;
                            
                            target = 0;
                            
                             //Tower Parameters
                             basicdamage = 1;
                             damage = basicdamage + basicdamage * _root.dmgmulti/100;
                             basicrange = 100;
                             range = basicrange + basicrange * _root.radiusmulti/100;
                             basicrate = 0;
                            rate = basicrate - basicrate * _root.ratemulti/100;
                             laserWidth= 2;
                             laserColor = "Red";
                             splash = 0;
                             slow = 0;
                            
                             level = 1;
                             //Parameters: Description, Cost, Damage, Range, Rate, Width, Color, Splash, Slow
                             upgrades = [["-", 20, 40, 80, 0, 4, Red, 0, 0],
                                            ["-", 30, 75,100, 0, 6, Red, 0, 0],
                                            ["-", 55,150,120, 0, 8, Red, 0, 0],
                                            ["-", 85,250,140, 0,10, Red, 0, 0]];    
                        }
                        on (press)
                        {
                             _root.towerInfo(this);
                        }

                        onClipEvent (enterFrame)
                        {
                             damage = basicdamage + basicdamage * _root.dmgmulti/100;
                             range = basicrange + basicrange * _root.radiusmulti/100;    
                             rate = basicrate - basicrate * _root.ratemulti/100;
                             if (active == 1  && _root.creepArray.length > 0)
                            {
                                  //v.clear()
                                  _root.towerInfo(_root.selectedTower);
                                for (i = 0; i <= _root.creepArray.length; i++)
                                        {
                                                 d = Math.sqrt(Math.pow(this._x - _root.creepArray[i]._x, 2) +
                                                                  Math.pow(this._y - _root.creepArray[i]._y, 2));
                                                 if (d < range)
                                                 {
                                                      target = 1;
                                                      creep = _root.creepArray[i];
                                                      _rotation = Math.atan2(creep._y - this._y, creep._x - this._x) *
                                                                                                             180 / 3.141593E+000;
                                                                  //v.clear();
                                                                   //v.lineStyle(1,0xFF0000,100);
                                                               //v.moveTo(this._x, this._y);
                                                                //v.lineTo(creep._x , creep._y);
                                                                //v.dmg.text = "True";

                                                      creep.health -= damage;
                                                      break;
                                                 } //End If

                                            } //End For
                             } //End If
                        }

                         

                        i stopped the rotation of the tower and the stats updater.. and still, no effect.. i came to the conclusion, that simply the fact of searching for the target every frame, makes it lag..

                        • 9. Re: How to pass parameters or values trough duplicate movie?
                          kglad Adobe Community Professional & MVP

                          cacheAsBitmap works wll for moving movieclips but not for rotating ones.

                           

                          you probably don't want an enterframe loop in each tower.  one loop that repeatedly updates your game is more efficient.

                           

                          in addition, you might want to check on using a proximity manager for your game where each creep is assigned to a grid element.

                          • 10. Re: How to pass parameters or values trough duplicate movie?
                            Radu Cristian Dimitrie Level 1

                            wow! Thanks alot!

                            you really helped me