简体   繁体   English

切换句柄多维数组。 意外结果

[英]Switch handle multidimensional array. Unexpected result

so I have a javascript function that pushes a multidimensional array value to a parameter like so: 所以我有一个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]);
            };
        };
    };
};

This pushed buttonPos[i][4] to the eventButton function which is: 这将buttonPos [i] [4]推送到eventButton函数,该函数是:

function eventButton(d){
    switch(d){
        case 0: // STARTBUTTON
            alert("Button");
            break;
        default:
            alert("NoButton");
    };
};

The array is set in the drawButton function as so: 数组在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]]);
};

I then call that function in the menuStart function as so: 然后,我在menuStart函数中这样调用该函数:

function menuStart(){
    drawButton(getCenterX(100),getCenterY(50),100,50,"Start",0);
};

So the mouseHandle function DOES give the eventButton function a parameter of 0 as expected (I alerted the 'd' parameter within the default case). 因此,mouseHandle函数确实为eventButton函数提供了一个预期的0参数(在默认情况下,我提醒了'd'参数)。 However it's as if the switch statement doesn't recognise the 0 as it uses the default case and alerts 'NoButton'. 但是,就像switch语句无法识别0一样,因为它使用默认大小写并警告“ NoButton”。

Any idea why this is not working? 知道为什么这行不通吗?

NOTE - JSFIDDLE AS REQUESTED. 注意-要求的JSFIDDLE。 :: http://jsfiddle.net/jWFwX/ :: http://jsfiddle.net/jWFwX/

Parse d into and int first. 首先将d解析为int。 Working JSFiddle 工作中的JSFiddle

New code: 新代码:

function eventButton(d){
    var buttonInt = parseInt(d);
    switch(buttonInt){
    case 0: // STARTBUTTON
        alert("Button");
        break;
    default:
        alert("NoButton");
        alert(d);
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM