So I know this is all really crappy codes and probably poorly done, but I need some help and clarification.
DocumentClass:
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import Classes.Levels
import Classes.GlobalVariables
public class DocumentClass extends MovieClip
{
public var gameTimer:Timer;
private var LevelActionScript;
public function DocumentClass()
{
GlobalVariables.vars.character = new Character();
addChild( GlobalVariables.vars.character );
LevelActionScript = new Levels();
LevelActionScript.Levels();
/*gameTimer = new Timer(500);
gameTimer.addEventListener( TimerEvent.TIMER, CharacterMovement );
gameTimer.start();
}*/
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var mainSpeed:Number = 7;
var mainJumping:Boolean = false;
var jumpSpeedLimit:int = 15;
var jumpSpeed:Number = jumpSpeedLimit;
GlobalVariables.vars.character .addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void
{
if (leftKeyDown)
{
GlobalVariables.vars.character .x -= mainSpeed;
}
if (rightKeyDown)
{
GlobalVariables.vars.character .x += mainSpeed;
}
if (upKeyDown || mainJumping)
{
mainJump();
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown = true;
}
if (event.keyCode == 38 || event.keyCode == 87)
{
upKeyDown = true;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown = true;
}
if (event.keyCode == 40 || event.keyCode == 83)
{
downKeyDown = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown = false;
}
if (event.keyCode == 38 || event.keyCode == 87)
{
upKeyDown = false;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown = false;
}
if (event.keyCode == 40 || event.keyCode == 83)
{
downKeyDown = false;
}
}
function mainJump():void
{
if (! mainJumping)
{
mainJumping = true;
jumpSpeed = jumpSpeedLimit * -1;
GlobalVariables.vars.character .y += jumpSpeed;
}
else
{
if (jumpSpeed < 0)
{
jumpSpeed *= 1 - jumpSpeedLimit / 75;
if (jumpSpeed > - jumpSpeedLimit / 5)
{
jumpSpeed *= -1;
}
}
if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
{
jumpSpeed *= 1 + jumpSpeedLimit / 50;
}
GlobalVariables.vars.character .y += jumpSpeed;
if (GlobalVariables.vars.character .y >= stage.stageHeight - GlobalVariables.vars.character .height)
{
mainJumping = false;
GlobalVariables.vars.character .y = stage.stageHeight - GlobalVariables.vars.character .height;
}
}
}
}
}
}
Levels:
package
{
import flash.display.Sprite;
import flash.display.MovieClip;
public class Levels extends MovieClip
{
//LEVEL VARIABLES
//the current lvl
var lvlCurrent:int = 1;
/*The key for the level arrays:
1: Regular Block
X: Main Character
*/
//this variable will hold the character
var X:String = 'MAIN';
//the array for level 1
var lvlArray1:Array = new Array(
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
);
public function Levels()
{
//creating the level
//this guy will hold all of the blocks
var blockHolder:Sprite = new Sprite();
//then we add him to stage
//current row that we are creating
var row:int = 0;
addChild(blockHolder);
function createLvl():void
{
//getting the current level that we are on
var lvlArray:Array = MovieClip(root)['lvlArray' + lvlCurrent];
//we have to find how far this level goes
//this will be used so we know when to move to the next row
//there will always be 16 rows, so this is how we find it out
//of course, this will make the lvl formatting very strict
var lvlColumns:int = Math.ceil(lvlArray.length / 16);
//now we must create the level
for (var i:int = 0; i<lvlArray.length; i++)
{
if (lvlArray* == 1)
{
//checking if we move onto the next row
//this checks if i is divisible by the # of columns
if (i / lvlColumns == int(i / lvlColumns))
{
row++;
}
//making a new block
var newBlock:Block = new Block();
//drawing the block
newBlock.graphics.beginFill(0xFFFFFF,1);
//turning the shape into a square;
newBlock.graphics.drawRect(0,0,25,25);
//change the coordinates of the block;
newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
newBlock.y = (row-1)*newBlock.height;
//then finally adding it to stage
blockHolder.addChild(newBlock);
}
else if (lvlArray* == 'MAIN')
{
GlobalVariables.vars.character.x = (i-(row-1)*lvlColumns)*newBlock.width;
GlobalVariables.vars.character.y = (row-1)*newBlock.height;
}
}
//reset the row for another use
row = 0;
}
//running the createlvl funciton
createLvl();
}
}
}
GlobalVariables:
package
{
public class GlobalVariables
{
public static var vars:Object = {};
}
}
Block:
package{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.*;
//sprites are just movieclips without any frames in them
public class Block extends Sprite{
//_root will signify the root of the document
private var _root:Object;
public function Block(){
//this code will only be run once
addEventListener(Event.ADDED, beginClass);
//this code will constantly be run
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void{
//defining the root of the document
_root = MovieClip(root);
}
private function eFrame(event:Event):void{
}
}
}
The error I’m getting:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Function/<anonymous>()
at Levels()
at DocumentClass()
So please help me out, much thanks in advance.