Null object error; cannot access it - AS3 - actionscript-3

I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.

You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.

You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player

Related

How to add score when collecting things (Actionscript 3 Adobe Animate)?

How to create an incremented score after he pickup the rubbish? I am very new on this thing and need help on coding.
My code below is on a separated .as file (movieclip).
As you can see, there are codes for when the player hittestobject, the "rubbish" will go away but how to apply a new code for score to that specific function? so the score will be incremented each time he pick up the rubbish.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class mazeClass extends MovieClip {
var mouseUp:Boolean = false;
var mouseDown:Boolean = false;
var mouseLeft:Boolean = false;
var mouseRight:Boolean = false;
var speed:Number = 5;
public function mazeClass() {
// constructor codea
stage.addEventListener(KeyboardEvent.KEY_DOWN, CheckDownKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, CheckUpKeys);
stage.addEventListener(Event.ENTER_FRAME, updatePos);
}
private function CheckDownKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =true;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =true;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =true;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =true;
}
}
private function CheckUpKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =false;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =false;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =false;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =false;
}
}
private function updatePos(e:Event){
if(mouseUp == true){
if(!wall.hitTestPoint(jacob.x,jacob.y-22, true)){
jacob.y -= speed;
}
}
if(mouseDown == true){
if(!wall.hitTestPoint(jacob.x, jacob.y+22, true)){
jacob.y += speed;
}
}
if(mouseLeft == true){
if(!wall.hitTestPoint(jacob.x-22, jacob.y, true)){
jacob.x -= speed;
}
}
if(mouseRight == true){
if(!wall.hitTestPoint(jacob.x+22, jacob.y, true)){
jacob.x += speed;
}
}
if(jacob.hitTestObject(a1)){
a1.x = a1.y = -1000;
_root.score = 0;
_root.score++;
score.text = _root.score;
}
if(jacob.hitTestObject(a2)){
a2.x = a2.y = -1000;
}
if(jacob.hitTestObject(a3)){
a3.x = a3.y = -1000;
}
if(jacob.hitTestObject(a4)){
a4.x = a4.y = -1000;
}
}
}
}

Trying to add keyboard events to a child movieclip I call to the stage in code

So this is my first semester in school coding and right now I'm trying to make a little 2D game with a character who I need to move up and down.
So far, I've been able to create a titlescreen and then when you click start it goes to the next screen, where my main character is.
I add him to the stage manually through the code, and then I tried to make him move up down left and right with the arrow keys but he only appears, and does not move.
This is my code so far
package lib.fly
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class FlyGame extends MovieClip
{
public var mainCharacter:MovieClip;
public var vx:Number;
public var vy:Number;
public function FlyGame()
{
trace ("Initiate");
init();
}
private function init():void
{
vx = 0;
vy = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
stage.addEventListener(KeyboardEvent.KEY_UP, dontMove);
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
trace ("Keyboard Event Listeners");
}
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = - 5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
private function dontMove(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
vy = 0;
}
}
public function onEnterFrame(event:Event):void
{
mainCharacter = new BoyFlying();
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.x += vx;
mainCharacter.y += vy;
addChild(mainCharacter);
}
}
}
The output produces the trace statements up until my "Actual Keyboard Events"
Sorry, I'm brand new to this so any help would be appreciated. Thank you for your time
Try this logic below and see if it helps your program to work as expected...
Adjust function init()
private function init():void
{
vx = vy = 0; //chain them since same value
mainCharacter = new BoyFlying(); //create once here and control in other functions
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.addEventListener(Event.ENTER_FRAME, onEnterFrame);
addChild(mainCharacter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
//stage.addEventListener(KeyboardEvent.KEY_UP, dontMove); //what is it good for?
trace ("added... Keyboard Event Listeners");
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
}
Adjust function moveAround()
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{ vx -= 5; }
if (event.keyCode == Keyboard.RIGHT)
{ vx += 5; }
if (event.keyCode == Keyboard.UP)
{ vy -= 5; }
if (event.keyCode == Keyboard.DOWN)
{ vy += 5; }
}
Adjust function onEnterFrame()
public function onEnterFrame(event:Event):void
{
//# no need for += here since function moveAround() changes these vx and vy values on key press
//# infact your character could be moved (x/y) just by keyboard event instead of per FPS screen update (eg: not with EnterFrame)
mainCharacter.x = vx;
mainCharacter.y = vy;
}

