PDA

View Full Version : Attacking in Actionscript


Darunia9
02-25-2008, 06:28 PM
How would I make the main character of my game, a sort of Zelda like game attack and hurt enemies?

I have this code but it doesn't take off any HP off my enemy, can anyone help?

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");
}
}
}

kibilocomalifasa
03-08-2008, 04:10 PM
try: HP -= (random script)

filipeabreu
03-08-2008, 05:11 PM
Change your script a little to this:

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");
}
}
}
}

That's because on the code you posted the damage var will only be active inside the onClipEvent (load) function. When I put the onClipEvent (enterFrame) function inside the onClipEvent (load) function, the damage variable can be accessed from the onClipEvent (enterFrame) function too. The best way would be to not use the script on the movieclips, but to use frame actions and declare the variable outside any event handler function.

PS: For anyone else reading this... can we declare a variable with movieclip scripts?