前回は、
今回は、
オートリピートをどう作るか
さっそくサンプルプログラムを見てみましょう。これはメトロノームで、
<HTMLM><HEAD><TITLE>metronom</TITLE>
<SCRIPT type="text/javascript"><!--
var tempo = 0;
var accval = 0;
var arm = 0;
var t0_interval = 50;
var repeat_delay = 0;
var repeat_val = 0;
function update_tempo(val)
{
tempo += val;
if (tempo < 10)
tempo = 10;
else if (tempo > 300)
tempo = 300;
document.getElementById("tempo").innerHTML = tempo + "";
}
function int_t0()
{
accval += tempo;
if (accval >= 60000 / t0_interval) {
accval -= 60000 / t0_interval;
arm ^= 1;
document.getElementById("box").style.marginLeft = (arm)? "0px" : "200px";
}
if (repeat_delay <= 0)
;
else if (repeat_delay < 10)
repeat_delay++;
else
update_tempo(repeat_val);
}
function press_plus()
{
update_tempo(repeat_val = 1);
repeat_delay = 1;
}
function press_minus()
{
update_tempo(repeat_val = -1);
repeat_delay = 1;
}
function release()
{
repeat_delay = 0;
}
function init()
{
tempo = parseInt(document.getElementById("tempo").innerHTML);
setInterval(int_t0, t0_interval);
document.onmouseup = release;
}
// --></SCRIPT>
</HEAD><BODY onload="init()">
<H1>metronom</H1>
<DIV id="box" style="margin-left:0px; width:50px; height:50px; background:#f00;">
</DIV>
<P><BIG><BIG>
<SPAN onmousedown="press_minus()">[-1]</SPAN>
<SPAN id="tempo">120</SPAN>
<SPAN onmousedown="press_plus()">[+1]</SPAN>
</BIG></BIG></P>
</BODY></HTML>
[-1]や[+1]のボタンを押し続けると、
リピート処理をしているあいだ、
まずメトロノームの処理ですが、
そこで解決策として、
毎分61拍の場合、
次に、
マウスの場合、
グローバル変数に保存するのは、
こうして、
グローバル変数を減らす
さて、
このまま処理の数が増えていったら、
では、
C言語の場合
組込みシステムではC言語をよく使います。C言語の場合、
関数
/* mode=0:repeat 1:+=val -1:release */
static void button_proc(int mode, int val)
{
static int dir = 0;
static int delay = 0;
if (mode < 0) {
delay = 0;
return;
}
if (mode > 0) {
dir = val;
delay = 1;
} else if (delay <= 0)
return;
else if (delay < 10) {
delay++;
return;
}
update_tempo(dir);
}
前掲のサンプルとの整合性を優先したため、
この方法はいつでも利用できるというわけではありませんが、
JavaScriptの場合
一方、
クロージャの典型的な使い方は、
function 図形上でプレス()
{
var sx = x座標;
var sy = y座標;
document.onmousemove = function() {
var x移動量 = x座標 - sx;
var y移動量 = y座標 - sy;
/* 移動処理 */
};
ducument.onmouseup = function() {
document.onmousemove();
/* リリース処理 */
document.onmousemove = null;
document.onmouseup = null;
};
document.onmousemove();
}
「図形上でプレス」
では、
<HTML><HEAD><TITLE>metronom</TITLE>
<SCRIPT type="text/javascript"><!--
var tempo = 0;
var onrepeat = null;
function update_tempo(val)
{
tempo += val;
if (tempo < 10)
tempo = 10;
else if (tempo > 300)
tempo = 300;
document.getElementById("tempo").innerHTML = tempo + "";
}
function press_plus()
{
var delay = 0;
onrepeat = function(force) {
if ((force)||(delay >= 9))
update_tempo(1);
else
delay++;
};
document.onmouseup = function() {
onrepeat = null;
};
onrepeat(1);
}
function press_minus()
{
var delay = 0;
onrepeat = function(force) {
if ((force)||(delay >= 9))
update_tempo(-1);
else
delay++;
};
document.onmouseup = function() {
onrepeat = null;
};
onrepeat(1);
}
function init()
{
var t0_interval = 50;
var accval = 0;
var arm = 0;
tempo = parseInt(document.getElementById("tempo").innerHTML);
setInterval(function() {
accval += tempo;
if (accval >= 60000 / t0_interval) {
accval -= 60000 / t0_interval;
arm ^= 1;
document.getElementById("box").style.marginLeft = (arm)? "0px" : "200px";
}
if ((onrepeat))
onrepeat(0);
}, t0_interval);
}
// --></SCRIPT>
</HEAD><BODY onload="init()">
<H1>metronom</H1>
<DIV id="box" style="margin-left:0px; width:50px; height:50px; background:#f00;">
</DIV>
<P><BIG><BIG>
<SPAN onmousedown="press_minus()">[-1]</SPAN>
<SPAN id="tempo">120</SPAN>
<SPAN onmousedown="press_plus()">[+1]</SPAN>
</BIG></BIG></P>
</BODY></HTML>
onrepeatというグローバル変数は、
ボタンが押されたら、
また、
次回は処理の分割について、