Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with Flash CS3 Particle Effect Tutorial Code

Explorer ,
Jul 18, 2013 Jul 18, 2013

I did the tutorial from http://www.schoolofflash.com/2008/03/flash-cs3-particle-effect/. I used a star to replace the circle. Here's my code:

var starsArray:Array = new Array();

var maxStarss:Number = 8;

function addStars(e:Event)

{

    var stars:Stars = new Stars();

    stars.x = stage.stageWidth/2;

    stars.y = stage.stageHeight/2;

    stars.alpha = Math.random() * .8 + .2;

    stars.scaleX = stars.scaleY = Math.random() * .8 + .2;

    stars.xMovement = Math.random() * 10 - 5;

    stars.yMovement = Math.random() * 10 - 5;

    starsArray.push(stars);

    addChild(stars);

    stars.cacheAsBitmap = true;

    if (starsArray.length >= maxStarss)

    {

        removeChild(starsArray.shift());

    }

    stars.addEventListener(Event.ENTER_FRAME,moveStars);

}

function moveStars(e:Event)

{

    e.currentTarget.x += e.currentTarget.xMovement;

    e.currentTarget.y += e.currentTarget.yMovement;

}

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars);

myTimer.start();

This time, I'm trying to make the stars shrink and transparent as they move away from the point. So I coded it like this:

import flash.events.Event;

var starsArray:Array = new Array();

var maxStarss:Number = 8;

function addStars(e:Event)

{

    var stars:Stars = new Stars();

    stars.x = stage.stageWidth/2;

    stars.y = stage.stageHeight/2;

    stars.alpha = Math.random() * .8 + .2;

    stars.scaleX = stars.scaleY = Math.random() * .8 + .2;

    stars.xMovement = Math.random() * 10 - 5;

    stars.yMovement = Math.random() * 10 - 5;

    starsArray.push(stars);

    addChild(stars);

    stars.cacheAsBitmap = true;

    if (starsArray.length >= maxStarss)

    {

        removeChild(starsArray.shift());

    }

    stars.addEventListener(Event.ENTER_FRAME,moveStars);

    stars.addEventListener(Event.ENTER_FRAME,animeStars);

}

function animeStars(e:Event)

{

    trace(this.starsArray);

    this.scaleX-=0.01;

    this.scaleY-=0.01;

    this.alpha-=0.01;

    if (this.alpha<=0) // remove this object from stage

}

function moveStars(e:Event)

{

    e.currentTarget.x += e.currentTarget.xMovement;

    e.currentTarget.y += e.currentTarget.yMovement;

}

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars, animeStars);

myTimer.start();

I couldn't make it work. All I got was an error message saying "


1084: Syntax error: expecting identifier before rightbrace.

Help?

TOPICS
ActionScript
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 21, 2013 Jul 21, 2013

Try this:

var starsArray:Array = new Array();

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars);

myTimer.start();

addEventListener(Event.ENTER_FRAME, moveStars);

function addStars(e:TimerEvent):void

