AS3动态加载和自动滚动随机大小的动画片段

时间:2013-05-02 04:38:00

标签: actionscript-3 flash

我对AS3& Flash并想知道是否有人可以看看这个测试片。

在我的Flash文件中,我有许多随机和放大的动画片段。按下按钮时动态添加到舞台。所有的动画片段都有相同的宽度,但高度不同,所有的动画片段的注册点都位于左下方(我现在只是为了测试目的使用按钮)。

我想做的是'自动'(而不是使用按钮)从图书馆向舞台添加无穷无尽的随机选择的mc(也就是说mc应该连续添加 - 即一个之后其他没有间隙的其他部分)从舞台顶部向下自动滚动向下(以永不停止的传送带的方式),然后当它们在舞台上不再可见时返回到库。

任何人都有任何想法。

//mc's are dynamically loaded & returned to the library
//mc's have 'export for Actionscript' property
//mc's have their anchor point placed bottom left

//stop all
stop();

//Speed of the vertical auto-scroll movement
var scrollSpeed:uint = 1;

//auto load random mc from library & place top left corner of stage

//load random mc via button for test purposes
McButton.addEventListener(MouseEvent.CLICK,attachMovieclip);
function attachMovieclip(event:MouseEvent):void{

//create a random number for choosing a mc from the array
var newNumber:int = (Math.random ()*14)

//define the mc's
var mc1:Red01 = new Red01();
var mc2:Red02 = new Red02();
var mc3:Red03 = new Red03();
var mc4:Orange01 = new Orange01();
var mc5:Orange02 = new Orange02();
var mc6:Orange03 = new Orange03();
var mc7:Yellow01 = new Yellow01();
var mc8:Yellow02 = new Yellow02();
var mc9:Green01 = new Green01();
var mc10:Green02 = new Green02();
var mc11:Blue01 = new Blue01();
var mc12:Blue02 = new Blue02();
var mc13:Purple01 = new Purple01();
var mc14:Purple02 = new Purple02();

//create an array which holds all the mc's
var Mcarray:Array = newArray(mc1,mc2,mc3,mc4,mc5,mc6,mc7,mc8,mc9,mc10,mc11,mc12,mc13,mc14);

//add child (or random mc) to the stage
addChild(Mcarray[newNumber]);

//place mc at specific starting point coordinate - i.e. top of the stage
Mcarray[newNumber].x=0
Mcarray[newNumber].y=0

//trace mc random numeric value for test purposes
trace(newNumber);

//auto-scroll the randomly chosen mc vertically down the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
function moveScroll(e:Event):void{
Mcarray[newNumber].y += scrollSpeed;

//once first mc is completley on stage load the next random mc

//once a mc has completely left the bottom of the stage return it to the library
}
}

2 个答案:

答案 0 :(得分:0)

离开我的头顶所以它可能有点粗糙但是......

从第一个剪辑开始,将其存储在“onscreenClips”数组中(将像队列一样使用):

1。)将“onscreenClips”中的起始剪辑设置为y = -height 这会将剪辑的底部排列到舞台的顶部。

然后在你的输入框架循环中:

1。)以速度

向下移动“onscreenClips”中的任何剪辑

2.)检查“onscreenClips”中的第一个对象是否已到达底部(y属性将等于舞台高度)。如果是这样,请将其从显示屏中移除(因为它现在在屏幕外)并从队列中移除。第一个对象始终是队列中“最旧的”。

3.)检查“onscreenClips”中的最后一个对象是否已到达舞台的顶部(y属性已达到0并且不再为负)。这意味着顶部边缘与舞台的顶部对齐,如果向下移动,则会有间隙。如果发生这种情况,则在y = -height处添加下一个剪辑集,然后将其推送到队列。

4。)继续,直到不再添加任何对象。然后继续检查第2步条件,直到“onscreenClips”数组/队列为空。

答案 1 :(得分:0)

使用这个进一步推进。

添加了检查以确定何时可以看到mc的整个高度& '在舞台上'并且当mc的全高度不可见时& '离开舞台',两者都正常运作。

我还添加了一个删除子语句,以便当mc变得不可见时&在舞台上将它从舞台上移除并返回到图书馆。这在视觉上起作用(即,mc确实从舞台上消失)但是,根据输出结果,mc(尽管不可见)仍然看起来存在。旅行越过可见的舞台限制。