循环自定义光标不透明度落在无限循环中

时间:2013-02-04 13:57:59

标签: actionscript-3 flash loops opacity

我试图通过改变其不透明度来使我的自定义光标慢慢发光。问题是,我能看到它工作的唯一方法是创建一个while,它会创建一个无限循环。有没有办法继续这样我的光标从0不透明度变为1不透明度并上下移动。这是我目前的代码......我试图找到另一种方法继续进行,但我真的没有看到任何其他方式。

public var Alpha:Number = 1;
public var sense:String = "down";

private function thisMouseOver(e:MouseEvent):void{


        Mouse.hide();

        //draws the cursor
        drawCursor();

        //Animates cursor
        if(!this.animationStarted)
        {
            this.animationStarted = true;
            animateCursor();
        }


    }

private function animateCursor():void{
        while(this.animationStarted)
        {
            if(this.Alpha==1)
            {
                this.sense = "down";
            }
            else if(this.Alpha == 0)
            {
                this.sense = "up";
            }

            if(this.sense == "up")
                this.Alpha += 0.1;
            else
                this.Alpha -= 0.1;

            this.graphics.beginFill(0x333333);
            this.graphics.drawRect(0,0,25,25);
            this.graphics.endFill();

            drawCursor();
        }
    }

private function drawCursor():void{
        this.graphics.beginFill(0x00BFFF,this.Alpha);
        //top left
        this.graphics.drawRect(0,0,6,2);
        //bottom left
        this.graphics.drawRect(0,23,6,2);
        //left top
        this.graphics.drawRect(0,0,2,6);
        //left bottom
        this.graphics.drawRect(0,19,2,6);
        //top righ
        this.graphics.drawRect(19,0,6,2);
        //right top
        this.graphics.drawRect(23,0,2,6);
        //bottom right
        this.graphics.drawRect(19,23,6,2);
        //right bottom
        this.graphics.drawRect(23,19,2,6);
        this.graphics.endFill();

    }

3 个答案:

答案 0 :(得分:0)

最好的方法是使用tweener,我最喜欢的是GTween

private function test():void
{
    var shape:Shape = new Shape();
    addChild(shape);

    shape.graphics.beginFill(0xFF0000, 1);
    shape.graphics.drawCircle(100, 100, 50);
    shape.graphics.endFill();

    var tween:GTween = new GTween(shape, 1, {alpha:0}, {reflect:true, repeatCount:2});
    tween.nextTween = tween;

}

使用tween.paused = true/false播放()/停止()补间。

还有另一个没有tweener的变种(但我建议使用补间,因为代码更清晰,可读且资源成本更低)更像是你的,而不是while循环使用EnterFrame事件方法 - 从{移除while循环{1}}方法,在animateCursor()调用

点添加Event.ENTER_FRAME侦听器
animateCursor();

答案 1 :(得分:0)

避免无限循环问题的一种方法是将animateCursor代码放在ENTER_FRAME事件处理程序中。

以下是它的外观:http://www.swfcabin.com/open/1360303185

(您的自定义光标形状看起来很棒;)

这是您在MiniBuilder中重新制作的代码(函数按字母顺序排列):

package com.glowingcursor {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.ui.*;

public class Application extends Sprite {
    private var cursor:Sprite;
    private var cursorOffset:Point;
    private var sense:Boolean;

    public function 
    Application() {
        addEventListener( Event.ADDED_TO_STAGE, onAdded );
    }

    private function 
    animateCursor( e:Event ):void {
        if ( cursor.alpha >= 1 ) sense = false;
        else if ( cursor.alpha <= 0.2 ) sense = true;

        if( sense ) cursor.alpha += 0.08;
        else cursor.alpha -= 0.08;
    }

    private function
    hideCursor( e:MouseEvent ):void {
        Mouse.show();
        removeChild( cursor );
        removeEventListener( Event.ENTER_FRAME, moveCursor );
        removeEventListener( Event.ENTER_FRAME, animateCursor );
    }

    private function
    initCursor():void {
        cursor = new Sprite();
        cursor.graphics.beginFill( 0xff0000 );
        cursor.graphics.drawRect(  0,  0, 6, 2 ); // top left
        cursor.graphics.drawRect(  0, 23, 6, 2 ); // bottom left
        cursor.graphics.drawRect(  0,  0, 2, 6 ); // left top
        cursor.graphics.drawRect(  0, 19, 2, 6 ); // left bottom
        cursor.graphics.drawRect( 19,  0, 6, 2 ); // top right
        cursor.graphics.drawRect( 23,  0, 2, 6 ); // right top
        cursor.graphics.drawRect( 19, 23, 6, 2 ); // bottom right
        cursor.graphics.drawRect( 23, 19, 2, 6 ); // right bottom
        cursor.graphics.endFill();

        // So InteractiveObjects react to the custom cursor properly
        cursor.mouseEnabled = false;

        // For cursor centering
        cursorOffset = new Point( cursor.width / 2, cursor.height / 2 );
    }

    private function 
    moveCursor( e:Event ):void {
        cursor.x = mouseX - cursorOffset.x;
        cursor.y = mouseY - cursorOffset.y;
    }

    private function
    onAdded( e:Event ):void {
        initCursor();
        showCursor();
    }

    private function
    showCursor():void {
        Mouse.hide();
        addChild( cursor );
        addEventListener( Event.ENTER_FRAME, moveCursor );
        addEventListener( Event.ENTER_FRAME, animateCursor );
    }
}
}

答案 2 :(得分:0)

您可以将animateCursor中的代码放在不同的方法中,并使用Timer调用该方法。通过这种方式,您可以控制光标闪烁的“快速”时间。

Timer timer = new Timer( 25, 0 );

private function animateCursor():void
{
    timer.addEventListener( TimerEvent.TIMER, timerHandler );
    timer.start()
}
private function timerListener( e:TimerEvent ):void
{
    if(this.Alpha==1)
    {
       this.sense = "down";
    }
    else if(this.Alpha == 0)
    {
       this.sense = "up";
    }

    if(this.sense == "up")
       this.Alpha += 0.1;
    else
       this.Alpha -= 0.1;

    this.graphics.beginFill(0x333333);
    this.graphics.drawRect(0,0,25,25);
    this.graphics.endFill();

    drawCursor();
}

也可以将你的alpha条件从this.Alpha == 0更改为this.Alpha&lt; = 0,因为你可以选择将修改alpha的数量更改为并不总是添加的值最多0或1。