{

          var star:Stars = new Stars();

          star.x = stage.stageWidth / 2;

          star.y = stage.stageHeight / 2;

          star.alpha = Math.random() * .8 + .2;

          star.scaleX = star.scaleY = Math.random() * .8 + .2;

          star.xMovement = Math.random()

...
Translate
Enthusiast ,
Jul 18, 2013 Jul 18, 2013

Flash is complaining about the unfinished If statement

if (this.alpha<=0) // remove this object from stage

should be

if(this.alpha<=0){

     //remove this object from stage

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 18, 2013 Jul 18, 2013

I did what you said but I got another error:

Warning: 3553: Function value used where type Boolean was expected.  Possibly the parentheses () are missing after this function reference.

Did you know that you can download the flash file at the end of the tutorial (http://www.schoolofflash.com/2008/03/flash-cs3-particle-effect/4/) in order to look at the code?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 18, 2013 Jul 18, 2013

It is because of this line of code at the end

myTimer.addEventListener(TimerEvent.TIMER, addStars, animeStars);

if you read the documentation on addeventlistener it looks for these type of arguments to be passed

addEventListener(type:String, listener:Function, useCapture:Boolean);

animeStars is not a boolean it is a function. Since you already have animeStars in your addStars function you can exclude it in the last event listerer. try this:

myTimer.addEventListener(TimerEvent.TIMER, addStars);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 18, 2013 Jul 18, 2013

Something went wrong.

When I tried to test the movie, it didn't show the result but went to the "output" screen saying,"[object Stars],[object Stars],[object Stars],[object Stars],[object Stars],[object Stars]" and so on...

I think it has something to do with the placement of "stars.addEventListener(Event.ENTER_FRAME,animeStars);" or maybe it's because there are two alphas, two scaleX, and two scaleY. Not sure...

Thanks for your explanation about addeventlistener. I'm still learning Actionscript 3. It's really different from actionscript 2 and also I really didn't use flash that much....I'm planning to use it for a flash banner and after I resolve the problem, I'm going to try and put this on motion path or something like that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 19, 2013 Jul 19, 2013

Hello? I don't want to brother you if you are busy. Still, I haven't solved my problem yet.

When I tried to test the movie, it didn't show the result but went to the "output" screen saying,"[object Stars],[object Stars],[object Stars],[object Stars],[object Stars],[object Stars]" and so on...

I think it has something to do with the placement of "stars.addEventListener(Event.ENTER_FRAME,animeStars);" or maybe it's because there are two alphas, two scaleX, and two scaleY. Not sure...

I appreciate any help that you offers.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2013 Jul 20, 2013

To begin with you should not use this tutorial as is - it promotes extremely bad inefficiencies. The top two are:

  1. It uses a lot of ENTER_FRAME listeners;
  2. It does not remove listeners from unused objects.

Code below make it much more efficient.

import flash.display.MovieClip;

var starsArray:Array = new Array();

var maxStarss:Number = 8;

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars);

myTimer.start();

addEventListener(Event.ENTER_FRAME, moveStars);

function addStars(e:Event):void

{

          var stars:Stars = new Stars();

          stars.x = stage.stageWidth / 2;

          stars.y = stage.stageHeight / 2;

          stars.alpha = Math.random() * .8 + .2;

          stars.scaleX = stars.scaleY = Math.random() * .8 + .2;

          stars.xMovement = Math.random() * 10 - 5;

          stars.yMovement = Math.random() * 10 - 5;

          starsArray.push(stars);

          addChild(stars);

          stars.cacheAsBitmap = true;

 

          if (starsArray.length >= maxStarss)

          {

                    removeChild(starsArray.shift());

          }

}

function moveStars(e:Event):void

{

          for each (var star:MovieClip in starsArray)

          {

                    star.x += star.xMovement;

                    star.y += star.yMovement;

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2013 Jul 20, 2013

And your second iteration that changes scale an alpha should be something like this:

import flash.display.MovieClip;

var starsArray:Array = new Array();

var maxStarss:Number = 8;

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars);

myTimer.start();

addEventListener(Event.ENTER_FRAME, moveStars);

function addStars(e:Event):void

{

          var stars:Stars = new Stars();

          stars.x = stage.stageWidth / 2;

          stars.y = stage.stageHeight / 2;

          stars.alpha = Math.random() * .8 + .2;

          stars.scaleX = stars.scaleY = Math.random() * .8 + .2;

          stars.xMovement = Math.random() * 10 - 5;

          stars.yMovement = Math.random() * 10 - 5;

          starsArray.push(stars);

          addChild(stars);

          stars.cacheAsBitmap = true;

          if (starsArray.length >= maxStarss)

          {

                    removeChild(starsArray.shift());

          }

}

function moveStars(e:Event):void

{

          for each (var star:MovieClip in starsArray)

          {

                    star.x += star.xMovement;

                    star.y += star.yMovement;

          }

          this.scaleX -= 0.01;

          this.scaleY -= 0.01;

          this.alpha -= 0.01;

          if (this.alpha <= 0)

          {

                    removeEventListener(Event.ENTER_FRAME, moveStars);

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2013 Jul 20, 2013

Well, this code is great except for one tiny thing.

the whole screen shrink and disappear, not just the stars itselves that are supposed to fade away and shrink as they move away from the point......

do I change "this." to "stars."? Except when I do this, I get this error, "1120: Access of undefined property stars.".

There are another way I'm kinda thinking of doing. Double-click on the star to go inside the stars' movie clip's timeline and then try to shrink and alpha the star inside the timeline, using either a classic tween or motion tween. What do you think?

Thanks for the code!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2013 Jul 21, 2013

Try this:

var starsArray:Array = new Array();

var myTimer:Timer = new Timer(50);

myTimer.addEventListener(TimerEvent.TIMER, addStars);

myTimer.start();

addEventListener(Event.ENTER_FRAME, moveStars);

function addStars(e:TimerEvent):void

{

          var star:Stars = new Stars();

          star.x = stage.stageWidth / 2;

          star.y = stage.stageHeight / 2;

          star.alpha = Math.random() * .8 + .2;

          star.scaleX = star.scaleY = Math.random() * .8 + .2;

          star.xMovement = Math.random() * 10 - 5;

          star.yMovement = Math.random() * 10 - 5;

          starsArray.push(star);

          addChild(star);

          star.cacheAsBitmap = true;

}

function moveStars(e:Event):void

{

          for each (var star:MovieClip in starsArray)

          {

                    star.x += star.xMovement;

                    star.y += star.yMovement;

                    star.alpha -= .01;

                    star.scaleX = star.scaleY -= .01;

                    if (star.scaleX <= 0)

                              kill(star);

          }

}

function kill(star:MovieClip):void

{

          starsArray.splice(starsArray.indexOf(star), 1);

          removeChild(star);

}

Message was edited by: Andrei1

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2013 Jul 21, 2013

It's working now!!! Thanks sooo much!

I have one last question: How do I control the speed? is it the "var myTimer:Timer = new Timer(50);" or what?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2013 Jul 21, 2013

Speed is controlled by setting xMovement and yMovement.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2013 Jul 21, 2013

Are you sure? No matter what I tried to adjust, the stars' speed seems to shoot off in many direction rapidly and a bit fast instead of slowing down.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 21, 2013 Jul 21, 2013

Did you attempt to understand what code does?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2013 Jul 21, 2013

All I know is that xMovement and yMovement tells the star to go to certain point on a grid (graph), like if you wanted it to go to x=5 and y= 12, the star will go to 5 steps right and 12 steps down. I did a lot of experiments with xMovement and yMovement in order to understand what xMovement and yMovement are and yet I don't see any difference in the speed, just where the star would go to.....

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2013 Jul 22, 2013

Show examples of how you attempted to manipulate x and y Movements.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 22, 2013 Jul 22, 2013

Examples:

1.To understand what x and y Movements are in its basic component, I choose single numbers:

   star.xMovement = 5;

   star.yMovement = 5;

          -It goes right and down

2. star.xMovement = 10;

    star.yMovement = 10;

          -it goes further right and down

3. star.xMovement = 10;

    star.yMovement = -15;

          -it goes further right and up

4. star.xMovement = Math.random() * 10 - 5;

    star.yMovement = Math.random() * 10 - 5;

          -Math.random and 10 - 5 means that the star will get a random number between -5 and +5 of an distance to go to.

5. Additionally, 40 - 20 means longer range while 4 - 2 means the shortest range.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2013 Jul 23, 2013

All examples definitely change speed. I am not sure what you refer to when you say that that you do not observe differences in speed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 23, 2013 Jul 23, 2013

Then what numbers are the one that slow down the speed of the star as it moves from point A to point B?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2013 Jul 23, 2013
LATEST

If you assigned small values - speed will decrease. For example:

star.xMovement = 1;

star.yMovement = 0.5;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 21, 2013 Jul 21, 2013

Additionally, from what I understand from my experienments, I can kinda control the production rate of the stars with the Timer and how long the star disappear with the alpha.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2013 Jul 20, 2013

Additionally, I think I'm going to remove the

stars.alpha = Math.random() * .8 + .2;stars.scaleX = stars.scaleY = Math.random() * .8 + .2;

because, I only wanted it to scale down and fade away as it move away from the point.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines