www.webdeveloper.com
Results 1 to 4 of 4

Thread: Javascript problem

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Javascript problem

    Hello, Im trying to make a game for a college project and I just cant seem to figure out what is wrong? Im trying to have it change the picture called WeaponYou to another depending on what random number from 1 - 8
    Thanks for any help!
    Code:
    <html>
    
    <head>
    
    <script>
    
    var rand = (Math.random));
    var myInt = Math.round(rand*8);
    var WeaponYou;
    
    
    
    
    function SpinButtonEvent1()
    {
    
    if ( myInt == 1 )
    
       {
         WeaponYou = 1;
         
         document.images["WeaponYou"].src= "Sheild.jpg";
       
       }
    
    else if ( myInt == 2 )
    
       {
        WeaponYou = 2;
        document.images["WeaponYou"].src= "BowAndArrow.jpg";
       }
    
    else if ( myInt == 3 )
    
       {
        WeaponYou = 3
        document.images["WeaponYou"].src= "Dagger.jpg";
       }
    
    else if ( myInt == 4 )
    
       {
       WeaponYou = 4;
       document.images["WeaponYou"].src= "IronSword.jpg";
       }
    
    else if ( myInt == 5 )
    
       {
        WeaponYou = 5;
        document.images["WeaponYou"].src= "Mace.jpg";
       
       }
    
    else if ( myInt == 6 )
       {
       
        WeaponYou = 6;
        document.images["WeaponYou"].src= "Spear.jpg";
       }
    
    else if ( myInt == 7 )
       {
    
        WeaponYou = 7;
        document.images["WeaponYou"].src= "Trident.jpg";
       }
    
    else if ( myInt == 8 )
    
       {
       WeapnYou = 8;
      document.images["WeaponYou"].src= "WoodenSword.jpg";
    
       }
    
    }
    
    
    </script>
    
    
    
    
    </head>
    
    
    <body>
    
    
    
    
    
    <DIV ALIGN=CENTER>
    <img src="ArenaBanner.jpg"/> <br>
    <img src="YouBattle.jpg"/> <br>
    <img src="YouEnemy.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="SlotBlackBox.jpg"/> <img name="WeaponYou" src="SlotPlaceHolder.jpg"/> <img src="SlotBlackBox.jpg"/> <img src="SlotPlaceHolder.jpg"/><img src="SlotBlackBox.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="PlayerFaceWin.jpg"/>
    
    <a href="#" onMouseDown="SpinButtonEvent1()"><img name="SpinButton" src="SpinButton.jpg"></a>
    
    
    <img src="EnemyFaceWin.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="VictoryPoints.jpg"/> <br>
    
    </div>
    
    
    </body>
    </html>

  2. #2
    Join Date
    Apr 2012
    Posts
    1

    Syntax error with Math.random method

    Hi,
    Your code is having a problem with the call to Math.random. As you did not use brackets (), you are copying the reference to function Math.random into the variable 'rand'. Just add () to this statement.

    The code can also be simplified by copying all the image names into an array from position 1 to 8, so that, it can be directly assigned to the 'img' element based on the random number resulted.

    <html>
    <head>
    <script>
    var rand = Math.random();
    var myInt = Math.round(rand*8);
    var WeaponYou;
    var myImages = ['','Sheild.jpg', 'BowAndArrow.jpg', 'Dagger.jpg', 'IronSword.jpg', 'Mace.jpg', 'Spear.jpg', 'Trident.jpg', 'WoodenSword.jpg'];
    console.log(myInt);
    function SpinButtonEvent1(){
    document.images["WeaponYou"].src= myImages[myInt];
    }
    </script>
    </head>
    <body>
    <div align=center>
    <img src="ArenaBanner.jpg"/> <br>
    <img src="YouBattle.jpg"/> <br>
    <img src="YouEnemy.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="SlotBlackBox.jpg"/>
    <img name="WeaponYou" src="SlotPlaceHolder.jpg"/>
    <img src="SlotBlackBox.jpg"/>
    <img src="SlotPlaceHolder.jpg"/>
    <img src="SlotBlackBox.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="PlayerFaceWin.jpg"/>

    <a href="#" onMouseDown="SpinButtonEvent1()"><img name="SpinButton" src="SpinButton.jpg"></a>

    <img src="EnemyFaceWin.jpg"/> <br>
    <img src="BlackSpace.jpg"/> <br>
    <img src="VictoryPoints.jpg"/> <br>
    </div>
    </body>
    </html>

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    var rand = (Math.random));

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function randomNumber(m,n){return Math.floor(Math.random()*(n-m+1))+m;}
    var WeaponYou = 0;
    
    function SpinButtonEvent1(){
    var myInt=randomNumber(1,8),
    wy=document.getElementById('WeaponYou');
    switch(myInt){
    case 1: WeaponYou = 1; wy.src="Sheild.jpg"; break;
    case 2: WeaponYou = 2; wy.src="BowAndArrow.jpg"; break;
    case 3: WeaponYou = 3; wy.src="Dagger.jpg"; break;
    case 4: WeaponYou = 4; wy.src="IronSword.jpg"; break;
    case 5: WeaponYou = 5; wy.src="Mace.jpg"; break;
    case 6: WeaponYou = 6; wy.src="Spear.jpg"; break;
    case 7: WeaponYou = 7; wy.src="Trident.jpg"; break;
    case 8: WeaponYou = 8; wy.src="WoodenSword.jpg"; break;
    }
    }
    </script>
    </head>
    <body>
    <div align="center">
    <img src="ArenaBanner.jpg" alt="ArenaBanner" /> <br />
    <img src="YouBattle.jpg" alt="YouBattle" /> <br />
    <img src="YouEnemy.jpg" alt="YouEnemy" /> <br />
    <img src="BlackSpace.jpg" alt="BlackSpace" /> <br />
    <img src="SlotBlackBox.jpg" alt="SlotBlackBox" /> <img id="WeaponYou" src="SlotPlaceHolder.jpg" alt="SlotPlaceHolder" /> <img src="SlotBlackBox.jpg" alt="SlotBlackBox" /> <img src="SlotPlaceHolder.jpg" alt="SlotPlaceHolder" /><img src="SlotBlackBox.jpg" alt="SlotBlackBox" /> <br />
    <img src="BlackSpace.jpg" alt="BlackSpace" /> <br />
    <img src="PlayerFaceWin.jpg" alt="PlayerFaceWin" />
    <a href="#null" onMouseDown="SpinButtonEvent1()"><img name="SpinButton" src="SpinButton.jpg" alt="SpinButton" /></a>
    <img src="EnemyFaceWin.jpg" alt="EnemyFaceWin" /> <br />
    <img src="BlackSpace.jpg" alt="BlackSpace" /> <br />
    <img src="VictoryPoints.jpg" alt="VictoryPoints" /> <br />
    </div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles