Created
October 15, 2024 21:01
-
-
Save naosim/63d386edc554384dc9d5b36a90d2aa47 to your computer and use it in GitHub Desktop.
変更を監視する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChangeEventChecker { | |
add(name, checkerFunc) { | |
this[name] = {checkerFunc, lastValue:undefined, isChanged:false}; | |
return this; | |
} | |
check() { | |
Object.entries(this).forEach((e) => { | |
const [k, v] = e; | |
const value = v.checkerFunc(); | |
if(v.lastValue !== undefined) { | |
v.isChanged = v.lastValue != value; | |
} | |
v.lastValue = value; | |
}) | |
} | |
} |
本クラスを使う場合
var ex = 0; // 経験値
var lv = 1; // レベル
var changeEventChecker;
init = function() {
changeEventChecker = new ChangeEventChecker();
changeEventChecker.add("lv", () => lv);// 監視対象を設定
}
update = function() {
ex++;
if(ex % 100 == 0) {
lv++;
}
}
draw = function() {
changeEventChecker.check();
if(changeEventChecker.lv.isChanged) {
// 音を鳴らす
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
本クラスを使わな場合