Flash - 使用多个事件处理程序?

时间:2012-11-11 12:32:07

标签: actionscript-3 events actionscript event-handling addchild

所以我正在开发一个使用多个.as文件的游戏 - 而且我正在尝试对游戏进行编程,但是当它到达游戏中的第21行时,它一直在踢回以下错误 - 错误是..

TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild() at theGame()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\theGame.as:21] at main/changeState()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\main.as:23] at mainMenu/brnP_Button()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\mainMenu.as:40] Cannot display source code at this location.

我正在使用以下.as文件。

main.as

//main.as
package
{
import flash.display.*;
import flash.system.fscommand;

public class main extends MovieClip
{
    public function main()
    {
        changeState(null, "menu");
    }
    public function changeState (currentState, nextState)
    {
        if (currentState != null)
        {
            removeChild(currentState);
        }
        switch(nextState)
        {
            case "menu": var mm:mainMenu = new mainMenu(changeState); 
                         addChild(mm);
            break;
            case "game": var g:theGame = new theGame(changeState);
                         addChild(g);
            break;
            case "exit": fscommand("quit");
            break;
        }
    }

}

}

mainMenu.as `

//mainMenu.as
package
{
import flash.display.*;
import flash.events.*;

public class mainMenu extends MovieClip
{
    var theCallBackFunction:Function;

    private var ballX:Number = 10; // Declaring a variable known as ballX
    private var ballY:Number = 0; // Declaring a variable knwon as ballY
    private const GRAVITY:Number = 2; // Declaring a const variable known as gravity
    public const ROTATION:Number = 1;
    public const BOUNCE:Number = 0.9;
    var gameBall:Ball = new Ball();

    public function mainMenu(callBack)
    {
        addEventListener(Event.ENTER_FRAME, menuFrameHandler);
        addChild(gameBall);

        var btnPlay:mmPlay = new mmPlay();
        btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, brnP_Button);
        btnPlay.x = width/2;
        btnPlay.y = height/2 - btnPlay.height/2;
        addChild(btnPlay);

        var btnExit:mmExit = new mmExit();
        btnExit.addEventListener(MouseEvent.MOUSE_DOWN, brnE_Button);
        btnExit.x = width/2;
        btnExit.y = height/2 - btnExit.height/2;
        btnExit.y += btnExit.height + 4;
        addChild(btnExit);

        theCallBackFunction = callBack;
    }
    public function brnP_Button(e:MouseEvent)
    {
        removeEventListener(Event.ENTER_FRAME, menuFrameHandler);
        theCallBackFunction(this, "game");
        return;
    }

    public function brnE_Button(e:MouseEvent)
    {
        theCallBackFunction(this, "exit");
        return;
    }

    private function menuFrameHandler (e:Event):void
    {
        // Gravitate the Ball
        ballY += GRAVITY; // The ball is effected by gravity each frame

        // Move The Ball
        gameBall.x += ballX;
        gameBall.y += ballY;
        gameBall.rotation += ROTATION * ballX;
        checkBoundaryCollision();
    }

    private function checkBoundaryCollision():void
    {
        var left:Number;
        var right:Number;
        var bottom:Number;

        left = gameBall.x - (gameBall.width / 2);
        right = gameBall.x + (gameBall.width / 2);
        bottom = gameBall.y;

        if (left < 0 && ballX < 0)
        {
            gameBall.x = (gameBall.width / 2)
            ballX  *= -1;
        }
        else if (right > stage.stageWidth && ballX > 0)
        {
            gameBall.x = stage.stageWidth - (gameBall.width / 2)
            ballX  *= -1;
        }

        else if (bottom > stage.stageHeight && ballY > 0)
        {
            gameBall.y = stage.stageHeight - (gameBall.height/2)
            ballY *= -BOUNCE;
        }
    }
}

}

theGame.as

//theGame.as
package
{
import flash.display.MovieClip
import flash.text.TextField
import flash.events.Event
import flash.events.MouseEvent

public class theGame extends MovieClip
{
    public const GRAVITY:Number = 2; // Declaring a const variable known as gravity
    public const BOUNCE:Number = 0.8;
    public const HIT:Number = 15;
    public const ROTATION:Number = 1;
    public var gameBall:Ball;

    private var ballX:Number; // Declaring a variable known as ballX
    private var ballY:Number; // Declaring a variable knwon as ballY

    public function theGame(callBack): void
    {
        addChild(gameBall);
        ballX = Math.random(); // Initalising ballX
        ballY = Math.random(); // Initalising ballY

        gameBall.buttonMode = true;

        addEventListener(Event.ENTER_FRAME, gameFrameHandler);
        addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    }

    private function gameFrameHandler (e:Event):void
    {
        // Gravitate the Ball
        ballY += GRAVITY; // The ball is effected by gravity each frame

        // Move The Ball
        gameBall.x += ballX;
        gameBall.y += ballY;
        gameBall.rotation += ROTATION * ballX;

        // Check Stage Boundaries For Collisions
        checkBoundaryCollision();
    }

    private function mouseDownHandler (e:MouseEvent):void
    {
        // Hit the ball if it has been clicked
        if (e.target == gameBall)
        {
            hit(e.target.mouseX, e.target.mouseY);
        }
    }

    private function checkBoundaryCollision():void
    {
        var left:Number;
        var right:Number;
        var bottom:Number;
        var top:Number;

        left = gameBall.x - (gameBall.width / 2);
        right = gameBall.x + (gameBall.width / 2);
        bottom = gameBall.y + (gameBall.height / 2);
        top = gameBall.y - (gameBall.height / 2);

        if (left < 0 && ballX < 0)
        {
            gameBall.x = (gameBall.width / 2)
            ballX  *= -1;
        }
        else if (right > stage.stageWidth && ballX > 0)
        {
            gameBall.x = stage.stageWidth - (gameBall.width / 2)
            ballX  *= -1;
        }

        if (top < 42.70 && ballY < 0)
        {
            gameBall.y = 42.70 + (gameBall.height / 2)
            ballY  *= -1;
        }
        else if (bottom > stage.stageHeight && ballY >= 0)
        {
            gameBall.y = stage.stageHeight - (gameBall.height/2)
            ballY *= -BOUNCE;
            ballX *= BOUNCE;
        }
    }

    private function hit(hitX:Number, hitY:Number):void
    {
        // Adjust vertical velocity
        if (ballY > 0)
        {
            ballY *= -BOUNCE / 2;
        }

        ballY -= HIT;

        //adjust horizontal veloity
        if (ballX * hitX > 0)
        {
            ballX *= -BOUNCE;
        }

        ballX -= (hitX / gameBall.width * HIT);
    }
}
}

我将如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的课程中没有gameBall的实例游戏。

尝试添加

    gameBall = new Ball();

    addChild(gameBall);