I'll begin by saying that I've got hardly any experience with ActionScript3, and I've only just started with it by attempting to create a platformer using a tutorial. What I'm trying to do at the moment is have a Start scene in my game, where the player can start the game, quit the game etc, however, I run into this problem when pressing on the MovieClip button: "TypeError: Error #1009: Cannot access a property or method of a null object reference. at GAMEMAIN_fla::MainTimeline/loop()" I've looked at similiar questions on here before, however, they were more personalised towards the code that was written within the post, and I was unable to find any general fixes to this problem. On my Start scene, I've just got a Start button in the form of a MovieClip under the instance name of btnx, with the following code: stop(); btnx.addEventListener(MouseEvent.CLICK, myClick); function myClick(event:MouseEvent) { btnx.removeEventListener(MouseEvent.CLICK, myClick); gotoAndPlay(0,"Game"); }
And on the game itself, I've got this code (I didn't come up with the code, I just followed a tutorial) var leftPressed:Boolean = false; var rightPressed:Boolean = false; var upPressed:Boolean = false; var downPressed:Boolean = false;
var leftBumping:Boolean = false; var rightBumping:Boolean = false; var upBumping:Boolean = false; var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55); var rightBumpPoint:Point = new Point(30, -55); var upBumpPoint:Point = new Point(0, -120); var downBumpPoint:Point = new Point(0, 0);
var scrollX:Number = 0; var scrollY:Number = 500;
var xSpeed:Number = 0; var ySpeed:Number = 0;
var speedConstant:Number = 4; var frictionConstant:Number = 0.9; var gravityConstant:Number = 1.8; var jumpConstant:Number = -35; var maxSpeedConstant:Number = 18;
var doubleJumpReady:Boolean = false; var upReleasedInAir:Boolean = false;
var keyCollected:Boolean = false; var doorOpen:Boolean = false;
var currentLevel:int = 1;
var animationState:String = "idle";
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event😞void{ if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){ //trace("leftBumping"); leftBumping = true; } else { leftBumping = false; }
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){ //trace("rightBumping"); rightBumping = true; } else { rightBumping = false; }
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){ //trace("upBumping"); upBumping = true; } else { upBumping = false; }
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){ //trace("downBumping"); downBumping = true; } else { downBumping = false; }
if(leftPressed){ xSpeed -= speedConstant; player.scaleX = -1;
} else if(rightPressed){ xSpeed += speedConstant; player.scaleX = 1; }
/*if(upPressed){ ySpeed -= speedConstant;
} else if(downPressed){ ySpeed += speedConstant;
} */
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){ //if we are touching the floor if(ySpeed > 0){ ySpeed = 0; //set the y speed to zero } if(upPressed){ //and if the up arrow is pressed ySpeed = jumpConstant; //set the y speed to the jump constant }
//DOUBLE JUMP if(upReleasedInAir == true){ upReleasedInAir = false; } if(doubleJumpReady == false){ doubleJumpReady = true; } } else { //if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
//DOUBLE JUMP if(upPressed == false && upReleasedInAir == false){ upReleasedInAir = true; //trace("upReleasedInAir"); } if(doubleJumpReady && upReleasedInAir){ if(upPressed){ //and if the up arrow is pressed //trace("doubleJump!"); doubleJumpReady = false; ySpeed = jumpConstant; //set the y speed to the jump constant } }
}
if(keyCollected == false){ if(player.hitTestObject(back.other.doorKey)){ back.other.doorKey.visible = false; keyCollected = true; trace("key collected"); } }
if(doorOpen == false){ if(keyCollected == true){ if(player.hitTestObject(back.other.lockedDoor)){ back.other.lockedDoor.gotoAndStop(2); doorOpen = true; trace("door open"); } } }
if(xSpeed > maxSpeedConstant){ //moving right xSpeed = maxSpeedConstant; } else if(xSpeed < (maxSpeedConstant * -1)){ //moving left 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 = "running"; } else if(downBumping){ animationState = "idle"; } else { animationState = "jumping"; }
if(player.currentLabel != animationState){ player.gotoAndStop(animationState); }
}
function nextLevel():void{ currentLevel++; trace("Next Level: " + currentLevel); if(currentLevel == 2){ gotoLevel2(); } else if (currentLevel == 3){ gotoLevel3(); } // can be extended... // else if(currentLevel == 3) { gotoLevel3(); } // etc, etc. }
function gotoLevel2():void{ back.other.gotoAndStop(2); back.visuals.gotoAndStop(2); back.collisions.gotoAndStop(2); scrollX = 0; scrollY = 500;
keyCollected = false; back.other.doorKey.visible = true; doorOpen = false; back.other.lockedDoor.gotoAndStop(1); }
function gotoLevel3():void{ back.other.gotoAndStop(3); back.visuals.gotoAndStop(3); back.collisions.gotoAndStop(3); scrollX = 0; scrollY = 500;
keyCollected = false; back.other.doorKey.visible = true; doorOpen = false; back.other.lockedDoor.gotoAndStop(1); }
function keyDownHandler(e:KeyboardEvent😞void{ if(e.keyCode == Keyboard.LEFT){ leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){ rightPressed = true;
} else if(e.keyCode == Keyboard.UP){ upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){ downPressed = true; if(doorOpen && player.hitTestObject(back.other.lockedDoor)){ //proceed to the next level if the player is touching an open door nextLevel(); } } }
function keyUpHandler(e:KeyboardEvent😞void{ if(e.keyCode == Keyboard.LEFT){ leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){ rightPressed = false;
} else if(e.keyCode == Keyboard.UP){ upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){ downPressed = false; } }
|