まず、ifブロックposition.x
の1つで10ずつインクリメントし、もう1つで10ずつデクリメントします。両方のvelocity.x
意味だと思います。position.x
次に、想像してみmovingPlatform.position.x
てください。150で、enterFrameHandler
1回実行します。movingPlatform.position.x
160は160になり、次回enterFrameHandler
は呼び出されます。160は150以下でも260以上でもないため、ifブロックは実行されません。
速度を使用して、移動している側を示し、エッジを超えたら、次のように反転することができます。
// 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の間に配置するか、方向を2回以上反転しないようにチェックを追加してください。 。
これはそれを行うためのより良い方法かもしれません:
// 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;
}