Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2007, 04:35 AM   PM User | #1
Geodesic_D
Regular Coder

 
Join Date: Nov 2004
Location: Perth, UK
Posts: 105
Thanks: 1
Thanked 0 Times in 0 Posts
Geodesic_D is an unknown quantity at this point
Help with Fighting Game

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.

Code:
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.
__________________
Geodesic_D
"I'd much rather be called GeodesicDragon."

Last edited by Geodesic_D; 04-08-2007 at 04:36 AM.. Reason: I forgot to mention the bit at the bottom.
Geodesic_D is offline   Reply With Quote
Old 04-09-2007, 11:10 AM   PM User | #2
justinbird
New to the CF scene

 
Join Date: Apr 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
justinbird is an unknown quantity at this point
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:

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

to avoid getting the same random number twice:

PHP Code:
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.
justinbird is offline   Reply With Quote
Old 04-09-2007, 02:17 PM   PM User | #3
Geodesic_D
Regular Coder

 
Join Date: Nov 2004
Location: Perth, UK
Posts: 105
Thanks: 1
Thanked 0 Times in 0 Posts
Geodesic_D is an unknown quantity at this point
How do I use Arrays?
__________________
Geodesic_D
"I'd much rather be called GeodesicDragon."
Geodesic_D is offline   Reply With Quote
Old 04-09-2007, 06:33 PM   PM User | #4
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
See if this'll help you http://www.webwasp.co.uk/tutorials/b28-array/index.php
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:18 PM.


Advertisement
Log in to turn off these ads.