PDA

View Full Version : 1120: Access of undefined property climber


Falcs27
12-03-2008, 07:21 PM
Hi everyone,

I'm currently working my way through a Flash assignment to create a simple 2d game. I have been following an actionscript 3.0 book, but as usual code is my achilles heel.

Anyway, i'm seriously stuck with my application... when i run the game in strict mode (in publish settings, actionscript 3.0 settings). I get LOTS of:

1120: Access of undefined property climber (starting at line 132)
1120: Access of undefined property lasTime
1120: Access of undefined property gameMode
and a few 1180: Call to a possibly undefined method addChild

...anyway, a bunch of these guys that totals 93 compile errors :(

However, if i turn OFF the strict mode, i get the following errors:

Warning: 1060: Migration issue: The method gotoAndStop is no longer supported (line 344)
Warning: 1060: Migration issue: The method nextFrame is no longer supported (line 347
and lastly 5006: An actionscript file can not have more than one externally visible definition: movePoliceman, cleanUp


As you can see i'm in a fair pickle here and really needing help...my deadline is due tommorrow, although a 24hr grace period can be used, so friday is my last stop :/

If anyone can help me out, i'd be so grateful...as i said before, code is not my forte and i've probably made a few idiotic mistakes that is causing 93 errors to occur.


I'll add the actionscript code in the next post, as i've posted too many chars in this one....

Falcs27
12-03-2008, 07:22 PM
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.getTimer;

public class AdrenalineJunky extends MovieClip {

// screen constants
static const edgeDistance:Number = 100;

// object array
private var fixedObjects:Array;

// hero and enemies
private var climber:Object;
private var policeman:Array;

// game state
private var gameMode:String = "start";
private var lastTime:Number = 0;

// start game
public function startAdrenalineJunky() {
gameMode = "play";
trace("!");
}

// start level
public function startGameLevel() {

// create characters
createClimber();
addPoliceman();

// examine level and note all objects
examineLevel();

// add listeners
this.addEventListener(Event.ENTER_FRAME,gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyD ownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpF unction);

// set game state
gameMode = "play";
}

// creates the climber object and sets all properties
public function createClimber() {
climber = new Object();
climber.mc = gamelevel.climber;
climber.dx = 0.0;
climber.dy = 0.0;
climber.direction = 1;
climber.animstate = "stand";
climber.climbAnimation = new Array(2,3);
climber.animstep = 0;
climber.moveLeft = false;
climber.moveRight = false;
climber.moveUp = false;
climber.moveDown = false;
climber.climbSpeed = .10;
climber.width = 20.0; //height and width constants for determining collisions
climber.height = 40.0;
climber.startx = climber.mc.x;
climber.starty = climber.mc.y;
}

// finds all enemies in the level and creates an object for each
public function addPoliceman() {
policeman = new Array();
var i:int = 1;
while (true) {
if (gamelevel["police"+i] == null) break;
var police = new Object();
police.mc = gamelevel["police"+i];
police.dx = 0.0;
police.dy = 0.0;
police.inAir = false;
police.direction = 1;
police.animstate = "stand"
police.climbAnimation = new Array(1, 2);
police.animstep = 0;
police.moveRight = true;
police.moveLeft = false;
police.climbSpeed = .08;
police.width = 30.0;
police.height = 30.0;
policeman.push(police);
i++;
}
}

// look at all level children and note walls, floors and items
public function examineLevel() {
fixedObjects = new Array();
for(var i:int=0;i<this.gamelevel.numChildren;i++) {
var mc = this.gamelevel.getChildAt(i);

// add floors and walls to fixedObjects
if ((mc is Floor) || (mc is Wall)) {
var floorObject:Object = new Object();
floorObject.mc = mc;
floorObject.leftside = mc.x;
floorObject.rightside = mc.x+mc.width;
floorObject.topside = mc.y;
floorObject.bottomside = mc.y+mc.height;
fixedObjects.push(floorObject);
}

}
}

// note key presses, set hero properties
public function keyDownFunction(event:KeyboardEvent) {
if (gameMode != "play") return; // don't move until in play mode

if (event.keyCode == 37) {
climber.moveLeft = true;
} else if (event.keyCode == 39) {
climber.moveRight = true;
} else if (event.keyCode == 38) {
climber.moveUp = true;
} else if (event.keyCode == 40) {
climber.moveDown = true;
}
}
}

public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
climber.moveLeft = false;
} else if (event.keyCode == 39) {
climber.moveRight = false;
} else if (event.keyCode == 38) {
climber.moveUp = false;
} else if (event.keyCode == 40) {
climber.moveDown = false;
}

}

// perform all game tasks
public function gameLoop(event:Event) {

// get time differentce
if (lastTime == 0) lastTime = getTimer();
var timeDiff:int = getTimer()-lastTime;
lastTime += timeDiff;

// only perform tasks if in play mode
if (gameMode == "play") {
moveCharacter(climber,timeDiff);
movePoliceman(timeDiff);
//movePlanes(timeDiff);
checkCollisions();
scrollWithClimber();
}
}

// loop through all enemies and move them
public function movePoliceman(timeDiff:int) {
for(var i:int=0;i<policeman.length;i++) {

// move
moveCharacter(policeman[i],timeDiff);

// if hit ground or roof, turn around
if (policeman[i].hitRoof) {
policeman[i].moveUp = false;
policeman[i].moveDown = true;
} else if (policeman[i].hitGround) {
policeman[i].moveUp = true;
policeman[i].moveDown = false;
}
}
}

