if语句的多个条件? (代码不起作用)

时间:2015-04-01 05:48:53

标签: actionscript-3 flash

这是一个类似拼图的益智游戏。我希望它能在所有部件到位后直接进入下一个场景。因此,每个“地图片段”旁边都是它在适当位置的坐标。

但是//惊喜突然//它不起作用:c是否有可能首先放入这么多条件?

感谢您阅读c:(justABeginner)

if (map1.x== 259.45 && 
map1.y== 77.05 &&

map2.x== 368.3 &&
map2.y== 69.45 && 

map3.x== 445.30 &&
map3.y== 90.4 &&

map4.x== 288.5 &&
map4.y== 207.15 &&

map5.x== 325.75 &&
map5.y== 164.65 &&

map6.x== 436.20 &&
map6.y== 187.65)

{
gotoAndStop (1, "Scene 3");
}

3 个答案:

答案 0 :(得分:0)

代码看起来很好。如果条件不评估为真,那么它实际上可能是不同的值。在像printf这样的If语句之前进行简单的调试步骤,以检查值是否完全相同。

有时,精度将在if语句中发挥重要作用。

快乐编码:)

答案 1 :(得分:0)

您可能会发现它不起作用,因为它很难将对象与子像素精度对齐。因此,您可能需要考虑“铺设”或“舍入”您的数字。我建议使用地板以避免将其四舍五入到下一个值。

if (Math.floor(map1.x) == 259 && Math.floor(map1.y) == 77 &&
    Math.floor(map2.x) == 368 && Math.floor(map2.y) == 69 && 
    Math.floor(map3.x) == 445 && Math.floor(map3.y) == 90 &&
    Math.floor(map4.x) == 288 && Math.floor(map4.y) == 207 &&
    Math.floor(map5.x) == 325 && Math.floor(map5.y) == 164 &&
    Math.floor(map6.x) == 436 && Math.floor(map6.y) == 187)
{
    gotoAndStop (1, "Scene 3");
}

做这样的事情会给你一个更“模糊”的比较,并且应该更容易排列拼图。此外,您可能想要考虑通过添加“对齐放置”功能来帮助用户......

function inrange(targetX, targetY, mapX, mapY, strength) {
    targetX -= (strength / 2);
    targetY -= (strength / 2);
    return (mapX >= targetX && mapX <= (targetX+strength) &&
        mapY >= targetY && mapY <= (targetY+strength));
}

//snap map piece into place if within 3 pixels
if (inrange(259, 77, map1.x, map1.y, 3)) {
  map1.x = 259;
  map1.y = 77;
}

答案 2 :(得分:0)

作为一般建议,您不应该使用带浮点数的等式检查(==)。在某些时候,你会发现你不能相信0.5 + 0.2等于0.7。它最终可能等于0.70000000001或0.6999999999999

正如其他人所提到的那样,尝试对你的数字进行四舍五入,或者更好的是,尝试一个小范围,在这个范围内,这个部分会被卡入位置,无论是正负10个像素?

相关问题