我正在尝试创建一个简单的循环,添加一个随机数量的星星,将它们淡化并删除它们。
我提出的剧本除了删除它们之外什么都做了,也许我需要更少的把孩子添加到舞台上。
这是我想出的
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函数删除属性剪辑,如果我不这样做,电影最终会因为这么多(看不见的)动画片段而减速和崩溃。
谢谢!
答案 0 :(得分:3)
将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);
}
}
答案 1 :(得分:1)
注意到这里的错误:
for (var i:Number=1; i<=randomNumber(2,7);i++) {
每次进入循环时,这将要求一个介于2和7之间的随机数。你会偏向1或2星而不是5或6.(从randomNumber传递的值减1,因为你的索引是1而不是0)我想你不是故意这样做的吗? / p>
var len:int = randomNumber(2, 7);
for (var i:int = 1; i <= len; i++) {
可能更接近你的工作方式。