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;
答案 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
包含一些原语而不是对象作为值。