【问题标题】:Slow down method execution so animation appears in flash减慢方法执行速度,使动画出现在 Flash 中
【发布时间】:2013-09-06 07:50:59
【问题描述】:

我有一种方法可以在 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;

}

【问题讨论】:

    标签: actionscript-3 flash animation flash-cs5


    【解决方案1】:

    从您的代码中并不太清楚动画本身是如何发生的(显然在其他地方有渲染函数),但是要在 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;
    
    }
    

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

    【讨论】:

    • 我尝试了您的建议,但每种方法都有两个相同的错误。 Line 122 1067: Implicit coercion of a value of type int to an unrelated type Function.Line 122 1067: Implicit coercion of a value of type Function to an unrelated type Number.,该行是 setTimeout(500, swapPieces, piece1, piece2);
    • 对不起!我在setTimeout() 调用中的参数顺序错误。我现在修改了有罪的行。希望这能满足您的需求!
    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多