2013-06-12 82 views
0

如果有人能幫助我? 當我嘗試登錄我的主遊戲時遇到問題。 我有這樣的錯誤:TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。不能使用stage.addEventListener

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; 
    } 
} 

} 
} 

在這裏,這是MYFILE下載: https://www.dropbox.com/s/g36hfykvebiu0sw/MyGame.rar

感謝您的幫助

+0

http://martywallace.com/post/as3-error-1009-cannot-access-a-property-or-method-of-a-null-object-reference – Marty

回答

2

您無法訪問舞臺,因爲在實際將對象添加到舞臺之前會調用舞臺。這通常發生在您嘗試訪問構造函數中的階段時。改用AddedToStage事件。試試這個:

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); 
} 
+0

謝謝你們,它的工作 –

相關問題