17.5.11

Okay. Here is the code that didn't work last time.

package  {

import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;


import flash.events.KeyboardEvent;

public class PlaneGame extends MovieClip {

public var level:Level1;
public var player:Plane;


public function PlaneGame() {
creategame();
createplayer();
addlisteners();

}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);

}


private function createplayer():void
{

var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);


}

private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}


private function movelevel(e:Event):void
{


level.x-=10
}

private function moveplayer(k:KeyboardEvent):void

{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}


}

}



I'm going to remove the function "add listeners" from the constructor, add those listeners to the creategame and createplayer functions and then lead them to the resultant functions. 

6.5.11

Game Doesn't Work!

As was written in my previous post, my Game didn't work. There were two compiler errors:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

From just looking at these two errors, I have discerned that the cause of the problem is the "null object reference", which I assume means the the Actionscript file is calling objects that were not even instantiated yet or added to the Display list- one or the other.

Well, looking back at the code, I can see that the main variables in the game- na aircraft and na stage- have already been instantiated, and therefore the problem must be that the objects were not added to the display list before they were worked on. So that's my answer.

First Game OMG!

Yes well I tried to make a game a week ago. Here is the code:


package  {

import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;


import flash.events.KeyboardEvent;

public class PlaneGame extends MovieClip {

public var level:Level1;
public var player:Plane;


public function PlaneGame() {
creategame();
createplayer();
addlisteners();

}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);

}


private function createplayer():void
{

var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);


}

private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}


private function movelevel(e:Event):void
{


level.x-=10
}

private function moveplayer(k:KeyboardEvent):void

{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}


}

}


Not surprisingly, the code didn't work. I got two compiler errors. These are the errors:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()


What do I want to do with this, huh?