Hi there, if the contents of this post brings your palm to your face try and keep in mind I’m quite new to this.
I’m having a lot of trouble with a game I’m making and some advice would be greatly appreciated. The following class is tied to my game’s health bar:
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class HealthBar extends MovieClip
{
public var maxHealth:Number;
public var currentHealth:Number;
public var decreaseAmount:Number;
private var decreaseBar:Number;
private var barWidth:Number;
private var healthTimer:Timer;
public function HealthBar() {
barWidth = this.width;
}
public function startTimer(delay:Number, health:Number, decrease:Number):void {
currentHealth = maxHealth = health;
decreaseAmount = decrease;
decreaseBar = (this.width * decreaseAmount) / maxHealth;
healthTimer = new Timer(delay)
healthTimer.addEventListener(TimerEvent.TIMER, decreaseHealth);
healthTimer.start();
}
public function addHealth(amount:Number):void {
this.width += (barWidth * amount) / maxHealth;
currentHealth += amount;
}
public function resetBar() {
this.width = barWidth;
currentHealth = maxHealth;
}
public function removeTimer() {
healthTimer.stop();
healthTimer.removeEventListener(TimerEvent.TIMER, decreaseHealth);
}
private function decreaseHealth(e:TimerEvent):void {
this.width -= decreaseBar;
currentHealth -= decreaseAmount;
if (currentHealth <= 0) {
this.width = 0;
healthTimer.stop();
trace("way to go *******");
stop();
}
}
}
}
and I’m having trouble calling the functions within this class in a different class, namely my enemy class.
package {
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
public class enemy extends Sprite {
private var core:Object;
private var yd:Number;
private var xd:Number;
public function enemy() {
addEventListener(Event.ADDED_TO_STAGE,onadd);
}
private function onadd(e:Event) {
core=MovieClip(root);
yd = core.sp.y-y;
xd = core.sp.x-x;
addEventListener(Event.ENTER_FRAME,loop);
}
private function loop(e:Event) {
var angle:Number=Math.atan2(yd,xd);
x+=Math.cos(angle)*12;
y+=Math.sin(angle)*12;
for (var i:int = 0; i<core.bulletholder.numChildren; i++) {
var bulletTarget:Sprite=core.bulletholder.getChildAt(i);
var ris:Number=bulletTarget.y-y;
var run:Number=bulletTarget.x-x;
var dis:Number=Math.sqrt(Math.pow(ris,2)+Math.pow(run,2));
if (dis<100) {
if (run<0) {
x+=4.5;
} else {
x-=4.5;
}
}
if (hitTestObject(bulletTarget)) {
core.bulletholder.getChildAt(i).removeListeners();
core.bulletholder.removeChild(bulletTarget);
var boom:MovieClip = new explosion();
boom.x=x;
boom.y=y;
stage.addChild(boom);
removeEventListener(Event.ENTER_FRAME, loop);
core.removeChild(this);
}
}
}
public function removeListeners():void {
removeEventListener(Event.ENTER_FRAME, loop);
}
}
}
}
What I’m trying to do is set up a hit test so that when a collision is detected between the enemy and the player it’ll call the decreaseHealth function in my health bar class. Having followed countless tutorials and trying dozens of iterations of hitTestObject I’m out of ideas of how to do this. Any suggestions?