这可能有一个我没有意识到的简单解决方案。使用以下代码(来自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.
}
}
}
}
}
}