Hi everyone
I’m trying to create my first game in actionscript, and I’m facing a few problems
The player and the enemies are not being bound on the stage, I’m not sure how to fix that.
Also when the player loses and its goes to the last endgame frame, I get a null error even tho i removed all the event listeners.
The enemy is being called via actionscript, its not placed on the stage.
Any help appreciated
var speed:int = 5;
var startTime:int = 0;
var timeLimit:int = 35;
var dropSpeed:int = 16;
var dragonContainer:MovieClip;
var score:Number;
var loseScore:Number = 10;
dragonContainer = new MovieClip();
addChild(dragonContainer);
//setChildIndex(appleContainer, 0);
score = 0;
player_mc.addEventListener(Event.ENTER_FRAME, movePlayer);
player_mc.mouseEnabled = false;
Mouse.hide();
function movePlayer(e:Event)
{
player_mc.x = mouseX;
player_mc.y = mouseY;
createDragons();
checkForCatches();
}
if(player_mc.y>=400)
{player_mc.y=390;}
if(player_mc.y<=15)
{player_mc.y=15;}
if(player_mc.x>=530)
{player_mc.x=530;}
if(player_mc.x<=10)
{player_mc.x=10;}
function createDragons(){
if(startTime < timeLimit){
startTime++;
} else {
var newDragon:Dragon = new Dragon();
newDragon.y = newDragon.height * -1;
//making thedragon's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newDragon.y = int(Math.random()* stage.stageWidth - newDragon.width);
//make sure the dragons are not going off the left edge of the stage
if(newDragon.x < newDragon.width){
newDragon.x = newDragon.width;
}
newDragon.addEventListener(Event.ENTER_FRAME, dropDragons);
//then add the dragons to Stage
dragonContainer.addChild(newDragon);
//and reset the startTime to create a delay in creating the next dragons
startTime = 5;
}
}
function dropDragons(e:Event){
//check to see if the dragon goes off stage / if so, remove it
if(e.target.y > stage.stageWidth){
e.target.removeEventListener(Event.ENTER_FRAME, dropDragons);
removeChild(MovieClip(e.target));
}
// move the dragon left
e.target.x += dropSpeed;
e.target.dropSpeed += 0.5;
}
function checkForCatches(){
for(var i:int = 0; i < dragonContainer.numChildren; i++){
var dragonTarget = dragonContainer.getChildAt(i);
if(player_mc.hitTestObject(dragonTarget)){
dragonTarget.removeEventListener(Event.ENTER_FRAME, dropDragons);
dragonContainer.removeChild(dragonTarget);
player_mc.alpha -= 0.1;
// increment the user’s score
score++;
// check to see if the user won
checkScore();
}
}
}
function checkScore(){
score_txt.text = String(score);
if(score >= loseScore){
//title_mc.visible = true;
//title_mc.gotoAndStop(“gameover”);
endGame();
}
}
function endGame()
{
gotoAndPlay(“gameover”);
removeChild(dragonContainer);
removeEventListener(Event.ENTER_FRAME, dropDragons);
removeEventListener(Event.ENTER_FRAME, movePlayer);
Mouse.show();
}
stop();