0

我在我的项目中创建了三个类(游戏开始),我对 AS3 非常陌生。

首先,我创建了一个名为 player 的类,其中包含 ball_A 的运动。
然后我创建了一个名为enemy 的类,它应该包含ball_B 的运动。

在我的主类中,我创建了对玩家和敌人类的引用,并尝试根据我的玩家类分配 ball_A.x 和 ball_A.y 坐标,对于敌人类也是如此。

这是我的主要课程。

package  
{
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;

public class Main extends MovieClip
{
    public var playerClass:player;
    public var enemyClass:enemy;
    
    
    
    public function Main()
    {
        playerClass = new player();
        enemyClass = new enemy();
        addChild(playerClass);
        addChild(enemyClass);
        
        ball_A.x = playerClass.ball_A_x;
        ball_A.y = playerClass.ball_A_y;
        
        ball_B.x = enemyClass.ball_B_x;
        ball_B.y = enemyClass.ball_B_y;
        
        
        
    }//Public function

}// Class

}// Package

这是我的播放器类:

    package  {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;


public class player extends MovieClip
{
    
    var leftIsPressed:Boolean = false;
    var rightIsPressed:Boolean = false;
    var upIsPressed:Boolean = false;
    var downIsPressed:Boolean = false;
    var spaceIsPressed:Boolean = false;
    var rightCtrlIsPressed:Boolean = false;
    var speed:Number = 15;
    var vx:Number = 0;
    var vy:Number = 0;
    var ball_A_x:int = 0;
    var ball_A_y:int = 0;
    
                
        
    
    public function player() 
    {
        this.addEventListener(Event.ADDED_TO_STAGE, initialize);
    }
        
        private function initialize(e:Event):void 
        {
            //EventListeners
            this.removeEventListener(Event.ADDED_TO_STAGE, initialize);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        
        }
        
        
        
        
        //Functions
        
        function keyDownHandler(e:KeyboardEvent):void 
        {
            switch(e.keyCode) 
            {
                case Keyboard.LEFT : leftIsPressed = true; break;
                case Keyboard.RIGHT : rightIsPressed = true; break;
                case Keyboard.UP : upIsPressed = true; break;
                case Keyboard.DOWN : downIsPressed = true; break;
                case e.keyCode = 163 : rightCtrlIsPressed = true; break;
                
            }
            
        }

        function keyUpHandler(e:KeyboardEvent):void 
        {
            switch(e.keyCode)
            {
                case Keyboard.LEFT : leftIsPressed = false; break;
                case Keyboard.RIGHT : rightIsPressed = false; break;
                case Keyboard.UP : upIsPressed = false; break;
                case Keyboard.DOWN : downIsPressed = false; break;
                case e.keyCode = 163 :  rightCtrlIsPressed = false; break;
            }
            
        }

        function enterFrameHandler(e:Event):void 
        {
            vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
            vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
            
                ball_A_x += vx;
                ball_A_y += vy;
                
                if(ball_A_x >= 530)
                {
                    ball_A_x = 0;
                }
                else if(ball_A_x <= 0)
                {
                    ball_A_x = 530;
                }
                if(ball_A_y >= 370)
                {
                    ball_A_y = -28;
                }
                else if(ball_A_y <= -28)
                {
                    ball_A_y = 370;
                }
                    
        }
            

    //}//Public function
    

}//Class
}//Package

我想让连接到我的主类的 .fla 中的两个符号具有玩家类和敌人类的功能。(没有显示敌人,因为代码基本相同)

但是当我尝试使用此代码时,我收到此错误:

TypeError:错误 #1009:无法访问空对象引用的属性或方法。
在玩家()
在 Main()

我认为这是因为我的 eventListeners 没有返回任何内容,因为它们是无效的。我怎样才能解决这个问题而不必在一个类中编写所有运动命令?

4

1 回答 1

0

我认为问题在于您在将 Player 添加到舞台之前指的是 Player 中的“舞台”。事实上,Player 永远不会添加到舞台上,因此,Player 将永远无法访问舞台。在 Main 中,将您的 Player 和 Enemy 添加到舞台:

addChild(playerClass);
addChild(enemyClass);

在 Player 和 Enemy 类的构造函数中,添加:

this.addEventListener(Event.ADDED_TO_STAGE, initialize);

private function initialize(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, initialize);
... other event listeners here, now that Player is on stage
}
于 2013-09-16T10:19:59.360 回答