0

我正在尝试通过更改其不透明度来使自定义光标缓慢发光。问题是我可以看到它工作的唯一方法是制作 a 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();

    }
4

3 回答 3

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播放()/停止()补间。

还有另一个没有 tween 的变体(但我建议使用 tween,因为代码更清晰、可读且资源成本更低)更像你的,但不是 while 循环使用 EnterFrame 事件方法 - 从animateCursor()方法中删除 while 循环,添加Event.ENTER_FRAME侦听器在animateCursor();呼叫点:

if(!this.animationStarted)
{
     this.animationStarted = true;
     addEventListener(Event.ENTER_FRAME, onEnterFrame);
} 

private function onEnterFrame():void
{
    animateCursor();
}
于 2013-02-04T14:14:29.147 回答
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 <= 0 也是一个好主意,因为您可以选择将修改 alpha 的数量更改为不总是加起来为 0 或1.

于 2013-02-08T17:31:04.797 回答
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 );
    }
}
}
于 2013-02-08T07:42:36.310 回答