中的一个的第一,如果你的块由10你是10递减velocity.x
另一个递增position.x
我猜你的意思是在这两个position.x
。
其次,想象一下movingPlatform.position.x
是150,你的enterFrameHandler
运行一次。 movingPlatform.position.x
将变为160,并且在下一次enterFrameHandler
被调用时,如果块将被执行,则由于160不小于或等于150或大于或等于260而不被调用。
可以使用速度来指示侧其运动并倒转,一旦你超越的优势,是这样的:
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150 || movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -movingPlatform.velocity.x;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
显然,如果对象是已在假设X = 100,这将只是保持反相它的速度,所以要么使这可能会导致问题确保将其150-260之间,或者添加额外的检查,以防止其反转它的方向不止一次。
这可能是做一个更好的方式:
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150) {
movingPlatform.velocity.x = 1;
} else if (movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -1;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
胡感谢的人,这工作就像一个魅力。仍然让我感受到颈部特性。 – Robster 2013-03-26 23:26:47