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

Posted

技术标签:

【中文标题】Flash - 使用多个事件处理程序?【英文标题】:Flash - using multiple event handlers? 【发布时间】:2012-10-31 03:14:28 【问题描述】:

所以我正在开发一个使用多个 .as 文件的游戏 - 我正在尝试对游戏进行编程,但是当它到达 theGame.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】:

theGame 类中没有gameBall 的实例。

尝试添加

    gameBall = new Ball();

之前

    addChild(gameBall);

【讨论】:

以上是关于Flash - 使用多个事件处理程序?的主要内容,如果未能解决你的问题,请参考以下文章

使用单个事件处理程序 asp.net 从网格中处理多个删除事件

Websockets 在客户端发送多个事件和多个事件处理程序

CHANGE事件未从数字步进器(AS3,Flash)触发

使用单个事件处理程序更改多个文本框值

ajax调用后Rails不显示flash消息

如何在 Expression Blend 中的单个事件处理程序中为按钮单击添加多个事件?