我的程序中有一个按钮功能。大多数工作,但当我点击它,它不会执行if。代码包括在内

时间:2018-06-05 04:58:01

标签: javascript function

var rectbutton = function(x,y,width,height,bevel,label,basecolor,textcolor,hovercolor,changevar,changevarvalue){
textAlign(CENTER,CENTER);
textSize(height);
fill(basecolor);
rect(x-width/2,y-height/2,width,height,bevel);
fill(textcolor);
text(label,x,y+5);
if(mouseX>=x-width/2 && mouseX<=x+width/2 && mouseY>=y-height/2 && mouseY<=y+height/2){
    fill(hovercolor);
    rect(x-width/2,y-height/2,width,height,bevel);
    fill(textcolor);
    text(label,x,y+5);
    if(mouseIsPressed){
        var changevar=changevarvalue;
    }
}
}; 

使用: rectbutton(200,250,250,50,5,"PLAY",color(255,255,255),color(0,0,0),color(255,0,0),playerState,1);

一切正常,直到我点击。不会将changevar设置为changevarvalue。尝试changevar=changevarvalue;代替var changevar=changevarvalue;

2 个答案:

答案 0 :(得分:0)

In your code snipped there is no mouseIsPressed defined anywhere, is it missing? You might need to provide us a more complete code example.

Also try to look at your console if any javascript errors occure.

答案 1 :(得分:0)

在js中,对象通过引用传递,基元通过值传递。如果要更新changevar变量,则必须在调用rectbutton函数时传递changevar参数中的对象,而不是某些原始值。我假设playerState包含一些原语而不是对象作为值。