停止动作脚本动画

Posted

技术标签:

【中文标题】停止动作脚本动画【英文标题】:Stopping an actionscript animation 【发布时间】:2013-11-27 02:58:59 【问题描述】:

如何停止基于 actionscript 而不是movieclip 的自动循环动画? 我已经尝试在结束帧上添加一个停止但没有奏效。顺便说一句,我正在使用 actionscript 3.0。

init();
    function init():void 
        displayWidth = 785;
        displayHeight = 285;
        waitCount = 100;
        minWait = 10;
        maxWait = 130;
        count = waitCount - 1;
        flareList = new Particle3DList();
        sparkList = new Particle3DList();
        maxDragFactorFlare = 0.6;
        maxDragFactorSpark = 0.6;
        maxNumSparksAtNewFirework = 3000;
        gravity = 0.03;
        maxNumFlares = 90; 
        maxNumSparksPerFlare = 2; 
        topMargin = 6;
        displayHolder = new Sprite;
        displayHolder.x = 100;
        displayHolder.y = 0;
        sparkBitmapData = new BitmapData(displayWidth, displayHeight, true, 0x00000000);
        sparkBitmap = new Bitmap(sparkBitmapData);  
        var alphaToWhite:Number = 0.5;
        var alphaMult:Number = 1.6;
        var cmf:ColorMatrixFilter = new ColorMatrixFilter([1,0,0,alphaToWhite,0,                                                   0,1,0,alphaToWhite,0,                                                     0,0,1,alphaToWhite,0,                                                         0,0,0,alphaMult,0]);
        sparkBitmap.filters = [cmf];
        sky = new Sprite();
        starLayer = new Sprite();
        starLayer.x = 0;
        starLayer.y = 0;
        starLayer.blendMode = BlendMode.LIGHTEN;
        var k:int;
        var starGray:Number;
        var starY:Number;
        for (k = 0; k < 100; k++) 
            starY = Math.random()*(displayHeight - 2);
        
        var skyline:Sprite = new McSkyline() as Sprite;
        skyline.x = 1000;
        skyline.y = 1000;
        var frame:Shape = new Shape();
        frame.graphics.drawRect(0,0,displayWidth,displayHeight);
        frame.x = displayHolder.x;
        frame.y = displayHolder.y;
        this.addChild(displayHolder);
        displayHolder.addChild(sky);
        displayHolder.addChild(starLayer);
        displayHolder.addChild(sparkBitmap);
        displayHolder.addChild(skyline);
        this.addChild(frame);
        darken = new ColorTransform(1,1,1,0.87);
        blur = new BlurFilter(4,4,1);
        origin = new Point(0,0); //used in filters
        colorList = new <uint>[0x68ff04, 0xefe26d, 0xfc4e50, 0xfffae7, 0xffffff, 0xffc100,                  0xe02222,0xffa200,0xff0000,0x8aaafd,0x3473e5, 0xc157b7,0x9b3c8a, 0xf9dc98, 0xdc9c45, 0xee9338];
        this.addEventListener(Event.ENTER_FRAME, onEnter);
    
    function onEnter(evt:Event):void       
        count++;
        if ((count >= waitCount)&&(sparkList.numOnStage < maxNumSparksAtNewFirework)) 

随机化器

