添加/补间/删除 - 同一 MC 的多个实例 (Tweenlite)

发布于 2024-08-10 17:44:18 字数 1663 浏览 7 评论 0原文

我正在尝试创建一个简单的循环,添加随机数量的星星,淡出它们并删除它们。

我想出的脚本做了所有事情,但删除了它们,也许我需要更少的向舞台添加孩子。

这就是我想出的方法,

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();

我需要一种方法使影片剪辑独一无二,这样我就可以告诉我的 oncomplete 函数删除属性剪辑,如果我不这样做,影片最终会因为太多(看不见的)而变慢并崩溃) 影片剪辑。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-08-17 17:44:18

将 MovieClip 作为 onComplete 函数的参数传递:

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

function removeStar(mc:MovieClip):void
{
    if (contains(mc))
    {
        removeChild(mc);
    }
}

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);
    }
}
九局 2024-08-17 17:44:18

这里注意到一个错误:

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

每次循环时都会调用 2 到 7 之间的随机数。您将偏向 1 或 2 颗星,而不是 5 或 6 颗星。(从 randomNumber 传递的值减少 1,因为您的索引从 1 而不是 0 开始)我想您不是故意这样做的?

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

可能更接近您的预期工作方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文