如何从flash时间轴调用actionscript函数

时间:2015-03-23 17:15:55

标签: actionscript-3 flash actionscript

我正在使用AS3,我想将函数'startMatchThree'调用到flahs主时间轴操作中,该函数位于MatchThree.as(外部actionscript文件)中。

这是我的代码:     包{         import flash.display。;         import flash.events。;         import flash.text。*;         import flash.utils.Timer;

    public class MatchThree extends MovieClip {
         // constants
        static const numPieces:uint = 4;
        static const spacing:Number = 45;
        static const offsetX:Number = 30;
        static const offsetY:Number = 50;

    // game grid and mode
        private var grid:Array;
        private var gameSprite:Sprite;
        private var firstPiece:Piece;
        private var isDropping,isSwapping:Boolean;
        private var gameScore:int;

    // set up grid and start game
        public function startMatchThree() {

        // create grid array
            grid = new Array();
            for(var gridrows:int=0;gridrows<8;gridrows++) {
                grid.push(new Array());
            }
            setUpGrid();
            isDropping = false;
            isSwapping = false;
            gameScore = 0;
            addEventListener(Event.ENTER_FRAME,movePieces);
        }

        public function setUpGrid() {
        // loop until valid starting grid
            while (true) {
                // create sprite
                gameSprite = new Sprite();

            // add 64 random pieces
                for(var col:int=0;col<8;col++) {
                    for(var row:int=0;row<8;row++) {
                        addPiece(col,row);
                    }
                }

            // try again if matches are present
                if (lookForMatches().length != 0) continue;

            // try again if no possible moves
                if (lookForPossibles() == false) continue;

            // no matches, but possibles exist: good board found
                break;
            } 

        // add sprite
            addChild(gameSprite);
        }

    // create a random piece, add to sprite and grid
        public function addPiece(col,row:int):Piece {
            var newPiece:Piece = new Piece();
            newPiece.x = col*spacing+offsetX;
            newPiece.y = row*spacing+offsetY;
            newPiece.col = col;
            newPiece.row = row;
            newPiece.type = Math.ceil(Math.random()*4);
            newPiece.gotoAndStop(newPiece.type);
            newPiece.select.visible = false;
            gameSprite.addChild(newPiece);
            grid[col][row] = newPiece;
            newPiece.addEventListener(MouseEvent.CLICK,clickPiece);
            return newPiece;
        }

    // player clicks on a piece
        public function clickPiece(event:MouseEvent) {
            var piece:Piece = Piece(event.currentTarget);

        // first one selected
            if (firstPiece == null) {
                piece.select.visible = true;
                firstPiece = piece;

        // clicked on first piece again
            } else if (firstPiece == piece) {
                piece.select.visible = false;
                firstPiece = null;

        // clicked on second piece
            } else {
                firstPiece.select.visible = false;

            // same row, one column over
                if ((firstPiece.row == piece.row) &&     (Math.abs(firstPiece.col-piece.col) == 1)) {
                    makeSwap(firstPiece,piece);
                    firstPiece = null;

            // same column, one row over
                } else if ((firstPiece.col == piece.col) && (Math.abs(firstPiece.row-piece.row) == 1)) {
                    makeSwap(firstPiece,piece);
                    firstPiece = null;

            // bad move, reassign first piece
                } else {
                    firstPiece = piece;
                    firstPiece.select.visible = true;
               }
            }
        }

    // start animated swap of two pieces
        public function makeSwap(piece1,piece2:Piece) {
            swapPieces(piece1,piece2);

        // check to see if move was fruitful
            if (lookForMatches().length == 0) {
                swapPieces(piece1,piece2);
            } 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;

    }

    // if any pieces are out of place, move them a step closer to being in place
    // happens when pieces are swapped, or they are dropping
    public function movePieces(event:Event) {
        var madeMove:Boolean = false;
        for(var row:int=0;row<8;row++) {
            for(var col:int=0;col<8;col++) {
                if (grid[col][row] != null) {

                    // needs to move down
                    if (grid[col][row].y < grid[col][row].row*spacing+offsetY) {
                        grid[col][row].y += 5;
                        madeMove = true;

                    // needs to move up
                    } else if (grid[col][row].y > grid[col][row].row*spacing+offsetY) {
                        grid[col][row].y -= 5;
                        madeMove = true;

                    // needs to move right
                    } else if (grid[col][row].x < grid[col][row].col*spacing+offsetX) {
                        grid[col][row].x += 5;
                        madeMove = true;

                    // needs to move left
                    } else if (grid[col][row].x > grid[col][row].col*spacing+offsetX) {
                        grid[col][row].x -= 5;
                        madeMove = true;
                    }
                }
            }
        }

        // if all dropping is done
        if (isDropping && !madeMove) {
            isDropping = false;
            findAndRemoveMatches();

        // if all swapping is done
        } else if (isSwapping && !madeMove) {
            isSwapping = false;
            findAndRemoveMatches();
        }
    }


    // gets matches and removes them, applies points
    public function findAndRemoveMatches() {
        // get list of matches
        var matches:Array = lookForMatches();
        for(var i:int=0;i<matches.length;i++) {
            var numPoints:Number = (matches[i].length-1)*50;
            for(var j:int=0;j<matches[i].length;j++) {
                if (gameSprite.contains(matches[i][j])) {
                    var pb = new PointBurst(this,numPoints,matches[i][j].x,matches[i][j].y);
                    addScore(numPoints);
                    gameSprite.removeChild(matches[i][j]);
                    grid[matches[i][j].col][matches[i][j].row] = null;
                    affectAbove(matches[i][j]);
                }
            }
        }

        // add any new piece to top of board
        addNewPieces();

        // no matches found, maybe the game is over?
        if (matches.length == 0) {
            if (!lookForPossibles()) {
                endGame();
            }   
         }
     }

当我在时间轴的操作选项卡中放入'startMatchThree'时,它显示以下错误消息: 1180:调用可能未定义的方法startMatchThree。

那怎么解决这个错误!! 感谢您的帮助!! :)

1 个答案:

答案 0 :(得分:0)

startMatchThree()MatchThree类的方法,因此您需要引用MatchThree的实例。在某个与MatchThree类相关联的时间轴上是否有符号?然后你可以做这样的事情,其中​​&#34;匹配三个&#34;是符号的实例名称:

matchThree.startMatchThree();

您是否在代码中实例化MatchThree类?然后你只需要使用那个代码引用,如下所示:

var matchThree:MatchThree = new MatchThree();
matchThree.startMatchThree();