waitCount = minWait+Math.random()*(maxWait - minWait);
                fireworkColor = randomColor();
                count = 0;
                flareOriginX = 125 + Math.random()*300;
                flareOriginY = 90 + Math.random()*90;
                var i:int;
                var sizeFactor:Number = 0.1 + Math.random()*0.9;
                numFlares = (0.25+0.75*Math.random()*sizeFactor)*maxNumFlares;
                for (i = 0; i < numFlares; i++) 
                    var thisParticle:Particle3D = flareList.addParticle(flareOriginX, flareOriginY,0);
                    theta = 2*Math.random()*Math.PI;
                    phi = Math.acos(2*Math.random()-1);
                    mag = 8 + sizeFactor*sizeFactor*10;//sizeFactor*(60 + 3*Math.random());
                    thisParticle.vel.x = mag*Math.sin(phi)*Math.cos(theta);
                    thisParticle.vel.y = mag*Math.sin(phi)*Math.sin(theta);
                    thisParticle.vel.z = mag*Math.cos(phi);
                    thisParticle.airResistanceFactor = 0.015;
                    //envelope - this determines how long a flare will live.
                    thisParticle.envelopeTime1 = 45 + 60*Math.random();

                    thisParticle.color = fireworkColor;
                
            
            particle  = flareList.first;
            while (particle != null) 
                nextParticle = particle.next;
                dragFactor = particle.airResistanceFactor*Math.sqrt(particle.vel.x*particle.vel.x + particle.vel.y*particle.vel.y + particle.vel.z*particle.vel.z);
                if (dragFactor > maxDragFactorFlare) 
                    dragFactor = maxDragFactorFlare;
                
                particle.vel.x += 0.05*(Math.random()*2 - 1);
                particle.vel.y += 0.05*(Math.random()*2 - 1) + gravity;
                particle.vel.z += 0.05*(Math.random()*2 - 1);
                particle.vel.x -= dragFactor*particle.vel.x;
                particle.vel.y -= dragFactor*particle.vel.y;
                particle.vel.z -= dragFactor*particle.vel.z;
                particle.pos.x += particle.vel.x;
                particle.pos.y += particle.vel.y;
                particle.pos.z += particle.vel.z;
                particle.age += 1;
                if (particle.age > particle.envelopeTime1) 
                    particle.dead = true;
                
                if ((particle.dead)||(particle.pos.x > displayWidth) || (particle.pos.x < 0) || (particle.pos.y > displayHeight) || (particle.pos.y < -topMargin)) 
                    flareList.recycleParticle(particle);
                       
                else 
                    numSparks = Math.floor(Math.random()*(maxNumSparksPerFlare+1)*(1 - particle.age/particle.envelopeTime1));
                    for (i = 0; i < maxNumSparksPerFlare; i++) 
                        randDist = Math.random();
                        var thisSpark:Particle3D = sparkList.addParticle(particle.pos.x - randDist*particle.vel.x, particle.pos.y - randDist*particle.vel.y, 0, 0);
                        thisSpark.vel.x = 0.2*(Math.random()*2 - 1);
                        thisSpark.vel.y = 0.2*(Math.random()*2 - 1);
                        thisSpark.envelopeTime1 = 10+Math.random()*40;
                        thisSpark.envelopeTime2 = thisSpark.envelopeTime1 + 6 + Math.random()*6;
                        thisSpark.airResistanceFactor = 0.2;
                        thisSpark.color = particle.color;
                    
                

                particle = nextParticle;
            
            sparkBitmapData.lock();
            //old particles will not be erased. Instead we will apply filters which more gradually fade out the particles.
            sparkBitmapData.colorTransform(sparkBitmapData.rect, darken);
            sparkBitmapData.applyFilter(sparkBitmapData, sparkBitmapData.rect, origin, blur);
            spark  = sparkList.first;
            while (spark != null) 
                nextSpark = spark.next;
                dragFactor = spark.airResistanceFactor*Math.sqrt(spark.vel.x*spark.vel.x + spark.vel.y*spark.vel.y);
                if (dragFactor > maxDragFactorSpark) 
                    dragFactor = maxDragFactorSpark;
                
                spark.vel.x += 0.07*(Math.random()*2 - 1);
                spark.vel.y += 0.07*(Math.random()*2 - 1) + gravity;
                spark.vel.x -= dragFactor*spark.vel.x;
                spark.vel.y -= dragFactor*spark.vel.y;
                spark.pos.x += spark.vel.x;
                spark.pos.y += spark.vel.y;
                spark.age += 1;
                if (spark.age < spark.envelopeTime1) 
                    sparkAlpha = 255;
                
                else if (spark.age < spark.envelopeTime2) 
                    sparkAlpha = -255/spark.envelopeTime2*(spark.age - spark.envelopeTime2);
                
                else 
                    spark.dead = true;
                   
                if ((spark.dead)||(spark.pos.x > displayWidth) || (spark.pos.x < 0) || (spark.pos.y > displayHeight) || (spark.pos.y < -topMargin)) 
                    sparkList.recycleParticle(spark);
                
                sparkColor = (sparkAlpha << 24) | spark.color;
                presentAlpha = (sparkBitmapData.getPixel32(spark.pos.x, spark.pos.y) >> 24) & 0xFF;
                if (sparkAlpha > presentAlpha) 
                    sparkBitmapData.setPixel32(spark.pos.x, spark.pos.y, sparkColor);
                
                spark = nextSpark;
            
            sparkBitmapData.unlock();
            grayAmt = 4 + 26*sparkList.numOnStage/5000;
            if (grayAmt > 30) 
                grayAmt = 30;
            
            sky.transform.colorTransform = new ColorTransform(1,1,1,1,grayAmt,grayAmt,1.08*grayAmt,0);
        

        function randomColor():uint 
            var index:int = Math.floor(Math.random()*colorList.length);
            return colorList[index];
        

【问题讨论】:

没有足够的信息来帮助解决这个问题。动画是补间动画吗? 它基于上面提到的 actionscript。该脚本声明了对象的 x 和 y 位置,因此帧上不会发生补间。 【参考方案1】:

这将停止你的动画:

this.removeEventListener(Event.ENTER_FRAME, onEnter);

【讨论】:

我已经做到了,但问题是动画是基于动作脚本而不是电影剪辑 然后给我们看一些代码,我们可能有机会帮助你 现在我在末尾添加了一个推子,而不是强制停止动画。

以上是关于停止动作脚本动画的主要内容,如果未能解决你的问题,请参考以下文章

动作脚本 3. 继续动画直到最后一帧而不中断

动作脚本 3. 在 gotoAndStop() 之后动画永远循环,并且在动画过程中当角色发生碰撞时会有延迟

无法将其他flas动画片段访问到动作脚本3

编写mysql多实例启动脚本

动作脚本 3. 如何在角色的底部制作阴影?

在scratch软件窗口中,像剧本一样搭建角色脚本,实现动画