在主时间轴中的另一个内执行movieclip

时间:2009-06-22 15:46:27

标签: flash movieclip

我正在尝试创建一个将在其他项目中使用的动画片段。它实际上是15-puzzle游戏。我有一切工作,除了当有人赢得游戏时如何执行movieclip。检查胜利是否有效,所以我知道何时需要播放剪辑,我只是​​不知道如何。每件事都需要留在Puzzle动画片段下。我会附上代码。我正在使用“虚假”的胜利进行测试,所以每次我想测试时我都不必玩游戏。我是COBOL编程的40年医生,但对Flash来说非常新。 我需要知道的是: 1)在哪里放win_mc 2)如何从我测试获胜的mousedown处理程序中调用它 3)对编码的评论非常感谢。我想以正确的方式学习Flash。

感谢您的帮助。 射线

Action Layer of "Puzzle"

    Function to initialize a new game.
//  Should include:
//      tile size
//      x Offset
//      y Offset
//      Reset any counters used to record statistics for any given game.
function initGame()
{
    _global.numMoves = 0;
    _global.hours = 0;
    _global.minutes = 0;
    _global.seconds = 0;

    var _loc1 = this;
    //
    //  Place tiles on the board in the "solved" state.
    //
    tileDist = 52;  //  Tile size
    xOffset = -16;  //  Tile x offset, controls where the left side of the tiles start.
    yOffset = 115;  //  Tile y offset, controls where the top side of the tiles start.
    _global.solutionTileName = new Array;
    _global.solutionTileX = new Array;
    _global.solutionTileY = new Array;
    SliderFrame._x = 114;
    SliderFrame._y = 245;
    //
    for (x = 1; x <= 4; x++)
    {
        for (y = 0; y <= 3; y++)
        {
            tile = x + y * 4;
            _loc1["tile" + tile]._x = xOffset + (x * tileDist);
            _loc1["tile" + tile]._y = yOffset + (y * tileDist + tileDist);
//
//      Build a solution matrix for the puzzle.
//
        _global.solutionTileName[tile] = "Tile" + tile;
        _global.solutionTileX[tile] = xOffset + (x * tileDist);
        _global.solutionTileY[tile] = yOffset + (y * tileDist + tileDist);
        //
        } // end of for
    } // end of for
    //trace(solutionTileName);
    //trace(solutionTileX);
    //trace(solutionTileY);

//
//  Randomize the tiles after placing them on the board.
//  NOTE:   According to references, about half of the initial random configurations are unsolvable.
//          This puzzle is always solvable because the shuffle algorithm starts from the "final" 
//          image and makes a series of random legal moves.
//
    for (tilenum = 0; tilenum < 100; tilenum++)
    {
        do
        {
            tile = "tile" + (random(15) + 1);
            emptySpace = findEmpty(tile);
        } while (emptySpace == "none")
        moveTile(tile, findEmpty(tile));
    } // end of for
            _global.numMoves = 0;
        this.txt = numMoves;
} // End of the function
//
//
//  Function to find an empty slot adjacent to a given tile.
//      Returns :
//          "left", "right", "above", "below", or "none"
//
function findEmpty(tile)
{
    tilex = this[tile]._x;
    tiley = this[tile]._y;
//    trace("findEmpty - Tile=" + tile + "(" + tilex + "," + tiley + ")");
//  Check for empty slot - LEFT
    if (tilex > xOffset + tileDist)
    {
        if (!tileThere((tilex - tileDist), tiley))
        {
            //trace("tile not there LEFT - (" + (tilex - tileDist) + "," + tiley + ")");
            return ("left");
        } // end if
    } // end if

//  Check for empty slot - RIGHT
    if (tilex < (xOffset + (tileDist * 4)))
    {
        if (!tileThere((tilex + tileDist), tiley))
        {
            //trace("tile not there RIGHT - (" + (tilex + tileDist) + "," + tiley + ")");
            return ("right");
        } // end if
    } // end if

//  Check for empty slot - ABOVE
    if (tiley > (yOffset + tileDist))
    {
        if (!tileThere(tilex, (tiley - tileDist)))
        {
            //trace("tile not there ABOVE - (" + tilex + "," + (tiley - tileDist) + ")");
            return ("above");
        } // end if
    } // end if

//  Check for empty slot - BELOW
    if (tiley < (yOffset + (tileDist * 4)))
    {
        if (!tileThere(tilex, (tiley + tileDist)))
        {
            //trace("tile not there BELOW - (" + tilex + "," + (tiley + tileDist) + ")");
            return ("below");
        } // end if
    } // end if
    return ("none");
} // End of the function
//
//  Function to test if there is a tile in a given slot.
//      Returns :
//          "true" or "false"
//
function tileThere(thisx, thisy)
{
    var _loc1 = this;
    var _loc2 = thisx;
    var _loc3 = thisy;
    for (i = 1; i <= 15; i++)
    {
        if (_loc1["tile" + i]._x == _loc2)
        {
            if (_loc1["tile" + i]._y == _loc3)
            {
                return (true);
            } // end if
        } // end if
    } // end of for
    return (false);
} // End of the function
//
//  Function to move a given tile left, right, up, or down depending on direction passed.
//      Returns :
//          nothing
//

