Salam…
After much altering, repairing & debugging, the algorithm turns out much of what I’m expecting. Although, few more algorithm seems not working out since 6 hours ago (have to break fast first & guling2 = 3 hours total). After last post, the health bar is working but not in a proper way — it keep reducing although the enemies hasn’t hit the player yet. Anything else is working perfectly like enemy randomly spawn & targeting the player. However, I did not managed to make the keyboard inputs function running. The function is in the Player class & supposed to be kicking the functions when testing. If anyone out there stumbled across this post, please do help (This is my first game & have not learn ActionScript before).
This is the Player class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Mouse;
import flash.ui.Keyboard;
public class Player extends MovieClip {
private var stageRef:Stage;
private var rightKeyisDown:Boolean = false;
private var leftKeyisDown:Boolean = false;
private var upKeyisDown:Boolean = false;
private var downKeyisDown:Boolean = false;
private var playerSpeed:Number = 7;
private var vx:Number = 0;
private var vy:Number = 0;
private var totalHealth:Number=1;
public function Player(stageRef:Stage):void {
// constructor code
this.stageRef = stageRef;
addEventListener(Event.ENTER_FRAME, HitTest);
addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
addEventListener(Event.ENTER_FRAME, moveThePlayer);
}
public function HitTest(event:Event):void
{
if (hitTestPoint(this.x, this.y, true))
{
//if the value of totalHealth more than 0
if (totalHealth>0)
{
//reduce the totalHealth by 0.05
totalHealth-=0.05;
//trace("Ouch!");
}
//if the value of totalHealth is less than or equal 0
if (totalHealth<=0)
{
//make the totalHealth vaule become 0
totalHealth=0;
//trace("You're Dead");
}
dispatchEvent(new Event("hit"));
}
//make the x scale of healthBar movie clip same as totalHealth value
MovieClip(root).healthBar.scaleX = totalHealth;
}
private function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT){
rightKeyisDown = true;
}
if(event.keyCode == Keyboard.LEFT){
leftKeyisDown = true;
}
if(event.keyCode == Keyboard.UP){
upKeyisDown = true;
}
if(event.keyCode == Keyboard.DOWN){
downKeyisDown = true;
}
}
private function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT){
rightKeyisDown = false;
}
if(event.keyCode == Keyboard.LEFT){
leftKeyisDown = false;
}
if(event.keyCode == Keyboard.UP){
upKeyisDown = false;
}
if(event.keyCode == Keyboard.DOWN){
downKeyisDown = false;
}
}
private function moveThePlayer(event:Event):void
{
if(rightKeyisDown)
{
x += playerSpeed;
scaleX = 1;
}
if(leftKeyisDown)
{
x -= playerSpeed;
scaleX = -1;
}
if(upKeyisDown)
{
y -= playerSpeed;
scaleY = 1;
}
if(downKeyisDown)
{
y += playerSpeed;
scaleY = -1;
}
}// End of player movement function
}
}
Next, I’ll be adding the mouse click function, mouse rotation function & Bullet class. What’s the point of playing the game if you can’t do much. The idea is to shoot & survive the waves. In future enhancement, I was thinking of having a shop & adding stat points to the player’s attribute…but only after I’ve done the main goal of the game. Sekian…