我需要将 if 参数设为可选。如果这是有道理的。
如果您注意到我的两个功能 98% 相同,我需要将这种差异转换为参数,它只是不适合我。
getElement(x, y, class)
新参数类更改了我在代码中标记为 //HERE 的内容
//get the element under the mouse, ignoring all transparency.
function getElement(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0){ /////////////////////////////////////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if(pixelAlpha != 0){ ///////////////////////////////////////// AND HERE
return elem;
}
}
return -1;
}
//get the tile under the mouse, even if it's behind an object
function getTile(x, y){
var elem = document.elementFromPoint(x, y);
if($(elem).parents("#game").length != 1 && $(elem).attr("id") != "tileHighlight"){ return -1; }
var pixelAlpha = mousePixelOpacity(elem, x, y);
var track = Array;
for(var i = 0;i<50;i++){
if($(elem).attr("id") == "game"){ return -1;}
if(pixelAlpha == 0 || $(elem).attr('class') != "tile"){ /////////HERE
track[i] = elem;
for(var z = 0; z<i+1; z++){ //hide elements
$(track[z]).hide();
}
elem = document.elementFromPoint(x, y); //set the element right under the mouse.
pixelAlpha = mousePixelOpacity(elem, x, y);
for(var z = 0; z<i+1; z++){ //show all the recently hidden elements
$(track[z]).show();
}
}
if($(elem).attr('class') == "tile" && pixelAlpha != 0){ ///// AND HERE
return elem;
}
}
return -1;
}
我在想类似的东西
getElement(x, y, "title");
//(This can be right) OR (both of these can be right.)
if( (pixelAlpha == 0) || (class="tile" && onlycountclassifIsaidsovar) ){}
顺便说一句,如果你想看看这只小狗在行动,我在http://untitled.servegame.com 。
谢谢!