PDA

View Full Version : [AS3] Life of heroe in a game problem


laxmana
06-11-2007, 01:29 PM
Hi i am totaly new to actionscript and actionscript 3.0

I had this code


var lf=3;
if(dragonflyMen.hitTestObject(bug))
{
lf--;
this.life.text=lf;
}

The dragonflyMen symbol is attach with the mouse movements.
The life is a dynamic text.
The problem is that when i hit the symbol call bug the lifes decreased very quickly and not one per hit but many in one hit.
Is there anyother way to do this;;

hangalot
06-11-2007, 02:14 PM
hav a flag that indicates when you have a hit, then keep that value until the hittest is false again and when it hits again decrease it.

laxmana
06-11-2007, 06:22 PM
can you make it more clear;here is the code

this.addEventListener(MouseEvent.MOUSE_MOVE,dragon flyMenMove);
function dragonflyMenMove(event:MouseEvent):void {
var endX = this.mouseX;
var endY = this.mouseY;
this.dragonflyMen.x += (endX-this.dragonflyMen.x)/speed;
this.dragonflyMen.y += (endY-this.dragonflyMen.y)/speed;
this.dragonflyWoman.x=this.dragonflyMen.x/2;
this.dragonflyWoman.y=(this.dragonflyMen.y+30);
if (dragonflyMen.hitTestObject(bug)) {
lifes--;
this.life.text=lifes;
}
if (this.dragonflyMen.hitTestObject(dragonflyWoman)) {
totalpoints++;
this.score.text=totalpoints;
}
}

Because the if clause is in the mouse listener the decrease is so quickly.If i put a flag inside the listener i think it will be the same;;;
Thank you for the replay

jsimpson
06-11-2007, 06:41 PM
You could create a variable that would only allow life to be taken away after a set interval using an additional function.


// Change to your code added allowHit:Boolean
if (dragonflyMen.hitTestObject(bug) && allowHit) {
lifes--;
this.life.text=lifes;
allowHit = false;
var myTimer = new Timer(50, 1);
myTimer.addEventListener(TimerEvent.COMPLETE, updateAllowHit);
myTimer.start();
}

// outside dragonflyMenMove...
function updateAllowHit(e:TimerEvent){
allowHit = true;
}


That change would make your player take a health decrease every 50milliseconds. You could play with the time to find a reasonable amount depending on how your game works.

Edit: Another case of not carefully reading a post


if (dragonflyMen.hitTestObject(bug) && allowHit) {
lifes--;
this.life.text=lifes;
allowHit = false;
}else{
allowHit = true;
}

laxmana
06-11-2007, 07:23 PM
Thank a lot.You save me a lot of time of searching. thanks again.