简体   繁体   中英

how to call actionscript functions from flash timeline

I am using AS3, and i want to call function 'startMatchThree' which is in a MatchThree.as (external actionscript file) into the flahs main timeline actions.

here is my code: package { 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();
            }   
         }
     }

when I put 'startMatchThree' in the actions tab in timeline, it is showing this error message: 1180: Call to a possibly undefined method startMatchThree.

SO how to solve this error!! Thank you for the help!! :)

startMatchThree() is a method of the MatchThree class, so you need a reference to an instance of MatchThree . Is there a symbol on the timeline somewhere which is linked to the MatchThree class? Then you can do something like this, where "matchThree" is the instance name of the symbol:

matchThree.startMatchThree();

Are you instantiating the MatchThree class in code? Then you just need to use that code reference, something like this,:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM