切换句柄多维数组.意外结果
Switch handle multidimensional array. Unexpected result
所以我有一个javascript函数,它将多维数组值推送到参数,如下所示:
function mouseHandle(x,y){
for(var i=0; i<buttonPos.length; i++){
if(x>buttonPos[i][0] && x<buttonPos[i][2]){
if(y>buttonPos[i][1] && y<buttonPos[i][3]){
eventButton(buttonPos[i][4]);
};
};
};
};
这按下了 buttonPos[i][4] 到 eventButton 函数,它是:
function eventButton(d){
switch(d){
case 0: // STARTBUTTON
alert("Button");
break;
default:
alert("NoButton");
};
};
数组在 drawButton 函数中设置如下:
function drawButton(x,y,width,height,string,event){
xCenterButton=x+(width/2);
yCenterButton=y+(height/2);
ctx.fillStyle="rgba(242,255,195,1)";
ctx.fillRect(x,y,width,height);
ctx.rect(x,y,width,height);
ctx.fillStyle="rgba(0,0,0,1)";
ctx.stroke();
ctx.font="25px Arial";
fontSize = getFontSize();
centerNum = fontSize/4;
ctx.fillStyle="rgba(0,0,0,1)";
ctx.textAlign="center";
ctx.fillText(string,xCenterButton,yCenterButton+centerNum);
buttonPos.push([[x],[y],[x+width],[y+height],[event]]);
};
然后,我在menuStart函数中调用该函数,如下所示:
function menuStart(){
drawButton(getCenterX(100),getCenterY(50),100,50,"Start",0);
};
因此,mouseHandle 函数确实按预期为 eventButton 函数提供了 0 参数(我在默认情况下提醒了"d"参数)。但是,就好像 switch 语句无法识别 0,因为它使用默认大小写并提醒"NoButton"。
知道为什么这不起作用吗?
注意 - JSFIDDLE 按照要求。 :: http://jsfiddle.net/jWFwX/
首先解析 d 和 int。工作 JSFiddle
新代码:
function eventButton(d){
var buttonInt = parseInt(d);
switch(buttonInt){
case 0: // STARTBUTTON
alert("Button");
break;
default:
alert("NoButton");
alert(d);
};
};
相关文章:
- 意外结果,在 ASP.Net 中解析 JSON 对象的数组
- Javascript 数组反向函数意外行为
- 使用变量初始化数组:意外的令牌 +
- 在 JS 中短路空数组会产生意外结果:“[] ||真 == []'
- 推入数组会产生意外的结果
- JavaScript在数组内迭代数组的意外行为
- JavaScript数组-意外的令牌
- JavaScript:推送到空数组中的意外行为
- 意外的类型错误 - Javascript,多维数组
- 在用数组填充初始化的数组上使用数组映射的意外行为
- 切换句柄多维数组.意外结果
- 随后的多维数组排序会产生意外的结果
- 将数组项用作对象中的键时出现意外的令牌错误
- 从数组中拼接出意外的元素
- JavaScript中2D数组的值发生意外更改
- JavaScript 二维数组填充中的意外输出
- 将字段插入子文档数组中.意外行为
- AngularJS分析JSON数组对象意外的令牌:
- 意外标记d'当尝试将对象添加到Javascript对象数组时
- 传递对象数组-意外令牌[