私はゲームを作成しており、敵と弾丸があります。弾丸が敵に当たると、私は敵と弾を破壊したい。私はhitTestPoint()
メソッドを使用して、弾丸が敵に当たったかどうかをテストしています。hitTestPoint()は衝突を正しくテストしていません
for each(var bullet:Bullet in this.bullets) {
for each(var enemy:Enemy in this.enemies) {
if(enemy.hitTestPoint(bullet.x, bullet.y)) {
trace("hit");
}
}
bullet.update();
}
this.bullets
とthis.enemies
は弾丸と敵のためのオブジェクトを含む両方の配列です:ここに私のゲームループ内のコードです。 -
package com {
import flash.display.MovieClip;
import flash.display.Stage;
public class Enemy extends MovieClip {
public var speed:Number = 4;
private var stageRef:Stage;
public function Enemy(stage:Stage) {
this.stageRef = stage;
this.x = this.stageRef.stageWidth/3;
this.y = this.stageRef.stageHeight/2;
}
public function update() {
}
}
}
問題であり、xおよび弾丸と敵のyの値の両方ではなく、同じである場合hitTestPoint
のみ真を返す
package com {
import flash.display.MovieClip;
import flash.display.Stage;
public class Bullet extends MovieClip {
private var stageRef:Stage;
public var speed:Number = 10;
public function Bullet(stage:Stage) {
this.stageRef = stage;
}
public function update() {
this.x += Math.sin((Math.PI/180) * (360 - this.rotation)) * this.speed;
this.y += Math.cos((Math.PI/180) * (360 - this.rotation)) * this.speed;
}
}
}
:ここで、これら二つのクラスです2つのムービークリップが重なる場合よりもこれは、弾丸が敵を右に通過することにつながりますが、ヒットとして登録されません。多分私はバウンディングボックスを欠いているでしょうか?
hitTestPoint
は、弾丸と敵の座標が同じである場合にだけでなく、弾丸が敵に当たると真を返すことができますか?
ありがとうございました!
これはよかったです。ありがとうございます。それはまだ少しバグがあるようですが。時には、弾丸がヒットを検出せずに右端を通過することもありますが、弾丸が動いたときに弾丸がスキップするためかもしれません。 –
あなたのフレームレートになる可能性があります。あなたの弾丸がフレーム間のオブジェクトの上に飛ぶ場合、それはミスになります。 – Sam