0
import flash.display.MovieClip;
import flash.events.Event; 


public class rainfall extends MovieClip {


    public function rainfall() {
        // rainfall



        var i:int;
        for (i = 0; i< 50; i++)

        {  
           //variables
            var mc:MovieClip = new MovieClip ();



                //theStage, and alpha properties
                mc.x  = Math.random() * stage.stageWidth ;
                mc.y = Math.random() * 400 * 4 ;
                mc.alpha = Math.random()* 2;
            mc.graphics.beginFill(0x0000FF);
            mc.graphics.drawCircle(0,0,20);
                //trace
                trace(i);


            addChild(mc);
            mc.addEventListener(Event.ENTER_FRAME, moveDown) ;
        }

                    function moveDown(e:Event):void
                { //fall speed
                        e.target.y += 1 ;
                }

            }

在试图弄清楚如何让圆圈在连续循环中在屏幕上重复出现很多麻烦时,我对 actionscript 3 还很陌生,但是关于我做错了什么或我需要让它循环播放的任何提示

4

1 回答 1

0

尝试这样的事情:

import flash.display.Sprite;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.events.Event; 


package{

    public class Rainfall extends Sprite {//class names should be title case

        private var numDrops = 50;//this make it easier to configure
        private var dropRadius = 20;
        private var maxY;

        public function Rainfall() {
            addEventListener(Event.ADDED_TO_STAGE,init);//make sure the stage property will not be null
        }

        private function init(event:Event):void{
            maxY = stage.stageHeight;//getters can be slow, store the height so it can be reused for each drop reset
            var i:int;
            for (i = 0; i < numDrops; i++){  
               var mc:Shape = new Shape();//if no interactivity is needed, Shape is the simplest/lightest class to use for drawing
                mc.x  = Math.random() * stage.stageWidth ;
                mc.y = Math.random() * 400 * 4 ;
                mc.alpha = 0.1 + Math.random() * 0.9;//alpha values are from 0 to 1.
                mc.graphics.beginFill(0x0000FF);
                mc.graphics.drawCircle(0,0,dropRadius);
                addChild(mc);
            }
            addEventListener(Event.ENTER_FRAME, moveDown) ;
        }
        private function moveDown(e:Event):void
        { //fall speed
            for(var i:int = 0 ; i < numDrops; i++){
                var drop:DisplayObject = getChildAt(i);
                drop.y += 1 ;
                if(drop.y > maxY) drop.y = -dropRadius;//if the drop exits the screen downards, place it back at the top (above the visible area)
            }
        }

    }

}

它没有经过测试的代码,因此您可能会遇到语法错误,但这些想法已被注释:

您需要为您的问题使用条件:如果水滴的垂直位置大于舞台的高度,则水滴的垂直位置应重置回舞台的顶部(`if(drop.y > maxY) drop.y = -dropRadius;a)

如果您刚刚开始,虽然没有必要,但这里有一些关于在 Flash 中工作时的效率/速度的提示:

  1. MovieClip 是一个动态类(您可以动态向实例添加属性),但也有成本。由于您只需要渲染/绘制元素并且不需要事件或子元素,因此这使您的圆圈非常适合使用 Shape 类。此外,您的主类可以是 Sprite,因为您没有使用时间线
  2. getter 和 setter 在 actionscript 中可能有点慢。缓存/存储随时间变化不大的值以作为局部变量重用以加快访问速度是一种健康的习惯。
  3. 没有那么多性能问题:一个常见的缺陷是没有在显示对象上初始化舞台,这会导致令人讨厌的空对象引用错误,并且让初学者感到困惑。如果您在 DisplayObject(Shape/Sprite/MovieClip) 上使用舞台属性,最好确保使用ADDED_TO_STAGE事件将其添加到舞台(并且舞台属性不为空)

祝你好运!

于 2013-10-21T05:57:47.023 回答