中的一個的第一,如果你的塊由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