0

如果有人可以帮助我?当我尝试登录我的主游戏时遇到问题。我有这样的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MenuMain$iinit()
at DocumentMain/gamemain()
at DocumentMain/mouseClick1()

这是我在 MainGame 中的脚本:

package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.geom.Point;
import flash.display.Sprite;

 public class MenuMain extends MovieClip {  
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;

public var leftBumping:Boolean = false;
public var rightBumping:Boolean = false;
public var upBumping:Boolean = false;
public var downBumping:Boolean = false;
public var leftBumpPoint:Point = new Point(-36, -15);
public  var rightBumpPoint:Point = new Point(36, -15);
public var upBumpPoint:Point = new Point(0, -120);
public var downBumpPoint:Point = new Point(0, 0);
public var scrollX:Number = 0;
public var scrollY:Number = 650;
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public var speedConstant:Number = 4;
public var frictionConstant:Number = 0.9;
public var gravityConstant:Number = 1.8;
public var jumpConstant:Number = -35;
public var maxSpeedConstant:Number = 18;
public var doubleJumpReady:Boolean = false;
public var upReleasedInAir:Boolean = false;


public var animationState:String = "still";

public function MenuMain(){

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);}

public function loop(e:Event):void{
    if(back.collision.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
        leftBumping = true;
    } else {
        leftBumping = false;
    }
    if(back.collision.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
        rightBumping = true;
    } else {
        rightBumping = false;
    }
    if(back.collision.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
        upBumping = true;
    } else {
        upBumping = false;
    }
    if(back.collision.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
        downBumping = true;
    } else {
        downBumping = false;
    }   

    if(leftPressed){
        xSpeed -= speedConstant;
        player.scaleX = -1;

    } else if(rightPressed){
        xSpeed += speedConstant;
        player.scaleX = 1;
}
if(leftBumping){
    if(xSpeed < 0){
        xSpeed *= -0.5;
    }
}
if(rightBumping){
    if(xSpeed > 0){
        xSpeed *= -0.5;
    }
}
if(upBumping){
    if(ySpeed < 0){
        ySpeed *= -0.5;
    }
}
if(downBumping){ //Jika mengenai tanah/dasar
    if(ySpeed > 0){ 
        ySpeed = 0; //set y speed ke nol
    }
    if(upPressed){ //dan jika arah keatas ditekan
        ySpeed = jumpConstant; //set y speed ke jump constant
    }

    //DOUBLE JUMP
    if(upReleasedInAir == true){
        upReleasedInAir = false;
    }
    if(doubleJumpReady == false){
        doubleJumpReady = true;
    }
} else { //Jika mengenai tanah/dasar
    ySpeed += gravityConstant; //akslerasi kebawah

    //DOUBLE JUMP
    if(upPressed == false && upReleasedInAir == false){
        upReleasedInAir = true;

    }
    if(doubleJumpReady && upReleasedInAir){
        if(upPressed){ //dan jika arah keatas ditekan
            doubleJumpReady = false;
            ySpeed = jumpConstant; //set y speed ke jump constant
        }
    }
}

if(xSpeed > maxSpeedConstant){ //gerak kanan
    xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){ //gerak kiri
    xSpeed = (maxSpeedConstant * -1);
}

xSpeed *= frictionConstant;
ySpeed *= frictionConstant;

if(Math.abs(xSpeed) < 0.5){
    xSpeed = 0;
}

scrollX -= xSpeed;
scrollY -= ySpeed;

back.x = scrollX;
back.y = scrollY;

sky.x = scrollX * 0.2;
sky.y = scrollY * 0.2;

if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
    animationState = "jalan";
} else if(downBumping){
    animationState = "still";
} else {
    animationState = "jump";
}

if(player.currentLabel != animationState){
    player.gotoAndStop(animationState);
}
    }   

public function keyDownHandler(e:KeyboardEvent):void
{
    if(e.keyCode == 37){
        leftPressed = true;

    } else if(e.keyCode == 39){
        rightPressed = true;

    } else if(e.keyCode == 38){
        upPressed = true;

    } else if(e.keyCode == 40){
        downPressed = true;
    }

}


public function keyUpHandler(e:KeyboardEvent):void
{
    if(e.keyCode == 37){
        leftPressed = false;

    } else if(e.keyCode == 39){
        rightPressed = false;

    } else if(e.keyCode == 38){
        upPressed = false;

    } else if(e.keyCode == 40){
        downPressed = false;
    }
}

}
}

这是我的文件下载: https ://www.dropbox.com/s/g36hfykvebiu0sw/MyGame.rar

感谢帮助

4

1 回答 1

2

您无法访问该阶段,因为它是在对象实际添加到阶段之前被调用的。当您尝试访问构造函数中的阶段时,通常会发生这种情况。请改用 AdditionalToStage 事件。尝试这个:

public function MenuMain(){
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(e:Event):void{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    stage.addEventListener(Event.ENTER_FRAME, loop);
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
于 2013-06-12T16:48:32.100 回答