PDA

View Full Version : Actionscripting a Fighting Game (multiple questions)


thebangzats
05-19-2009, 01:59 PM
I'm currently working on a simple fighting game on Flash CS3, coded with Actionscript 2.0

I have the basic setup done, just need to make a few more features, including more playable characters, move combos, and a Mortal Kombat style fatality. What I've already done is a simple one round game, with just 4 moves, low kick, high kick, low punch, high punch, jump kick, jump punch, and guard.

---

Problem #1: Move Combos
The combos are not complicated, just pressing two keys at the same time (e.g. instead of just a low kick, pressing low kick and high kick simultaneously will activate a certain combo, instead of just low kicking or high kicking)

Here's a snippet of code:
if (!jump1 && this.punch == 0 && !dead1 && !Key.isDown(87)) {
if (Key.isDown(85)) {
this.gotoAndPlay("punch1");
}
if (Key.isDown(79)) {
this.gotoAndPlay("punch2");
}
if (Key.isDown(73)) {
this.gotoAndPlay("kick1");
}
if (Key.isDown(74)) {
this.gotoAndPlay("kick2");
}
}
if (this.punch == 0 && jump1) {
if (Key.isDown(85) or Key.isDown(79)) {
this.gotoAndPlay("jumppunch");
}
if (Key.isDown(73) or Key.isDown(74)) {
this.gotoAndPlay("jumpkick");
}
}
If a certain key is pressed, it will play an animation of the assigned action, but how do I make it so if two keys are pressed at the same time, it will play an altogether different animation?

Problem #2: A Mortal Kombat style, finishing Fatality
I thought I had this down before, I made a code where a certain key (reserved for the Fatality move) can only be pressed when the opponentís health reaches a certain level, e.g. :
if ((health2<=10) && Key.isDown(85)) {
this.gotoAndPlay("fatality");
}
So, if the opponentís (in this case, player 2)ís health is not below 10, the fatality key canít be pressed, so a fatality cannot be performed until the opponentís health is below 10

The problem is, what if both players use a fatality at the same time? It would cause chaos, so if by chance both player 1 and player 2 can do a fatality to each other (bothís health below 10), how do we prevent player 2 from activating a fatality when player 1 activated his fatality first (and vice versa)?

---

Help will be greatly appreciated, and if there isn't enough code to set an example, I can send you the actual .fla file of the prototype (not in english though), just tell me if you need it

Thanks in advance ^^

wibily
05-19-2009, 02:10 PM
with regard to the first problem, i'll suggest a few nested if loops ie

if(Key.isDown(79) || Key.isDown(85)){
if(Key.isDown(79) && Key.isDown(85)){
//combo move
}
else if (Key.isDown(79){
//79 move
}
else if (Key.isDown(85)){
//85 move
}
}


problem 2:
the chance that they BOTH do a fataility at the same time is very very rare but hey, if i were you i would just do something like this:

assuming we have player1 with health1 and player2 with health2

var:Boolean gameEnd = false;
.
.
.
.
if (health1 < 10 && Key.IsDown(fatality_key)){
this.gotoAndPlay("fatality");
gameEnd = true;
//probably more game ending code here
}
if (gameEnd == false && (health2 < 10 && Key.IsDown(fatlity_key2))){
this.gotoAndPlay("fatality");
//end game code here
}


so if they BOTH do a fatality at the same time...palyer1 wins and the game ends so the second player's fatality isn't executed.

runawayprisoner
05-19-2009, 08:37 PM
Problem #1: Move Combos

if (!jump1 && this.punch == 0 && !dead1 && !Key.isDown(87)) {
if (Key.isDown(85)) {
this.gotoAndPlay("punch1");
}
if (Key.isDown(79)) {
this.gotoAndPlay("punch2");
}
if (Key.isDown(73)) {
this.gotoAndPlay("kick1");
}
if (Key.isDown(74)) {
this.gotoAndPlay("kick2");
}
}
if (this.punch == 0 && jump1) {
if (Key.isDown(85) or Key.isDown(79)) {
this.gotoAndPlay("jumppunch");
}
if (Key.isDown(73) or Key.isDown(74)) {
this.gotoAndPlay("jumpkick");
}
}
If a certain key is pressed, it will play an animation of the assigned action, but how do I make it so if two keys are pressed at the same time, it will play an altogether different animation?

Use a boolean value for each key presses:



if(key.isDown(73)){
lkick = true;
}
if(key.isDown(74)){
hkick = true;
}



Then check them at run-time:



if(lkick && hkick){
// Do whatever
}
else if(lkick) {
// Do whatever
}

//...



Basically like the above but use boolean values instead to avoid overheads.

Also check for the combo keys before the single keys.


Problem #2: A Mortal Kombat style, finishing Fatality


Mmm...


if(!fatality && ((health1 < 10 && key.isDown(fatal)) || (health2 < 10 && key.isDown(fatal))){
// play animation
fatality = true;
}


That should do.