Another "Help me source my Error #1009: Cannot access a property....."
Hi guys, another AS3 newbie here. Just started using AS3 for a Uni module and though I'm making fairly good progress myself I can tell I don't really have a head for programming (thank god it's just 1 module).
I've been getting a few of these 1009 errors recently which, as I'm sure you all know, are rather annoying to try and trace since they are pretty vague.
Currently I'm building a simple base defense/platformer/beat'em'up. I have a lot of character movement in and I am now working on the enemy reactions to hits etc, that's where things started to go wrong and now I've got rage block because I just can't figure it out myself.
Please people, I beg of you to help me with this unholy nightmare I'm in.
Oh yeah and.... deadline is Monday. Thanks. ![]()
//VARIABLES
///////////
//creates a new variable called enemyArray of typ Array that is a new Array var
var enemyArray:Array = new Array();
//creates a variable called score that is initially zero
var score:Number = 0;
//creates a Timer called spawnTimer that is a new Timer that is 3 seconds long
var spawnTimer:Timer = new Timer(3000);
//creates an array'keysArray' that stores 4 Boolean values which all start false
var keysArray:Array=new Array(false, false, false, false, false); //(left, right, up, down, space)
//creates a variable 'moveSpeed' that has a value of X
var moveSpeed:int=2;
var playerySpeed:Number; //variable ‘ySpeed’ of type number with NO value...
var playerxSpeed:Number; //variable ‘xSpeed’ of type number with NO value...
var playerFlip:Boolean=false;
var playerFalling:Boolean=false;
var playerCanJump:Boolean=false;
var gravity:Number = 0; //variable ‘gravity’ of type number with value 0.3
var ground:Number; //var for ground that accepts a number value
var babansHealth:Number = 100; //var for the health of the bar, set to 100
var playerHealth:Number = 50; //var for the health of the player, set to 50
//FUNCTIONS
///////////
stop(); //tells Flash to stop on the frame in which this line appears
startGame(); //this calls the startGame funtion
stage.focus=stage //this makes sure to focus on stage once frame is entered
//SET GAME UP
/////////////
function startGame():void //a new function called startGame with no in/output
{
addToScore(0); //this makes the score display without adding any points
//adds and EventListener to spawnTimer to here whent he timer restarts
spawnTimer.addEventListener(TimerEvent.TIMER, spawnEnemy);
spawnTimer.start(); //physically starts the spawnTimer counting
ground = player.y; //sets the ground to the player's Y pos
//adds an EventListener that listens for a new frame being entered and which calls the update function
stage.addEventListener(Event.ENTER_FRAME, playerUpdate);
//adds an EventListener that is called when a key is pressed down
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
//adds an EventListener that is called when a key is released
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
trace("Game Started"); //prints "Game Started" in the output panel
}
//SPAWN NEW ENEMY
/////////////////
function spawnEnemy(event:TimerEvent):void //a function called spawnEnemy with no in/output
{
var cat:Enemy = new Enemy(); //a new variable called cat of type Enemy
enemyArray.push(cat); //pushes the new instance of Enemy (cat) onto the Array
//cat.x = 1000; //sets cats X pos to 200
cat.x = randomise(900,1200); //cat's X position now randomised using the function
//cat.y = 500; //sets cats Y pos to 100
cat.y = randomise(500, 600); //cat's Y position now randomised using the function
cat.width = 75; //sets cats width to 50
cat.scaleY = cat.scaleX; //scales cats height with above width
cat.walking.scaleX = -1; //sets the X(horizontal) scale to -1 i.e. -100%(flipped)
stage.addChild(cat); //add the enemy 'cat' as a child of the stage (to see it)
cat.activate(this); //call a function in 'cat' called 'activate' and passes the value 'this'
}
//BAR TAKES HIT
///////////////
function hitBabans():void //new function 'hit' that accepts no values and returns no value
{
//trace("hit"); //print the word "hit" in the Output panel
babansHealth-=10; //minus 10 from the value of health (replace -=10 with -- for -1 health)
healthBar.healthBarGreen.scaleX = babansHealth/100;
if(babansHealth <=0)
{
//trace("lost"); //prints the word 'lost' in the output panel
/*for the number i which we set to 0; while i is smaller
than the number of enemies we have (which will be equal
to enemyArray.length); execute the code in the body and
then add 1 to i (i++) before doing the loop again */
for(var i:int = 0; i <enemyArray.length; i++)
{
//call the shutDown function of the Enemy stored at position i in the enemyArray
enemyArray.shutDown();
}
spawnTimer.removeEventListener(TimerEvent.TIMER, spawnEnemy);
gotoAndStop(2); //tells the main time line to go to and stop on frame 2
}
}
//ADD TO SCORE
//////////////
//new function 'addToScore' that takes a number that will be referred to as points
function addToScore(points:Number):void
{
score += points; //adds the points passed to the function to a variable "score"
score_txt.text = "Defeated : " + score; //updates the text of a score box
}
//RANDOMISER
////////////
/* new function "randomise" which makes two
numbers "min" and "max" and returns a Number */
function randomise(min:Number, max:Number):Number
{
/* creates a variable called num that is equal to a random number between 0-1
multiplied by a range defined as (max -min) and adds the min value so if
Math.random() were nearly 0 then num would still equal roughly */
var num = (Math.random() * (max - min)) + min;
return num; //tells the function to equal whatever value num has
}
//SCREEN BORDER LIMITER
///////////////////////
//function to stop the player leaving the stage boundaries
function limitStageBorder(object:MovieClip)
{
//1.
var objectHalfWidth:uint=player.width/2;
var objectHalfHeight:uint=player.height/2;
//2.
if (player.x+objectHalfWidth>stage.stageWidth)
{
player.x=stage.stageWidth-objectHalfWidth;
}
else if (player.x-objectHalfWidth <0)
{
player.x=0+objectHalfWidth;
}
//3.
if (player.y-objectHalfHeight<0)
{
player.y=0+objectHalfHeight;
}
else if (player.y + objectHalfHeight > stage.stageHeight)
{
player.y=stage.stageHeight-objectHalfHeight;
}
}
//MOVEMENT KEY FUNCTIONS
////////////////////////
//new function 'keyPressed' which handles a KeyboardEvent which we'll refer to as keyEvent and which returns no value
function keyPressed(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode==37)//if key pressed is 'keyCode' 37 i.e. LEFT
{
keysArray[0]=true; //sets the associated array value to true
}
if(keyEvent.keyCode==39) //if key pressed is 'keyCode' 39 i.e. RIGHT
{
keysArray[1]=true; //sets the associated array value to true
}
if(keyEvent.keyCode==38) //if key pressed is 'keyCode' 38 i.e. UP
{
keysArray[2]=true; //sets the associated array value to true
}
if(keyEvent.keyCode==40) //if key pressed is 'keyCode' 40 i.e. DOWN
{
keysArray[3]=true; //sets the associated array value to true
}
if(keyEvent.keyCode==32&&playerCanJump==true) //if key pressed is SPACE
{
keysArray[4]=true;
playerJump();
}
}
//new function 'keyReleased' which handles a KeyboardEvent which we'll refer to as keyEvent and which returns no value
function keyReleased(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode==37) //if key released is 'keyCode' 37 i.e. LEFT
{
keysArray[0]=false; //sets the associated array value to false
}
if(keyEvent.keyCode==39) //if key released is 'keyCode' 39 i.e. RIGHT
{
keysArray[1]=false; //sets the associated array value to false
}
if(keyEvent.keyCode==38) //if key released is 'keyCode' 39 i.e. UP
{
keysArray[2]=false; //sets the associated array value to false
}
if(keyEvent.keyCode==40) //if key released is 'keyCode 40 i.e. DOWN
{
keysArray[3]=false; //sets the associated array value to false
}
if(keyEvent.keyCode==32) //if key released is SPACE
{
keysArray[4]=false; //sets the associated array value to false
}
}
//MOVEMENT KEY EFFECTS
//////////////////////
//new function 'playerUpdate' that handles an 'Event' labelled 'event' which returns no value
function playerUpdate(playerEvent:Event):void //'update' function attached to event listener ENTER_FRAME
{
//NO BUTTONS PRESSED i.e. STANDING STILL
if (keysArray[0]==false&&keysArray[1]==false&&keysArray[2]==false&&keysArray[3]==false&&keysArray[4]==false)
{
if (playerFalling==false)
{
player.gotoAndStop(1);
}
else if (playerFalling==true)
{
player.gotoAndStop(5);
}
}
//MOVING LEFT AND RIGHT!!!
//////////////////////////
//LEFT
//if player is going LEFT
if (keysArray[0]==true&&keysArray[1]==false)
{
{
if (playerFalling==false) //if player is NOT jumping
{
playerxSpeed=8;
player.x -= playerxSpeed; //subtract the moveSpeed from the X position
player.gotoAndStop(2); //stops "player" on his running frame
}
else if (playerFalling==true) //if player IS jumping
{
player.x -= playerxSpeed;
player.gotoAndStop(5);
}
}
playerFlip=true;
}
//RIGHT
//if player is going RIGHT
if (keysArray[0]==false&&keysArray[1]==true)
{
{
if (playerFalling==false) //if player is NOT jumping
{
playerxSpeed=8;
player.x += playerxSpeed; //subtract the moveSpeed from the X position
player.gotoAndStop(2); //stops "player" on his running frame
}
else if (playerFalling==true) //if player IS jumping
{
player.x += playerxSpeed;
player.gotoAndStop(5);
}
}
playerFlip=false;
}
//LEFT AND RIGHT AT SAME TIME FIX!!!!!!!
////////////////////////////////////////
//if the player presses both LEFT and RIGHT
if (keysArray[0]==true&&keysArray[1]==true)
{
player.gotoAndStop(1); //stop on standing frame
}
//ATTACKING!!!
//////////////
if (keysArray[2]==true)
{
if (keysArray[0]==false&&keysArray[1]==false) //if standing still
{
player.gotoAndStop(3); //stop on STANDING ATTACK frame
playerxSpeed=0;
}
}
//BLOCKING!!!
/////////////
if (keysArray[3]==true) //if player IS blocking
{
if (keysArray[0]==false&&keysArray[1]==false) //if standing still
{
player.gotoAndStop(4); //stop on STANDING BLOCK frame
playerxSpeed=0;
}
}
//JUMPING!!
///////////
if(playerFalling==true) //if the player is falling
{
playerySpeed += gravity; //increase the ySpeed by gravity
player.y += playerySpeed; //move player y pos. by ySpeed
player.gotoAndStop(5);
}
if (player.y >= ground)
{
playerFalling=false;
playerCanJump=true;
}
//CHARACTER FLIPPER!!!
//////////////////////
if(playerFlip == true)
{
if (player.currentFrame==1) //player standing frame
{
player.playerStand.scaleX = -1; //flips player left
}
else if (player.currentFrame==2) //player running frame
{
player.playerRun.scaleX = -1; //flips player left
}
else if (player.currentFrame==3) //player punching frame
{
player.playerAttack.scaleX = -1; //flips player left
}
else if (player.currentFrame==4) //player blocking frame
{
player.playerBlock.scaleX = -1; //flips player left
}
else if (player.currentFrame==5) //player jumping frame
{
player.playerJump.scaleX = -1; //flips player left
}
}
if(playerFlip == false)
{
if (player.currentFrame==1) //player standing frame
{
player.playerStand.scaleX = 1; //flips player left
}
else if (player.currentFrame==2) //player running frame
{
player.playerRun.scaleX = 1; //flips player left
}
else if (player.currentFrame==3) //player punching frame
{
player.playerAttack.scaleX = 1; //flips player left
}
else if (player.currentFrame==4) //player blocking frame
{
player.playerBlock.scaleX = 1; //flips player left
}
else if (player.currentFrame==5) //player jumping frame
{
player.playerJump.scaleX = 1; //flips player left
}
}
limitStageBorder(player); //runs the limitStageBorder function each frame
}
//JUMP FUNCTION!!!
//////////////////
function playerJump():void
{
playerFalling = true; //considers the player to be falling
playerCanJump = false;
playerySpeed = -10; //sets the ySpeed to -X i.e. X pixels per frame upwards
playerxSpeed = 5; //sets the xSpeed to X
gravity = 0.6;
Here is the code that is on my current enemy object,
//Imports
import flash.events.Event;
import flash.events.MouseEvent;
//Variables
var alive:Boolean = true; //creates a variable "alive" of type Boolean and sets it to true
var xSpeed:Number = -3; //create a new variable called xSpeed and set it to -3
var ySpeed:Number = 0; //create a new variable called ySpeed and set it to 0
var myParent:*; //creates a new variable called myParent with undefined type*
var attackTimer:int = 0; //new variable 'attackTimer' of type interger starts at zero
var ground:Number; //a new variable called 'ground' that is a number
var jumping:Boolean = false; //a new variable 'jumping' that is a Boolean and false
var health:Number; //creates a new variable called health that is a number
//Functions
ground = this.y; //sets the ground level for this Enemy to equal its initial Y co-ord
//a new function 'activate' that accepts a variable with an undefined type*
function activate(passParent:*):void
{
//adds a Listener for the start of a new frame
this.addEventListener(Event.ENTER_FRAME, update);
/*//adds an EventListener to this (Enemy) which listens for a MouseDown Event(click)
this.addEventListener(Event.ENTER_FRAME, hit);*/
//assigns the value of passParent to the variable in the Enemy Class called myParent
myParent = passParent;
//health should equal a whole number that is between 4&7 using our new function
health = Math.round(myParent.randomise(3,5));
}
function shutDown():void //a new function called "shutDown"
{
if(alive == true) //if a variable called alive is true
{
alive = false; //set alive to equal false (i.e. not alive OR dead)
//remove the EventListener called update from the Enemy
this.removeEventListener(Event.ENTER_FRAME, update);
this.parent.removeChild(this); //remove this MovieClip from its parent
myParent.addToScore(1); //call the function addToScore belonging to myParent
}
}
//Enter Frame
//new funtion 'update' that accepts an Event
function update(updateEvent:Event):void
{
//trace("Update"); //prints "Update" in the Output panel
this.x += xSpeed; //adds xSpeed (-3) to the x pos of this (Enemy) //xSpeed*2 (speeds up by multiple of 2 etc.
this.y += ySpeed; //this (Enemy) Y co-ordinate should be increased by ySpeed
if(jumping==true) //if jumping is true
{
ySpeed += 1; //increase the ySpeed by 1
if(this.y >= ground) //if the Enemy's Y co-ord is greater than the ground level
{
xSpeed = -3; //the Enemy's xSpeed should be set to -3 ~(i.e. 3 left)
ySpeed = 0; //the Enemy's ySpeed should be set to zero
this.y = ground; //the enemies Y co-ord is set to the ground height
jumping = false; //the enemy is no longer considered to be jumping
if(health < 1) //if the enemy health is less than 1
{
shutDown(); //call the shutDown function of this Enemy
}
}
}
//ATTACKING THE BAR
///////////////////
//if the enemy comes in contact with the bar HitTestObject
if(myParent.levelStage.babans.babansHitTestObject.hitTestObject(this.walking.enemyHitTestObject))
{
xSpeed = 0//sets the Enemys xSpeed to zero
attackTimer++; //adds 1 to the value of the attackTime
if(attackTimer>10) //check if the value attackTimer is greater thn 10
{
attackTimer=0; //reset the attackTimer to zero
myParent.hitBabans(); //call a function of myParent called hitBabans
}
}
if(myParent.player.playerAttack.playerSword.swordHitTestObject.hitTestObject(this.walking.enemyHitTestObject))
{
xSpeed = 10; //enemy xSpeed set to 10 (i.e. 10 to the right)
ySpeed = -7; //enemy ySpeed set to -7 (i.e. 7 upwards)
jumping = true; //sets a variable called jumping to true
health--; //reduces the value stored in "health" by 1
}
}
//Delete
//Hit
function hit(hitEvent:Event):void //a new function "hit"
{
//ATTACKED BY PLAYER
////////////////////
//if the player's sword HitTestObject hits this whilst on attacking frame
if(myParent.player.playerAttack.playerSword.swordHitTestObject.hitTestObject(this))
{
xSpeed = 10; //enemy xSpeed set to 10 (i.e. 10 to the right)
ySpeed = -7; //enemy ySpeed set to -7 (i.e. 7 upwards)
jumping = true; //sets a variable called jumping to true
health--; //reduces the value stored in "health" by 1
}
}
That's all my current code for now - i'd really appreciate any assistance you guys could give me with this project. I just need a few of the gameplay mechanics in place as it's only a prototype that hopefully shows our AS3 "understanding"....
Also please let me know if I need to post anything else or expand on anything - I don't use forums a huge amount so I'm a newbie to this too.
Cheers.
