这可能有一个我没有意识到的简单解决方案。使用以下代码(来自 php/javascript 拼图代码),我将一个对象与另一个对象(拼图)合并。我希望能够计算每次合并后有多少对象,以检查只剩下一个对象的情况(解决了难题)。我尝试编写一个单独的函数来计算对象的 hasownproperties 数量(尽管我只想计算合并的对象数量,而不是数量对象属性,以避免因不同的拼图难度级别而使此脚本复杂化),但它没有不工作。建议?
var cluster = new Array();
function merge_cluster(a, b) {
for (i in cluster[b]) {
obj = dd.elements[cluster[b][i]];
cluster[a][obj.name] = obj.name;
obj.cluster = a;
}
}
function my_DropFunc() {
align_cluster(dd.obj.cluster);
for (r = -1; r <= 1; r++) {
for (c = -1; c <= 1; c++) {
if (c != 0 || r != 0) {
obj = dd.elements['t_' + (dd.obj.row + r) + '_' + (dd.obj.col + c)];
if (obj) {
if (dd.obj.x + c * < ? = $vw ? > == obj.x && dd.obj.y + r * < ? = $vh ? > == obj.y) {
merge_cluster(dd.obj.cluster, obj.cluster);
//this is probably the ideal place to do a count check since it runs every time a piece is dropped, only once, whereas the merge_cluster function runs multiple times when a new cluster is merged.
}
}
}
}
}
}