Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.
    Last edited by Geodesic_D; 04-08-2007 at 04:36 AM. Reason: I forgot to mention the bit at the bottom.
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #2
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How do I use Arrays?
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •