I'm currently working on a Flash platform game and I'm trying to make each level have its own class that defines a hitTest function (Each class is linked to the MovieClip of the level), which would allow the character to walk on the level. Whenever I try to import the subclass into the Document class, errors start popping up and it is driving me crazy (Error 1120: Access of undefined property)!
Any kind of help would be appreciated!
Document Class (Class_Main.as):
package
{
import flash.events.*;
import flash.display.*;
import flash.geom.Point;
import Level1;
import Level2;
public class Class_Main extends MovieClip
{
public var leftKeyDown:Boolean = false;
public var rightKeyDown:Boolean = false;
public var upKeyDown:Boolean = false;
public var downKeyDown:Boolean = false;
public var onGround:Boolean = true;
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public var mainSpeed:Number = 3.75;
public var frictionPower:Number = 0.9;
public var jumpPower:Number = 15;
public var gravityPower:Number = 0.7;
public var terminalVelocity:Number = 75;
public var Level_1:Level1 = new Level1();
public var Level_2:Level2 = new Level2();
public function Class_Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
// constructor code
}
public function init(event:Event)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
stage.addEventListener(Event.ENTER_FRAME,hitTest);
stage.addEventListener(Event.ENTER_FRAME,vCamMovement);
stage.addEventListener(Event.ENTER_FRAME,Main);
}
public function Main(event:Event):void
{
moveCharacter();
dynamicMovement();
}
public function checkKeysDown(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftKeyDown = true;
}
if (event.keyCode == 38)
{
upKeyDown = true;
}
if (event.keyCode == 39)
{
rightKeyDown = true;
}
if (event.keyCode == 40)
{
downKeyDown = true;
}
}
public function checkKeysUp(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftKeyDown = false;
}
if (event.keyCode == 38)
{
upKeyDown = false;
}
if (event.keyCode == 39)
{
rightKeyDown = false;
}
if (event.keyCode == 40)
{
downKeyDown = false;
}
}
public function moveCharacter():void
{
if (leftKeyDown)
{
mcMain.scaleX = -1;
xSpeed -= mainSpeed;
}
if (rightKeyDown)
{
mcMain.scaleX = 1;
xSpeed += mainSpeed;
}
if (leftKeyDown && onGround || rightKeyDown && onGround)
{
mcMain.gotoAndStop(2);
}
if (upKeyDown)
{
ySpeed -= jumpPower;
}
if (upKeyDown && leftKeyDown)
{
ySpeed -= 0;
xSpeed -= 10;
}
if (upKeyDown && rightKeyDown)
{
ySpeed -= 0;
xSpeed += 10;
}
if (xSpeed > 3 && ! onGround || xSpeed < -3 && ! onGround)
{
if (mcMain.currentFrame == 2)
{
mcMain.gotoAndStop(5);
}
}
if (ySpeed < -0.5 && ! onGround)
{
mcMain.gotoAndStop(4);
}
else if (ySpeed > 0.5 && ! onGround)
{
mcMain.gotoAndStop(5);
}
if (mcMain.currentFrame == 5 && onGround)
{
mcMain.gotoAndStop(1);
}
if (mcMain.currentFrame == 2)
{
if (! leftKeyDown && ! rightKeyDown)
{
mcMain.gotoAndStop(3);
}
}
if (mcMain.currentFrame == 3)
{
if (mcMain.skidAnimation.currentFrame == mcMain.skidAnimation.totalFrames)
{
mcMain.gotoAndStop(1);
}
}
//if (! leftKeyDown && ! rightKeyDown && ! upKeyDown)
//{
//mcMain.gotoAndStop(1);
//}
}
public function dynamicMovement():void
{
mcMain.x += xSpeed;
xSpeed *= frictionPower;
if (xSpeed > 7)
{
xSpeed = 7;
}
if (xSpeed < -7)
{
xSpeed = -7;
}
mcMain.y += ySpeed;
ySpeed += gravityPower;
if (ySpeed > terminalVelocity)
{
ySpeed = terminalVelocity;
}
}
public function hitTest(event:Event)
{
spawnArea.visible = false;
mcMain.mcMainHitArea.visible = false;
Level_1.wallCollision.visible = false;
Level_1.deathArea.visible = false;
Level_1.goalArea.goalHitArea.visible = false;
while (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y,true))
{
mcMain.y--;
}
if (! Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 1,true))
{
//upKeyDown = false;
if (! Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 5,true))
{
upKeyDown = false;
onGround = false;
}
}
if (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 1,true))
{
ySpeed = 0;
if (Level_1.wallCollision.hitTestPoint(mcMain.x,mcMain.y + 5,true))
{
onGround = true;
}
}
if (Level_1.wallCollision.hitTestPoint(mcMain.x - 9,mcMain.y - 25,true))
{
mcMain.x + 9;
mcMain.y + 11;
upKeyDown = false;
leftKeyDown = false;
}
if (Level_1.wallCollision.hitTestPoint(mcMain.x + 9,mcMain.y - 25,true))
{
mcMain.x - 9;
mcMain.y - 11;
upKeyDown = false;
rightKeyDown = false;
}
if (Level_1.wallCollision.hitTestPoint(mcMain.x - 9,mcMain.y - 11,true))
{
xSpeed = 0;
leftKeyDown = false;
}
if (Level_1.wallCollision.hitTestPoint(mcMain.x + 9,mcMain.y - 11,true))
{
xSpeed = 0;
rightKeyDown = false;
}
if (Level_1.deathArea.hitTestPoint(mcMain.x,mcMain.y + 1,true))
{
mcMain.x = spawnArea.x;
mcMain.y = spawnArea.y;
}
if (mcMain.hitTestObject(Level_1.goalArea.goalHitArea))
{
if (stage.contains(Level_1))
{
this.removeChild(Level_1);
}
addChild(Level_2);
Level_2.x = -400;
Level_2.y = -700;
}
}
public function vCamMovement(event:Event):void
{
/*for (var i:int = 0; i < this.numChildren - 1; i++)
{
this.getChildAt(i).x -= xSpeed;
//levelObjects.getChildAt(i).y -= ySpeed;
}*/
Level_1.x += stage.stageWidth * 0.5 - mcMain.x;
Level_1.y += stage.stageHeight * 0.5 - mcMain.y;
Level_2.x += stage.stageWidth * 0.5 - mcMain.x;
Level_2.y += stage.stageHeight * 0.5 - mcMain.y;
spawnArea.x += stage.stageWidth * 0.5 - mcMain.x;
spawnArea.y += stage.stageHeight * 0.5 - mcMain.y;
mcMain.x = stage.stageWidth * 0.5;
mcMain.y = stage.stageHeight * 0.5;
}
}
}
Level 2 (Level2.as):
package
{
import flash.display.MovieClip;
import flash.events.*;
import Class_Main;
import Level2Walls;
public class Level2 extends MovieClip
{
public var classMain:Class_Main = new Class_Main ;
public var level2Walls:Level2Walls = new Level2Walls ;
public function Level2()
{
stage.addEventListener(Event.ENTER_FRAME,hitTest_2);
// constructor code
}
public function hitTest_2(event:Event)
{
while (level2Walls.hitTestPoint(mcMain.x,mcMain.y,true))
{
mcMain.y--;
}
if (! level2Walls.hitTestPoint(mcMain.x,mcMain.y + 1,true))
{
//upKeyDown = false;
if (! level2Walls.hitTestPoint(mcMain.x,mcMain.y + 5,true))
{
upKeyDown = false;
onGround = false;
}
}
if (level2Walls.hitTestPoint(mcMain.x,mcMain.y + 1,true))
{
ySpeed = 0;
if (level2Walls.hitTestPoint(mcMain.x,mcMain.y + 5,true))
{
onGround = true;
}
}
if (level2Walls.hitTestPoint(mcMain.x - 9,mcMain.y - 25,true))
{
mcMain.x + 9;
mcMain.y + 11;
upKeyDown = false;
leftKeyDown = false;
}
if (level2Walls.hitTestPoint(mcMain.x + 9,mcMain.y - 25,true))
{
mcMain.x - 9;
mcMain.y - 11;
upKeyDown = false;
rightKeyDown = false;
}
if (level2Walls.hitTestPoint(mcMain.x - 9,mcMain.y - 11,true))
{
xSpeed = 0;
leftKeyDown = false;
}
if (level2Walls.hitTestPoint(mcMain.x + 9,mcMain.y - 11,true))
{
xSpeed = 0;
rightKeyDown = false;
}
}
}
}