こんにちは。
フラッシュ暗算の要領で、数字の羅列ではなく既に設定したメッセージをonClickで表示する仕組みを作りたいのですが、下記のフラッシュ暗算用のコードからどのように書き換えていけばよいか悩んでいます。例えば、Math.floor(Math.random()*)の部分などはメッセージ表示であれば関係ないので必要ない、という程度までは辛うじて理解しているのですが・・・。何かご指摘や解決のヒントなどございましたら、よろしくお願いします。
HTML
1<div id="no"> 2? 3</div> 4<p> 5 <input type="button" value="Start" onClick="Start()"> 6 <input type="text" value="" id="kotae"> 7 <input type="button" value="Check" onClick="Check()"> 8</p>
javascript
1<script type="text/javascript"> 2 3var intRandom = function ( min, max, len ) { 4 var a = new Array( len ); 5 var r = [ ]; 6 var s = max - min + 1; 7 8 while( len-- ) r.push( Math.floor(Math.random() * s + min ) ); 9 return r; 10} 11 12 13var Flash = function ( ) { 14 this.init.apply( this, arguments ); 15}; 16 17 18Flash.prototype.init = (function ( ) { 19 return function ( id, vtime, htime, ary ) { 20 this.target = document.getElementById( id ); 21 this.vtime = 'number' == typeof vtime ? vtime: 700; 22 this.htime = 'number' == typeof htime ? htime: 100; 23 return this.ary = ary; 24 }; 25})(); 26 27 28Flash.prototype.stop = function ( ) { 29 clearTimeout( this.tmid ); 30 return this.setString( 'Stop!!' ); 31}; 32 33 34Flash.prototype.start = (function ( ) { 35 return function ( ) { 36 this.setString( 'Go!' ); 37 this.max = this.ary.length; 38 this.mode = false; 39 this.tmid = null; 40 this.cnt = 0; 41 setTimeout( (function(that){ return function(){ that.loop() }})(this), 500); 42 }; 43})(); 44 45 46Flash.prototype.setString = (function ( ) { 47 return function ( str ) { 48 return this.target.firstChild.nodeValue = 'undefined' == typeof str ? '': str + ''; 49 }; 50})(); 51 52 53Flash.prototype.loop = (function ( ) { 54 return function ( ) { 55 var t = '?'; 56 57 if( this.max >= this.cnt ) { 58 t = ( this.mode = ! this.mode ) ? '': this.ary[ this.cnt++ ]; 59 60 this.tmid = setTimeout( (function(that){ return function(){ that.loop() }})(this), 61 this.mode ? this.htime: this.vtime ); 62 } 63 this.setString( t ); 64 }; 65})(); 66 67 68Flash.prototype.getTotal = (function ( ) { 69 return function ( ) { 70 var t = 0, c = this.max, n; 71 while( c ) t += this.ary[ --c ]; 72 return t; 73 }; 74})(); 75 76 77Flash.prototype.setData = (function ( ) { 78 return function ( ary ) { 79 return this.ary = ary; 80 }; 81})(); 82 83 84Flash.prototype.check = (function ( ) { 85 return function ( total ) { 86 return total == this.getTotal(); 87 }; 88})(); 89 90var mondai = new Flash( 'no', 400, 150); 91 92function Start( ) { 93 document.getElementById( 'kotae' ).value = ''; 94 mondai.setData( intRandom( -10, 10, 5) ); 95 mondai.start( ); 96} 97 98function Check( ) { 99 var t = document.getElementById( 'kotae' ); 100 t.value = mondai.check( t.value ) ? "正解": mondai.getTotal(); 101} 102 103 104</script> 105
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/16 09:46
2017/09/16 11:23