Flash Game Actionscript-3 Output Error #1009

I am making my own game using Actionscript 3 for coding in Flash. The game is about a Runner Character just like Super Mario and Stickyman, who just run, jump, dies.. and so on..
After being very organized by dividing each window of the game in a different Scene, I was still having the problem of executing the piece of code "gotoAndStop()".. That's why I decided to make it much simple by using only one Scene, but each wind of the game in a different frame in the Main Timeline. So I worked on the root, but still getting the same problem that stuck in a white screen!
Anyways, one of the main problem that I am facing here is the Error #1009. Even when I tried to read other topics with the same issue but I didn't find the answer that suites my case.
This is the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HR4_fla::MainTimeline/movePlayer()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Apple/update()
Here is the Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;
stop();
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var score:int = 0;
var lives:int = 3;
player_mc.health = 100;
player_mc.dead = false;
//var TouchRestartBox:Boolean = false;
var playerSpeed:Number = 8;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump: Boolean = false;
//var appleCount:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(event:Event):void
{
scrollRect = new Rectangle(player_mc.x - stage.stageWidth/2, player_mc.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
//PressKey function here
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
//ReleaseKey function here
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
//stage.addEventListener(Event.ENTER_FRAME, GameOver);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
function movePlayer(event:Event):void
{
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x+= playerSpeed;
player_mc.scaleX = 0.59;
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x-= playerSpeed;
player_mc.scaleX = -0.59;
}
if(upKeyIsDown && canJump)
{
player_mc.gotoAndStop(3);
yVelocity = -15;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity = -13;
canDoubleJump = false;
}
yVelocity +=gravity;
if(!floor_mc.hitTestPoint(player_mc.x,player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for(var i:int=0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
for(var j:int=0; j<=2; j++)
{
if(rb.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.x = -1703.35;
player_mc.y = 322.1;
player_mc.scaleX = 0.59;
lives = lives - 1;
}
if(lives == 0)
{
// GameOver();
// remove all the event listeners
// stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
gotoAndStop(124);
//(root as MovieClip).gotoAndStop(124);
}
}
// appleCount_txt.text = "Apples:" + appleCount;
}
/*function GameOver()
{
// lives = 3;
// remove all the event listeners
stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
// player_mc.stop();
gotoAndStop(1); // this has your "dead" screen on it.
}*/
Can anyone help me solving this problem please?
Thanks
There's a listener still getting executed after the frame change. In that handler, an object is referenced that does not exist on that frame and is thus null.
Changing frames only provides visual changes, not necessarily a complete change of state.

Adobe Flash CS6 AS3: KeyUp Handler triggers immediately after KeyDown Handler

So in my code I was attempting to make it where you had two players, each player had their own key down and key up handlers, clearly labelled in my attached code, and in their I handle the movement, player one has WSAD and player two uses the Left, Right, Up and Down keys, however, when ever I press the relative key down handler, it immediately fires the key up handler straight after, and I do not know why, here is my code, if anyone could help that would be great! :)
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip{
var playerOneLeft:Boolean = new Boolean(false);
var playerOneRight:Boolean = new Boolean(false);
var playerOneUp:Boolean = new Boolean(false);
var playerOneDown:Boolean = new Boolean(false);
var playerTwoLeft:Boolean = new Boolean(false);
var playerTwoRight:Boolean = new Boolean(false);
var playerTwoUp:Boolean = new Boolean(false);
var playerTwoDown:Boolean = new Boolean(false);
var playerSpeed:int = 5;
var gamePage:GamePage;
public function Main(){
gamePage = new GamePage();
addChild(gamePage);
addAllPlayerHandlers();
}
public function playerOneKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = true;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = true;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = true;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = true;
trace("s");
}
}
public function playerOneKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.A){
playerOneLeft = false;
trace("a");
}
else if(event.keyCode == Keyboard.D){
playerOneRight = false;
trace("d");
}
else if(event.keyCode == Keyboard.W){
playerOneUp = false;
trace("w");
}
else if(event.keyCode == Keyboard.S){
playerOneDown = false;
trace("s");
}
}
public function playerTwoKeyDownHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = true;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = true;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = true;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = true;
trace("down");
}
}
public function playerTwoKeyUpHandler(event:KeyboardEvent){
if(event.keyCode == Keyboard.LEFT){
playerTwoLeft = false;
trace("left");
}
else if(event.keyCode == Keyboard.RIGHT){
playerTwoRight = false;
trace("right");
}
else if(event.keyCode == Keyboard.UP){
playerTwoUp = false;
trace("up");
}
else if(event.keyCode == Keyboard.DOWN){
playerTwoDown = false;
trace("down");
}
}
public function playerMovementHandler(event:Event):void{
if(playerOneLeft){
gamePage.playerOne.x -= playerSpeed;
}
else if(playerOneRight){
gamePage.playerOne.x += playerSpeed;
}
else if(playerOneUp){
gamePage.playerOne.y += playerSpeed;
}
else if(playerOneDown){
gamePage.playerOne.y -= playerSpeed;
}
if(playerTwoLeft){
gamePage.playerTwo.x -= playerSpeed;
}
else if(playerTwoRight){
gamePage.playerTwo.x += playerSpeed;
}
else if(playerTwoUp){
gamePage.playerTwo.y += playerSpeed;
}
else if(playerTwoDown){
gamePage.playerTwo.y -= playerSpeed;
}
}
public function playerOneCollision(event:Event):void{
if(gamePage.playerOne.hitTestObject(gamePage.leftWall)){
gamePage.playerOne.x += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.rightWall)){
gamePage.playerOne.x -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.topWall)){
gamePage.playerOne.y -= playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.bottomWall)){
gamePage.playerOne.y += playerSpeed;
}
if(gamePage.playerOne.hitTestObject(gamePage.middleWall)){
gamePage.playerOne.x -= playerSpeed;
}
}
public function playerTwoCollision(event:Event):void{
if(gamePage.playerTwo.hitTestObject(gamePage.leftWall)){
gamePage.playerTwo.x += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.rightWall)){
gamePage.playerTwo.x -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.topWall)){
gamePage.playerTwo.y -= playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.bottomWall)){
gamePage.playerTwo.y += playerSpeed;
}
if(gamePage.playerTwo.hitTestObject(gamePage.middleWall)){
gamePage.playerTwo.x += playerSpeed;
}
}
public function addPlayerMovementHandler(){
stage.addEventListener(Event.ENTER_FRAME,playerMovementHandler);
}
public function addPlayerKeyHandlers(){
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyUpHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyUpHandler);
}
public function addPlayerCollisionHandlers(){
gamePage.playerOne.addEventListener(Event.ENTER_FRAME,playerOneCollision);
gamePage.playerTwo.addEventListener(Event.ENTER_FRAME,playerTwoCollision);
}
public function addAllPlayerHandlers(){
addPlayerKeyHandlers();
addPlayerCollisionHandlers();
addPlayerMovementHandler();
}
}
when ever I press the relative key down handler, it immediately fires the key up handler straight after
This is because you registered them as
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerOneKeyUpHandler); // this is for type KEY_DOWN, but the handler is "Up"
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyDownHandler); //that looks suspicious, too
stage.addEventListener(KeyboardEvent.KEY_UP,playerTwoKeyUpHandler);
The lesson you should learn is that your copy & paste approach to writing code is a bad practice. You should not repeat yourself. All this duplicated code leads to problems like the one at hand.

Undefined hitTestPoint error

When I have this code, it comes up with this error "1061: Call to a possibly undefined method hitTestPoint through a reference with static type Class."
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
public function Code() {
// constructor code
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
charSpeed -= 10;
}
if (evt.keyCode == Keyboard.RIGHT){
charSpeed += 10;
}
if (evt.keyCode == Keyboard.DOWN){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
charSpeed = 0;
}
if (evt.keyCode == Keyboard.RIGHT){
charSpeed = 0;
}
}
function loop(evt:Event){
player.x = velocity;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
The option I had was adding a MovieClip variable to my platform, and that did remove this error, but I wasn't sure how to link my actual instance of platform to my variable. Oh and by the way the platform's instance is platform. If anyone has any clues on how to fix this error, that would be great.
You may set platform value in the constructor
public function class Code {
//assume Platform is the class type,remember to import it
private var platform:Platform;
public function Code(value:Platform) {
platform = value;
}
}
When you create Code instance
var code:Code = new Code(platform);//platform is the instance