English 中文(简体)
Add/Tween/Remove - Multiple instances of the same MC (Tweenlite)
原标题:

I m trying to create a simple loop that adds a random number of stars, fades them out and removes them.

The script I ve come up with does everything but remove them, and perhaps I need a less on adding children to a stage.

Here s what I come up with

import flash.display.*;
import com.greensock.*;
import com.greensock.easing.*;

// timer setup
var timer:Timer=new Timer(randomNumber(500,1000));
timer.addEventListener(TimerEvent.TIMER,run_stars);
timer.start();

// Random number generator
function randomNumber(low:Number=NaN, high:Number=NaN):Number {
  var low:Number = low;
  var high:Number = high;
  if(isNaN(low)) { throw new Error("no low number"); }
  if(isNaN(high)) { throw new Error("no high number"); }
  return Math.round(Math.random() * (high - low)) + low;
}

// randomly adding stars on timer
function run_stars(event:TimerEvent):void {
    // random num of stars
    for (var i:Number=1; i<=randomNumber(2,7);i++) {

            var star:m_star = new m_star();
            addChild(star);

            // This is where my problem starts, I m adding the same movie clip multiple times without any way to identify and remove. 


            star.x = randomNumber(0, stage.stageWidth);
            star.y = randomNumber(0,stage.stageHeight/2);

            TweenLite.to(star, randomNumber(0.5,1), {alpha:0.25, onComplete:removeStar()});

    }

    timer.delay = randomNumber(500,1000);
    timer.start();
}

function removeStar() {
    removeChild(star);
    //this would be where I attempt to remove a star but because they aren t unique it will never work, and the star movie clip is called inside of the function so it cant even see it. 
    }

stop();

I need a way to make the movie clips unique so I can tell my oncomplete function to remove the property clip, if I don t do this the movie will eventually slow down and crash because of so many (invisible) movieclips.

Thanks!

最佳回答

Pass the MovieClip as a parameter of the onComplete function:

TweenLite.to(star, randomNumber(0.5,1), {
    alpha:0.25,
    onComplete:removeStar, 
    onCompleteParams:[star]
});

function removeStar(mc:MovieClip):void
{
    if (contains(mc))
    {
        removeChild(mc);
    }
}
问题回答

Noticed a bug here:

for (var i:Number=1; i<=randomNumber(2,7);i++) {

That s going to call for a random number between 2 and 7 each time it goes through the loop. You will have a skew towards 1 or 2 stars rather than 5 or 6. (reduced by 1 from the value in the passed from randomNumber because you start your index at 1 not 0) I imagine you don t mean to do that?

var len:int = randomNumber(2, 7);
for (var i:int = 1; i <= len; i++) {

is probably closer to working how you intended.





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签