This is a code to control my character. "control" is a name of button class. Im using for loop to declare adding touch event listener. I had could change the color while its on touch. element on index 0 is for turn left, element on index 1 is for jump.
problem : when i check the number of i variable with console log in touch event function, its return 15. I only had 4 element. 15 was not match on my cases.
My question is how the i variable returns 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);