Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 循环自定义光标不透明度在无限循环中下降_Actionscript 3_Flash_Loops_Opacity - Fatal编程技术网

Actionscript 3 循环自定义光标不透明度在无限循环中下降

Actionscript 3 循环自定义光标不透明度在无限循环中下降,actionscript-3,flash,loops,opacity,Actionscript 3,Flash,Loops,Opacity,我正试图通过改变不透明度使自定义光标缓慢发光。问题是,我能看到它工作的唯一方法是在的同时创建一个,这会创建一个无限循环。有没有办法让我的光标从0不透明度变为1不透明度并上下移动。这是我目前的代码。。。我正试图找到另一种方式继续,但我真的看不到任何其他方式 public var Alpha:Number = 1; public var sense:String = "down"; private function thisMouseOver(e:MouseEvent):void{

我正试图通过改变不透明度使自定义光标缓慢发光。问题是,我能看到它工作的唯一方法是在
的同时创建一个
,这会创建一个无限循环。有没有办法让我的光标从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();

    }

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

使用
tween.paused=true/false
播放()

还有另一个没有tweener的变体(但我建议使用tween,因为代码更清晰、可读性更好,而且资源成本更低),它更像您的,但不是while循环,而是使用EnterFrame事件方法-从
animatecursors()
方法中删除while循环,添加
事件。在
animateCursor()点输入_FRAME
listener呼叫:

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

private function onEnterFrame():void
{
    animateCursor();
}

避免无限循环问题的一种方法是将animateCursor代码放入ENTER\u FRAME事件处理程序中

下面是它的外观:

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

以下是在MiniBuilder中重新生成的代码(函数按字母顺序排列):

package com.glowingcursor{
导入flash.display.*;
导入flash.events.*;
导入flash.geom.*;
导入flash.ui.*;
公共类应用程序扩展了Sprite{
私有变量游标:Sprite;
私有变量游标偏移:点;
私有变量意义:布尔;
公共职能
应用程序(){
addEventListener(Event.ADDED_至_阶段,onAdded);
}
私人职能
动画师(e:事件):无效{
如果(cursor.alpha>=1)sense=false;

else if(cursor.alpha您可以将来自AnimateCursors的代码放入另一个方法中,并使用计时器调用该方法。这样您可以控制光标闪烁的“快”程度和时间

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条件从这个.alpha==0更改为这个.alpha
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 );
    }
}
}
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();
}