减慢方法执行,使动画出现在flash中

时间:2013-09-06 07:50:59

标签: actionscript-3 flash animation flash-cs5

我有一种方法可以在Bejeweled中交换两个部分,但是当用户进行无匹配的交换时,动画交换不会显示给用户。

即。当用户试图交换宝石并且没有匹配发生时,宝石保持静止。应该发生的事情是珠宝交换位置,意识到没有匹配并且交换回来。

我认为问题在于,如果没有匹配且用户没有动画,交换几乎立即发生。

如何减慢第二次交换的执行时间(即交换回),以便交换动画?

// start animated swap of two pieces

public function makeSwap(piece1,piece2:Piece)
{
    swapPieces(piece1,piece2);

    //check to see if move works

    if (lookForMatches().length == 0)
    {
        swapPieces(piece1,piece2); //Swap the piece back
    } 
    else 
    {
        isSwapping = true;
    }
}

// swap two pieces

public function swapPieces(piece1,piece2:Piece) 
{
    // swap row and col values

    var tempCol:uint = piece1.col;
    var tempRow:uint = piece1.row;
    piece1.col = piece2.col;
    piece1.row = piece2.row;
    piece2.col = tempCol;
    piece2.row = tempRow;

    // swap grid positions

    grid[piece1.col][piece1.row] = piece1;
    grid[piece2.col][piece2.row] = piece2;

}

1 个答案:

答案 0 :(得分:0)

从您的代码中不太清楚动画本身是如何发生的(显然在其他地方有渲染函数),但是为了在AS3中执行函数添加延迟,您可以使用setTimeout函数或Timer类。

这是setTimeout

import flash.utils.setTimeout; //To be added with your other imports

// start animated swap of two pieces

public function makeSwap(piece1:Piece,piece2:Piece):void
{
    swapPieces(piece1,piece2);

    //check to see if move works

    if (lookForMatches().length == 0)
    {
        setTimeout(swapPieces, 500, piece1, piece2); //Swap the piece back, after delay of 500 milliseconds
    } 
    else 
    {
        isSwapping = true;
    }
}

// swap two pieces

public function swapPieces(piece1:Piece,piece2:Piece):void
{
    // swap row and col values

    var tempCol:uint = piece1.col;
    var tempRow:uint = piece1.row;
    piece1.col = piece2.col;
    piece1.row = piece2.row;
    piece2.col = tempCol;
    piece2.row = tempRow;

    // swap grid positions

    grid[piece1.col][piece1.row] = piece1;
    grid[piece2.col][piece2.row] = piece2;

}

这里是Timer

import flash.utils.Timer; //To be added with your other imports
import flash.events.TimerEvent; //To be added with your other imports

private var _swapBackTimer:Timer = new Timer(500, 1); //Delay of 500 milliseconds, timer running 1 time
private var _delaySwapPiece1:Piece;
private var _delaySwapPiece2:Piece;

// start animated swap of two pieces

public function makeSwap(piece1:Piece,piece2:Piece):void
{
    swapPieces(piece1,piece2);

    //check to see if move works

    if (lookForMatches().length == 0)
    {
        _swapBackTimer.reset();
        _swapBackTimer.addEventListener(TimerEvent.TIMER, swapBackTimer_complete);
        _swapPiece1 = piece1;
        _swapPiece2 = piece2;
        _swapBackTimer.start();
    } 
    else 
    {
        isSwapping = true;
    }
}

private function swapBackTimer_complete(evt:TimerEvent):void
{
    _swapBackTimer.removeEventListener(TimerEvent.TIMER, swapBackTimer_complete);
    swapPieces(_delaySwapPiece1, _delaySwapPiece2);
}

// swap two pieces

public function swapPieces(piece1:Piece,piece2:Piece):void
{
    // swap row and col values

    var tempCol:uint = piece1.col;
    var tempRow:uint = piece1.row;
    piece1.col = piece2.col;
    piece1.row = piece2.row;
    piece2.col = tempCol;
    piece2.row = tempRow;

    // swap grid positions

    grid[piece1.col][piece1.row] = piece1;
    grid[piece2.col][piece2.row] = piece2;

}

要记住的一件事是用户在延迟期间改变游戏状态的风险,使延迟的动作无效并引入错误。您可能已经在管理此问题,但为了以防万一,一种方法是在任何延迟操作期间设置标志,然后在此期间忽略任何后续输入。另一种方法是取消延迟的操作,让它立即执行,然后从该点处理新的用户操作。