こんにちは。
じゃんけんゲームのスクリプトをつけているのですが、重いからなのか、動作が上手くいったりいかなかったりします。コンソールにはエラーが出ていないので、書き方が悪いのでしょうが、キャッシュを溜める、チェーンを使うなどの対策をできる限りやってみても改善しません。
ここをこうしたほうが良いという意見をください。よろしくお願いいたします。
HTML
1 <div id="screen_box"> 2 <div id="enemy_life"></div> 3 <div id="our_life"></div> 4 <div id="screen"><img src="" id="screen_img"></div> 5 </div> 6 7 <div id="text_box">ボタンを押してください</div> 8 9 <div id="game_sta">ゲームスタート</div> 10 11 <div id="your_hands"> 12 <div id="rock"><img src="img/%E3%81%90%E3%83%BC.png" class="hand_imgs"></div> 13 <div id="scissors"><img src="img/%E3%81%A1%E3%82%87%E3%81%8D.png" class="hand_imgs"></div> 14 <div id="paper"><img src="img/%E3%81%B1%E3%83%BC.png" class="hand_imgs"></div> 15 </div> 16
JavaScript
1 2<script> 3 $(function(){ 4 //キャッシュ 5 var screen = $('#screen'); 6 var screen_img = $('#screen_img'); 7 var text_box = $('#text_box') 8 var rock = $('#rock'); 9 var scissors = $('#scissors'); 10 var paper = $('#paper'); 11 12 13 //敵のArray 14 var enemy = [ 15 'img/%E3%81%90%E3%83%BC.png', 16 'img/%E3%81%A1%E3%82%87%E3%81%8D.png', 17 'img/%E3%81%B1%E3%83%BC.png' 18 ]; 19 var enemy_name = ['グー','チョキ','パー']; 20 21 22 //敵を決定する関数 23 function show_up(){ 24 var num = Math.floor(Math.random()*3); 25 26 screen_img.fadeIn(50); 27 screen_img.attr('src',enemy[num]); 28 text_box.text(enemy_name[num] + 'が現れた!'); 29 30 return num; 31 }; 32 33 //スタートを押したとき 34 $('#game_sta').click(function(){ 35 $('#your_hands').show(); 36 $(this).hide(); 37 38 game_proc(); 39 }); 40 41 //相手との戦闘の判定をする関数 42 function game_proc(){ 43 on_command(); 44 show_up(); 45 46 if(show_up() === 0){ 47 rock.on('click',function(){ 48 counter(); 49 }); 50 scissors.on('click',function(){ 51 lose(); 52 }); 53 paper.on('click',function(){ 54 beat(); 55 }); 56 }else if(show_up() === 1){ 57 rock.on('click',function(){ 58 beat(); 59 }); 60 scissors.on('click',function(){ 61 counter(); 62 }); 63 paper.on('click',function(){ 64 lose(); 65 }); 66 }else if(show_up() === 2){ 67 rock.on('click',function(){ 68 lose(); 69 }); 70 scissors.on('click',function(){ 71 beat(); 72 }); 73 paper.on('click',function(){ 74 counter(); 75 }); 76 } 77 }; 78 79 //じゃんけんコマンドをオフ 80 function off_command(){ 81 rock.off('click'); 82 scissors.off('click'); 83 paper.off('click'); 84 }; 85 86 //じゃんけんコマンドをオン 87 function on_command(){ 88 rock.on('click'); 89 scissors.on('click'); 90 paper.on('click'); 91 }; 92 93 //相手を倒した時の関数 94 function beat(){ 95 screen_img.fadeOut(1000); 96 text_box.text('効果は抜群だ!'); 97 off_command(); 98 screen.click(function(){ 99 game_proc(); 100 }); 101 }; 102 103 //相手と引き分けた時の関数 104 function counter(){ 105 screen_img.fadeOut(20,function(){$(this).fadeIn(20,function(){$(this).fadeOut(20,function(){$(this).fadeIn(20)})})}); 106 text_box.text('相打ちだ!'); 107 off_command(); 108 screen.click(function(){ 109 game_proc(); 110 }); 111 }; 112 113 //相手に負けた時の関数 114 function lose(){ 115 screen_img.animate({height:"120px"}, 1000).animate({height:"300px"}, 1000); 116 text_box.text('大ダメージを受けた!'); 117 off_command(); 118 screen.click(function(){ 119 game_proc(); 120 }); 121 }; 122 123 }); 124 </script>
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/17 16:46
2015/11/18 00:22