...

View Full Version : Help with Fighting Game



Geodesic_D
04-08-2007, 05:35 AM
I'm trying to create a two-player fighting game in Flash, but I've ran into some problems.

When the game starts, I want to randomly pick the names of two fighters and then show their picture under their name.

The name is going to show up in a dynamic textbox called 'player1' and the picture is controlled by a movieclip called 'p1charpic'. The same applies to Player Two, except its 'player2' and 'p2charpic'.

This is the code I have just now (which doesn't work). I am using the same code for both characters, so it appears twice.



var p1character=Math.round(Math.random()*4);
var p2character=Math.round(Math.random()*4);

if (p1character="0")
{
player1.value="Fred"
p1charpic.gotoAndStop(2)
}
else if (p1character="1")
{
player1.value="John"
p1charpic.gotoAndStop(3)
}
else if (p1character="2")
{
player1.value="Joanne"
p1charpic.gotoAndStop(4)
}
else if (p1character="3")
{
player1.value="Bill"
p1charpic.gotoAndStop(5)
}
else if (p1character="4")
{
player1.value="Ethel"
p1charpic.gotoAndStop(6)
}


Can someone help me please?

Also, I want to prevent the game choosing the same character for both players.

justinbird
04-09-2007, 12:10 PM
First problem:
You'll need to use '==', not '=' for comparisons. When you use only one equal sign, you're actually setting the variable p1character to 0 and returning true.

The second problem:
0 doesn't need to be wrapped in quotes. You use quotes when your're dealing with strings, but not when you're dealing with numbers:

so, it should look like this:


if (p1character == 0)
{
player1.value="Fred";
p1charpic.gotoAndStop(2);
}


to avoid getting the same random number twice:


var p1character=Math.round(Math.random()*4);
do { var p2character=Math.round(Math.random()*4); }
while ( p2character == p1character; )

this will keep trying new random values for the second character until it doesn't match the 1st.

And a piece of advice - you should really look into using Arrays. Your code could be brought down to just a few lines that way, much less repetitive code.

Geodesic_D
04-09-2007, 03:17 PM
How do I use Arrays?

Nightfire
04-09-2007, 07:33 PM
See if this'll help you http://www.webwasp.co.uk/tutorials/b28-array/index.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum