PDA

View Full Version : Attacking in Actionscript



Darunia9
February 26th, 2008, 07:38 AM
I'm trying to make a charcter attack an enemy and either destroy or damage it. So for example when the hero attacks it goes to a clip of it disappearing or after it's lost so much hp it does, but I havent a clue how to do this

Can anyone help?

DangerousDan
February 26th, 2008, 07:45 AM
if(Key.isDown(Key.SPACE)) //press space bar to attack
{
attacking = true; //set some variable mentioning your attacking, not really needed for this
hero.gotoAndPlay("Attack"); //play attack animation
if(hero.hitTest(enemy1)) //check to see if the moveclip hero's blue bounding box is touching enemy1's blue bounding box.
{
enemy1.HP--; //damage enemy1, it could be rewrote at enemy1.HP = enemy1.HP - 1;
if(enemy1.HP <= 0) //if he has no more HP...
{
enemy1.die(); //he dies.
enemy1.gotoAndPlay("Die"); //play death animation
}
}
else
enemy1.gotoAndPlay("Hurt"); //play hurt animation
}


If you're having trouble with this I suggest you go find some tutorials, as this code won't work if you copy and paste it but it gives the general idea.

Darunia9
February 26th, 2008, 07:57 AM
Thanks for the help I think I can work it out now

sorry for the posts under this my friends are idiots


I have this code which I think should work, well it half does.


onClipEvent (load) {
damage = random(9)+1;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE)) {
if (this.hitTest(enemy)) {
_root.enemyHp -= damage;
if (_root.enemyHp<=0) {
_root.enemy.gotoAndPlay("die");
}
} else {
_root.enemy.gotoAndPlay("hurt");
}
}
}

except it doesn't seem to damage the enemy. I have a variable called enemyHp which is at 100, and it doesn't go down, even though it still goes to the hurt animation.

Darunia9
February 26th, 2008, 07:59 AM
sorry ¬_¬

Darunia9
February 26th, 2008, 08:00 AM
and again

sorry

DangerousDan
February 28th, 2008, 10:45 AM
I'm guessing you put this code on the hero movieclip, putting code on movieclips is generally bad practice (so I hear), it is often better to have it all organized on the frame.

That being said, add a trace after the part where they get hit to check the values of damage and enemyHp.

Also in terms of longevity, having a global variable for the HP of the enemy wouldn't work(Unless you plan on having one enemy at a time). You're going to have to have each enemy keep track of its own HP (through a enemy Class or something, this might be a bit over your head.) There's some great tutorials on Object Orientated Programming here on Kirupa to start you off.