MavericGamer
03-25-2009, 06:37 AM
Okay, so here's a tricky one. I've been writing this same game for a while, and I have the engine working, and it's a solid game. But, my enemies only come towards my side of the screen (from right to left), and have no vertical movement. I've been trying to implement movement. Putting in the code to make them go either up or down was no problem, but the code for changing their movement is the real issue here.
package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.utils.Timer;
public class Enemy extends MovieClip {
private var dx:Number; // speed and direction
private var dy:Number;
private var lastTime:int; // animation time
private var vertChange:Timer;
public function Enemy(side:String, speed:Number, altitude:Number)
{
this.x = 850; // start to the right
dx = -speed*1.5; // fly right to left
dy = speed*.75;
this.scaleX = 1;
this.y = altitude; // vertical position
this.gotoAndStop(1);
// set up animation
addEventListener(Event.ENTER_FRAME,moveEnemy);
lastTime = getTimer();
}
/*This is the class it is saying is undefined. I have tried to make it in so many different ways:
public function, private function, social function (that one didn't work too well), function... and
I've even tried everything in the parens that made sense: e:Event, event:TimerEvent, and for
both of those I have tried adding :void to the end of both. I'm not very advanced, so I'm not sure what those are doing, but the error remains.*/
public function moveVerts()
{
dy *= -1;
moveEnemy();
}
vertChange = new Timer(200+Math.random()*400,0);
vertChange.addEventListener(Timer.TIMER_COMPLETE,m oveVerts);
vertChange.start();
public function moveEnemy(event:Event)
{
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move enemy
this.x += dx*timePassed/1000;
this.y += dy*timePassed/1000;
// check to see if off screen
if (x < -50) {
deleteEnemy();
}
if ((this.y+this.height<<0)||(this.y-this.height>>600)) changeVerts();
}
// enemy hit, show explosion
public function enemyHit() {
removeEventListener(Event.ENTER_FRAME,moveEnemy);
MovieClip(parent).removeEnemy(this);
gotoAndPlay("explode");
}
// delete enemy from stage and enemy list
public function deleteEnemy() {
removeEventListener(Event.ENTER_FRAME,moveEnemy);
MovieClip(parent).removeEnemy(this);
parent.removeChild(this);
}
}
}
The error it is throwing is of type #1065, and it's saying that the variable is undefined... but unless I'm mistaken, I'm trying to call a constant/function, not a variable. It is 2:30 AM here, and I've been coding for several hours, so I'm calling for help.
package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.utils.Timer;
public class Enemy extends MovieClip {
private var dx:Number; // speed and direction
private var dy:Number;
private var lastTime:int; // animation time
private var vertChange:Timer;
public function Enemy(side:String, speed:Number, altitude:Number)
{
this.x = 850; // start to the right
dx = -speed*1.5; // fly right to left
dy = speed*.75;
this.scaleX = 1;
this.y = altitude; // vertical position
this.gotoAndStop(1);
// set up animation
addEventListener(Event.ENTER_FRAME,moveEnemy);
lastTime = getTimer();
}
/*This is the class it is saying is undefined. I have tried to make it in so many different ways:
public function, private function, social function (that one didn't work too well), function... and
I've even tried everything in the parens that made sense: e:Event, event:TimerEvent, and for
both of those I have tried adding :void to the end of both. I'm not very advanced, so I'm not sure what those are doing, but the error remains.*/
public function moveVerts()
{
dy *= -1;
moveEnemy();
}
vertChange = new Timer(200+Math.random()*400,0);
vertChange.addEventListener(Timer.TIMER_COMPLETE,m oveVerts);
vertChange.start();
public function moveEnemy(event:Event)
{
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move enemy
this.x += dx*timePassed/1000;
this.y += dy*timePassed/1000;
// check to see if off screen
if (x < -50) {
deleteEnemy();
}
if ((this.y+this.height<<0)||(this.y-this.height>>600)) changeVerts();
}
// enemy hit, show explosion
public function enemyHit() {
removeEventListener(Event.ENTER_FRAME,moveEnemy);
MovieClip(parent).removeEnemy(this);
gotoAndPlay("explode");
}
// delete enemy from stage and enemy list
public function deleteEnemy() {
removeEventListener(Event.ENTER_FRAME,moveEnemy);
MovieClip(parent).removeEnemy(this);
parent.removeChild(this);
}
}
}
The error it is throwing is of type #1065, and it's saying that the variable is undefined... but unless I'm mistaken, I'm trying to call a constant/function, not a variable. It is 2:30 AM here, and I've been coding for several hours, so I'm calling for help.