function moveTile(tile, direction)
{
    var _loc1 = tile;
    var _loc2 = this;
    var _loc3 = direction;
    if (_loc3 == "above")
    {
        _loc2[_loc1]._y = _loc2[_loc1]._y - tileDist;
        _global.numMoves = _global.numMoves + 1;
        this.txt = numMoves;
        return;
    } // end if
    if (_loc3 == "below")
    {
        _loc2[_loc1]._y = _loc2[_loc1]._y + tileDist;
        _global.numMoves = _global.numMoves + 1;
        this.txt = numMoves;
        return;
    } // end if
    if (_loc3 == "left")
    {
        _loc2[_loc1]._x = _loc2[_loc1]._x - tileDist;
        _global.numMoves = _global.numMoves + 1;
        this.txt = numMoves;
        return;
    } // end if
    if (_loc3 == "right")
    {
        _loc2[_loc1]._x = _loc2[_loc1]._x + tileDist;
        _global.numMoves = _global.numMoves + 1;
        this.txt = numMoves;
    } // end if
} // End of the function
//
//
//  Function to find which tile is under the mouse when clicked.
//      Returns :
//          i   an integer indicating Tile1, Tile2,...,Tile15
//
function tileUnderMouse()
{
    var _loc1 = this;
    for (i = 1; i <= 15; i++)
    {
        if (_loc1["Tile" + i].hitTest(_xmouse, _ymouse))
        {
            return (i);
        } // end if
    } // end of for
} // End of the function
function GetElapsedTime()
{
    _global.hours;
    _global.minutes;
    _global.seconds;
    _global.elapsedTime;
    seconds = seconds + 1;
    if (seconds == 60)
        {
            minutes = minutes + 1;
            seconds = 0;
        }
    if (minutes == 60)
        {
            hours = hours + 1;
            minutes = 0;
        }
    tSeconds = seconds;
    tMinutes = minutes;
    tHours = hours;
    if (Seconds < 10)
        {
            tSeconds = "0" + tSeconds;
        }
    if (Minutes < 10)
        {
            tMinutes = "0" + tMinutes;
        }
    if (minutes < 1)
    {
        tMinutes = "00";
    }
    if (hours < 10)
        {
            tHours = "0" + tHours;
        }
    if (hours < 1)
    {
        tHours = "00";
    }
    elapsedTime = tHours + ":" + tMinutes + ":" + tSeconds;
} // End of the function
//
//  Function to test if the puzzle is solved.
//      Returns :
//          "true" or "false"
//
function isWin()
{
    var win = 1;
    for (i = 1; i <= 15; i++)
    {
        if (("Tile" + i) != solutionTileName[i])
        {
            win = 0;
        } // end if
        if (this["Tile" + i]._x != solutionTileX[i])
        {
            win = 0;
        } // end if
        if (this["Tile" + i]._y != solutionTileY[i])
        {
            win = 0;
        } // end if
    } // end of for
    if (win == 1)
    {
        return(true);
    }
        else
        {
            return(false);
        }
} // End of the function
//
//  Entry point to movie clip Puzzle_mc
//
    _global.solutionTileName = new Array;
    _global.solutionTileX = new Array;
    _global.solutionTileY = new Array;
_global.numMoves = 0;
_global.hours = 0;
_global.minutes = 0;
_global.seconds = 0;
this.elapsedTime = "00:00:00";
var intervalId;
initGame();
intervalId = setInterval(GetElapsedTime,1000);
stop ();

Layer 16 of "Puzzle"

//
//  Action Function to handle the mouse click and move the tile to the appropriate position.
//      Returns :
//          nothing
//
onClipEvent (mouseDown) 
{ 
    //tileClicked = _root.Puzzle_mc.tileUnderMouse();
    //emptySpace = _root.Puzzle_mc.findEmpty("tile" + tileClicked); 
    //_root.Puzzle_mc.moveTile("tile" + tileClicked, emptySpace);
     tileClicked = _parent.tileUnderMouse(); 
     emptySpace = _parent.findEmpty("tile" + tileClicked); 
    _parent.moveTile("tile" + tileClicked, emptySpace);

//if (this.isWin())
if (_parent.isWin())
{
    trace(this + "TRUE");
}
else
{
    Win_mc.Play(2);
    //WinSymbol.gotoAndPlay("WinSymbolL");
    //gotoAndPlay("WinSymbolL");
    trace(this + "FALSE");
}
}

1 个答案:

答案 0 :(得分:0)

嗯,首先看起来这个代码是ActionScript 2而不是ActionScript 3.这对于开始工作来说非常好,但AS2基本上是一个deadend - Adob​​e不会用新功能更新它。

如果你想进入Flash只是为了好玩,AS2很好。如果你从职业/工作的角度来看待它,你也应该好好看看AS3。实际上,由于那里有各种各样的项目,因此了解这两者可能非常有价值。

现在,关于你的问题!

一般来说,最好避免将代码放在主时间轴以外的任何地方(更好的是使用外部类)。当代码放在GUI元素中时,由于范围问题,它可能使代码难以找到并且难以调试(正如您所发现的那样!)。

mouseDown事件处理程序附加到拼图动画片段的子项,而不是时间轴。这意味着此代码在该剪辑的范围内运行 - 因此所有对_parent的引用。这意味着您可以简单地将win_mc剪辑添加到拼图剪辑(在拼图顶部的自己的图层中),然后使用mouseDown处理程序中的_parent.Win_mc引用它。