// primary function for character movement
public function moveCharacter(char:Object,timeDiff:Number) {
if (timeDiff < 1) return;

// assume character pulled down by gravity
/*var verticalChange:Number = char.dy*timeDiff + timeDiff*gravity;
if (verticalChange > 15.0) verticalChange = 15.0;
char.dy += timeDiff*gravity;*/

// react to changes from key presses
var horizontalChange = 0;
var newAnimState:String = "stand";
var newDirection:int = char.direction;
if (char.moveLeft) {
// climb left
horizontalChange = -char.walkSpeed*timeDiff;
newAnimState = "climb";
newDirection = -1;
} else if (char.moveRight) {
// climb right
horizontalChange = char.walkSpeed*timeDiff;
newAnimState = "climb";
newDirection = 1;

} else if (char.moveUp) {
// climb up
verticalChange = char.walkSpeed*timeDiff;
newAnimState = "climb";
newDirection = 1;

} else if (char.moveDown) {
// climb right
verticalChange = char.walkSpeed*timeDiff;
newAnimState = "climb";
newDirection = -1;
}




// find new horizontal position
var newX:Number = char.mc.x + horizontalChange;

// loop through all objects to see if character has bumped into a wall
for(i=0;i<fixedObjects.length;i++) {
if ((newY > fixedObjects[i].topside) && (newY-char.height < fixedObjects[i].bottomside)) {
if ((char.mc.x-char.width/2 >= fixedObjects[i].rightside) && (newX-char.width/2 <= fixedObjects[i].rightside)) {
newX = fixedObjects[i].rightside+char.width/2;
char.hitWallLeft = true;
break;
}
if ((char.mc.x+char.width/2 <= fixedObjects[i].leftside) && (newX+char.width/2 >= fixedObjects[i].leftside)) {
newX = fixedObjects[i].leftside-char.width/2;
char.hitWallRight = true;
break;
}
}
}

// set position of character
char.mc.x = newX;
char.mc.y = newY;


// move along climb cycle
if (char.animstate == "climb") {
char.animstep += timeDiff/60;
if (char.animstep > char.climbAnimation.length) {
char.animstep = 0;
}
char.mc.gotoAndStop(char.climbAnimation[Math.floor(char.animstep)]);

// not climbing, show stand
} else {
char.mc.gotoAndStop(char.animstate);
}

// changed directions
if (newDirection != char.direction) {
char.direction = newDirection;
char.mc.scaleX = char.direction;
}
}

// scroll up or down if needed
public function scrollWithClimber() {
var stagePosition:Number = gamelevel.x+climber.mc.x;
var rightEdge:Number = stage.stageWidth-edgeDistance;
var leftEdge:Number = edgeDistance;
if (stagePosition > rightEdge) {
gamelevel.x -= (stagePosition-rightEdge);
if (gamelevel.x < -(gamelevel.width-stage.stageWidth)) gamelevel.x = -(gamelevel.width-stage.stageWidth);
}
if (stagePosition < leftEdge) {
gamelevel.x += (leftEdge-stagePosition);
if (gamelevel.x > 0) gamelevel.x = 0;
}
}

// check collisions with enemies
public function checkCollisions() {

// enemies
for(var i:int=policeman.length-1;i>=0;i--) {
if (climber.mc.hitTestObject(policeman[i].mc)) {
climberDie();

}
}
}
// remove enemy
public function policeDie(enemyNum:int) {
gamelevel.removeChild(policeman[enemyNum].mc);
policeman.splice(policeNum,1);
}

// enemy got player
public function climberDie() {
// show dialog box
var dialog:Dialog = new Dialog();
dialog.x = 175;
dialog.y = 100;
addChild(dialog);

if (gameMode == "dead") {
dialog.message.text = "Game Over!";
}

climber.mc.gotoAndPlay("die");
}

Falcs27
12-03-2008, 07:23 PM
and the end of the code, sorry there is so much :(


// level over, bring up dialog
public function levelComplete() {
gameMode = "done";
var dialog:Dialog = new Dialog();
dialog.x = 175;
dialog.y = 100;
addChild(dialog);
dialog.message.text = "Roof reached, level complete!";
}

// game over, bring up dialog
public function gameComplete() {
gameMode = "gameover";
var dialog:Dialog = new Dialog();
dialog.x = 175;
dialog.y = 100;
addChild(dialog);
dialog.message.text = "Congratulations, you are the adrenaline junky!";
}

// dialog button clicked
public function clickDialogButton(event:MouseEvent) {
removeChild(MovieClip(event.currentTarget.parent)) ;

// new life, restart, or go to next level
if (gameMode == "dead") {
// reset hero

climber.mc.x = climber.startx;
climber.mc.y = climber.starty;
gameMode = "play";
} else if (gameMode == "gameover") {
cleanUp();
gotoAndStop ("start");
} else if (gameMode == "done") {
cleanUp();
nextFrame();
}

// give stage back the keyboard focus
stage.focus = stage;
}

// clean up game
public function cleanUp() {
removeChild(gamelevel);
this.removeEventListener(Event.ENTER_FRAME,gameLoo p);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,k eyDownFunction);
stage.removeEventListener(KeyboardEvent.KEY_UP,key UpFunction);
}

}