添加/补间/删除 - 同一 MC 的多个实例 (Tweenlite)
我正在尝试创建一个简单的循环,添加随机数量的星星,淡出它们并删除它们。
我想出的脚本做了所有事情,但删除了它们,也许我需要更少的向舞台添加孩子。
这就是我想出的方法,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 MovieClip 作为 onComplete 函数的参数传递:
Pass the MovieClip as a parameter of the onComplete function:
这里注意到一个错误:
每次循环时都会调用 2 到 7 之间的随机数。您将偏向 1 或 2 颗星,而不是 5 或 6 颗星。(从 randomNumber 传递的值减少 1,因为您的索引从 1 而不是 0 开始)我想您不是故意这样做的?
可能更接近您的预期工作方式。
Noticed a bug here:
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?
is probably closer to working how you intended.