7 Replies Latest reply on Oct 27, 2006 1:38 PM by Mister Peanut

    Spark Effect

    Mister Peanut Level 1
      Hi, I recently found a post which gave some code for a spark effect. I cannot get it to work and was wondering if anyone could help me with it. this is the code I used:

      temp = 1;
      while (temp <= _parent.dustinstances) {
      current = random(1000);
      duplicateMovieClip (_parent.spark, "spark" add current, current);

      if (_parent.randomrotation <> 0) {
      _parent["spark" add current]._rotation = random(_parent.randomrotation);
      }
      else {
      _parent["spark" add current]._rotation = _parent.rotation;
      }

      _parent["spark" add current]._x = _x;
      _parent["spark" add current]._y = _y;

      _parent["spark" add current].xspeed = (random(_parent.xspeed)-3)+1;
      _parent["spark" add current].yspeed = random(_parent.yspeed)*-1;
      _parent["spark" add current].zspeed = random(_parent.zspeed)-5;
      _parent["spark" add current].fade = random(5)+3;
      temp++;
      }

      these are the errors I got:

      **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: ')' or ',' expected
      duplicateMovieClip (_parent.spark, "spark" add current, current);

      **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: ']' expected
      _parent["spark" add current]._rotation = random(_parent.randomrotation);

      **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: Unexpected '}' encountered
      }


      I am new to this, so I don't understand how to fix the problem.

      P
        • 1. Re: Spark Effect
          Mister Peanut Level 1
          Sorry, I forgot to mention that I was looking for a welding-torch effect. I have seen some tutorials out there with animated (cartoony) effects, but I'd like to find something more realistic.

          Also, I am looking for an effect that would be like a glow, but using light beams instead of a uiform glow, as if a car's headlights were shining from behind an object, and you sam rays of light all around in different intensities.
          • 2. Re: Spark Effect
            Level 7
            >>"spark" add current

            add is deprecated. Use "spark" + current

            --
            Dave -
            Head Developer
            www.blurredistinction.com
            Adobe Community Expert
            http://www.adobe.com/communities/experts/


            • 3. Re: Spark Effect
              Mister Peanut Level 1
              Thanks for the reply. I have tried what you suggested, but now I get an error that says:

              A script in this movie is causing Flash Player to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?

              ...so something isn't right still. Any suggestions?

              Cheers,

              P
              • 4. Re: Spark Effect
                Mister Peanut Level 1
                I have altered the code to the following, and it seems to work fine.

                // Number of sparks
                var total:Number = 60;

                // Strength of gravity....higher number makes gravity stronger
                var gravity:Number = 1;

                // Conversion necessary to get radians from degress
                var degreesToRadians:Number = Math.PI / 180;

                // Method that creates the sparks clip
                function createSparks():MovieClip
                {
                // Create the clip that contains the sparks
                var depth:Number = this.getNextHighestDepth();
                var ph:MovieClip = this.createEmptyMovieClip("ph" + depth, depth);

                // Loop thru to create each spark clip
                for (var i = 0; i < total; i++)
                {
                // Create and draw spark
                var name:String = "spark" + i;
                var mc:MovieClip = ph.createEmptyMovieClip(name, i);
                mc.lineStyle(3, 0xEB8914, 100);
                mc.lineTo(0.15, 0);

                // Figure out direction and speed of each spark clip
                var degrees:Number = Math.random() * 360;
                var radians:Number = degrees * degreesToRadians;
                var speed:Number = Math.ceil(Math.random() * 5) + 5;

                // Set values to spark clip
                mc.xVel = speed * Math.cos(radians);
                mc.yVel = speed * Math.sin(radians);

                // Set onEnterFrame event so it animates
                mc.onEnterFrame = move;
                }

                // Return reference of the clip containing the sparks
                return ph;
                }

                // Method called by onEnterFrame event
                function move()
                {
                // Update position of spark clip
                this._x += this.xVel;
                this._y += this.yVel;

                // Update y velocity due to gravity
                this.yVel += gravity;

                // Set alpha
                this._alpha -= Math.ceil(Math.random() * 5);

                // Get rid of parent clip once all sparks have faded out
                if (this._alpha <= 0)
                {
                this._parent.removeMovieClip();
                }
                }

                // Happens when you click on stage
                onMouseUp = function ()
                {
                // Create sparks
                var sparks:MovieClip = createSparks();

                // Position to mouse
                sparks._x = _xmouse;
                sparks._y = _ymouse;
                };

                I would, however, still appreciate some more help as I am trying to use attachMovie to attach a spark of my own design. Can anyone help me with what to do with my spark_mc? I believe it will work in place of:

                // Create and draw spark
                var name:String = "spark" + i;
                var mc:MovieClip = ph.createEmptyMovieClip(name, i);
                mc.lineStyle(3, 0xEB8914, 100);
                mc.lineTo(0.15, 0);

                But I've no idea how to do this. Looking at attachMovie in the LiveDocs doesn't help me much on this, as I just don't get how to apply it to this case.

                P
                • 5. Re: Spark Effect
                  Mister Peanut Level 1
                  I have used the following code:

                  this.attachMovie("spark", "spark_mc", this.getNextHighestDepth());
                  this.attachMovie("spark", "spark_mc", this.getNextHighestDepth(), {_x:100, _y:100});
                  var name:String = "spark" + i;
                  var mc:MovieClip = ph.attachMovie("spark", name, i);

                  however, I get 2 instances of my spark on the screen when I press the button that stays there. Any ideas why?
                  • 6. Spark Effect
                    Mister Peanut Level 1
                    .

                    • 7. Re: Spark Effect
                      Mister Peanut Level 1
                      okay, good news. I've got everything working. The spark is in its own movieclip, and working independently. It looks like someone is welding something. Looks really cool.

                      I still need help, though, with making this spark video loop so that it is not continually generating new sparks. This is causing all of my animations to slow down to turtle speed. Here is the code that I'm using for the spark generator, which is loaded into an MC container:

                      // Number of sparks
                      var total:Number = 5;

                      // Strength of gravity....higher number makes gravity stronger
                      var gravity:Number = .5;

                      // Conversion necessary to get radians from degress
                      var degreesToRadians:Number = Math.PI / 180;

                      // Method that creates the sparks clip
                      function createSparks():MovieClip
                      {
                      // Create the clip that contains the sparks
                      var depth:Number = this.getNextHighestDepth();
                      var ph:MovieClip = this.createEmptyMovieClip("ph" + depth, depth);

                      // Loop thru to create each spark clip
                      for (var i = 0; i < total; i++)
                      {
                      // Create and draw spark
                      this.attachMovie("spark", "spark_mc", this.getNextHighestDepth());
                      this.attachMovie("spark", "spark_mc", this.getNextHighestDepth(), {_x:100, _y:100});
                      var name:String = "spark" + i;
                      var mc:MovieClip = ph.attachMovie("spark", name, i);

                      //var name:String = "spark" + i;
                      //var mc:MovieClip = ph.createEmptyMovieClip(name, i);
                      //mc.lineStyle(3, 0xEB8914, 100);
                      //mc.lineTo(0.15, 0);

                      // Figure out direction and speed of each spark clip
                      var degrees:Number = Math.random() * 360;
                      var radians:Number = degrees * degreesToRadians;
                      var speed:Number = Math.ceil(Math.random() * 3) + 3;

                      // Set values to spark clip
                      mc.xVel = speed * Math.cos(radians);
                      mc.yVel = speed * Math.sin(radians);

                      // Set onEnterFrame event so it animates
                      mc.onEnterFrame = move;
                      }

                      // Return reference of the clip containing the sparks
                      return ph;
                      }

                      // Method called by onEnterFrame event
                      function move()
                      {
                      // Update position of spark clip
                      this._x += this.xVel;
                      this._y += this.yVel;

                      // Update y velocity due to gravity
                      this.yVel += gravity;

                      // Set alpha
                      this._alpha -= Math.ceil(Math.random() * 15);

                      // Get rid of parent clip once all sparks have faded out
                      if (this._alpha <= 0)
                      {
                      this._parent.removeMovieClip();
                      }
                      }
                      // original code was onMouseUp
                      // Happens when you click on stage
                      onEnterFrame = function ()
                      {
                      // Create sparks
                      var sparks:MovieClip = createSparks();
                      //sparks.stage.align = "C";

                      // Position to mouse
                      //sparks._x = _xmouse;
                      //sparks._y = _ymouse;
                      };


                      Thanks for any input.

                      P