在主时间线中的另一个影片剪辑中执行影片剪辑

发布于 2024-07-25 02:35:53 字数 8612 浏览 5 评论 0原文

我正在尝试创建一个将在其他项目中使用的影片剪辑。 它实际上是 15 个谜题的游戏。 除了当有人赢得游戏时如何执行影片剪辑之外,我一切都正常。 检查 win 是否有效,这样我就知道何时需要播放该剪辑,但我只是不知道如何播放。 所有东西都需要保留在 Puzzle 影片剪辑下。 我将附上代码。 我使用“假”胜利来测试,所以我不必每次想测试它时都玩游戏。 我是一位拥有 40 年 COBOL 编程经验的老手,但对 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");
}
}

I am trying to create a movieclip that will be used in other projects. It is actually the 15-puzzle game. I have every thing working except how to execute the movieclip when someone wins the game. Checking for win works so I know when I need to play the clip, I just don't know how. Every thing needs to stay under the Puzzle movieclip. I will attach the code. I am using the "false" win to test so I don't have to play the game everytime I want to test it. I am a 40 year vet of COBOL programming, but VERY new to Flash.
What I need to know is:
1) Where to put the win_mc
2) How to call it from the mousedown handler where I test for a win
3) Comments on coding greatly appreciated. I want to learn Flash the right way.

Thanks for any help.
Ray

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");
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-08-01 02:35:53

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

如果您只是为了好玩而接触 Flash,那么 AS2 就不错。 如果您从职业/工作的角度来研究它,那么您也最好看看 AS3。 事实上,现在了解这两者可能非常有价值,因为那里有各种各样的项目。

现在,至于你的问题!

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

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

Well, first off it looks like this code is ActionScript 2 rather than ActionScript 3. This is perfectly fine for getting things started, but AS2 is fundamentally a deadend - Adobe won't be updating it with new features.

If you're looking to get into Flash just for fun, AS2 is fine. If you're looking into it from a career/job standpoint, you'd do well to look at AS3 as well. And actually, knowing both can be very valuable these days since there is a mix of projects out there.

Now, as for your question!

In general it is best to avoid placing code anywhere other than in the main timeline (even better is to use external classes). When code is placed within GUI elements it can make the code difficult to find and difficult to debug (as you have found!) because of scope issues.

The mouseDown event handler there is attached to a child of the puzzle movieclip, not to a timeline. That means that this code runs in the scope of that clip - hence all of the references to _parent. This means that you can simply add the win_mc clip to the puzzle clip (in it's own layer on top of the tiles) and then reference it with _parent.Win_mc from your mouseDown handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文