這是控制我角色的代碼。 「控制」是按鈕類的名稱。我使用for循環來聲明添加觸摸事件偵聽器。我可以在接觸時改變顏色。索引0上的元素用於左轉,索引1上的元素用於跳轉。返回意外號碼
問題: 當我用控制檯登錄觸摸事件函數檢查i變量的數量時,它的返回值爲15.我只有4個元素。 15我的案件不匹配。
我的問題是我如何返回15?
var getControl = {
buts : document.getElementsByClassName("control"),
build : function(world){
for(i = 0; i < 4; i++){
this.buts[i].addEventListener("touchstart",function(e){
console.log(getControl.temp);
e.preventDefault();
this.style.backgroundColor = "green";
console.log(i);
switch(i){
case 0:
misc[0].left();
console.log("left");
break;
case 1:
misc[0].jump();
break;
case 2:
misc[0].right();
break;
case 3:
break;
};
},false);
this.buts[i].addEventListener("touchend",function(e){
e.preventDefault();
this.style.backgroundColor = "red";
},false);
this.buts[i].addEventListener("touchleave",function(e){
e.preventDefault();
this.style.backgroundColor = "red";
},false);
到底如何,何時何地你檢查它的價值呢? – zerkms