問答収集 カテゴリ:Flashプログラミング
現在のスレッド一覧 / 新規に質問を投稿
レス数:5 / 状態:解決済み / No:110 / ATOM
1 名前:てつじん 日付:2013/02/02(土)17:54:41 ID:+wf4G83hjjDq File:[ 0.zip ]
初投稿です

今、as2を使ったシューティングゲームを製作しているのですが
そのプログラムでどうしてもわからないことがあります。


「円同士で当たり判定を取る」を参考にして自機と敵の当たり判定処理作ったのですが
敵を増殖させたかったのでduplicateMovieClip()を使い敵の数を増やすということを行いました。

これはそのfunction文です。
// --------------------------------------------------
//
// 敵の増殖
//
// --------------------------------------------------
function EnemyUP(eOrder){
for(e=1;e<EnemyMax;e++){
if(EnemyUse[e] == 0)break;
}
if(e == EnemyMax)return false;
// 敵の増殖
duplicateMovieClip ("t","t" +e, e + 100);

_root["t" + e]._x = eOrder.x;
_root["t" + e]._y = eOrder.y;

EnemyUse[e] = 1;
}


そして、敵インスタンス「t」に

onClipEvent (load) {
var enemyHP = 5; // 敵のHP
var tspeed = 2;// 移動速度
var dx2 = 0;// ランダムはねっ返り
var dy2 = 0;// ランダムはねっ返り

var ax = _x;// 中心 x 座標
var ay = _y;// 中心 y 座標
var ar = _width / 2;// 半径


// ランダム初期移動方向
varnRx = Math.floor(Math.random()*(500-1+1))+1;
varnRy = Math.floor(Math.random()*(500-1+1))+1;
vartrad2 = Math.atan2(nRy - this._y,nRx - this._x);
vardx2 = Math.cos( trad2 ) * this.tspeed;
vardy2 = Math.sin( trad2 ) * this.tspeed;
}

// 移動
onClipEvent (enterFrame) {
this.ax = this._x;
this.ay = this._y;
_x += this.dx2;
_y += this.dy2;


// 壁、自機接触時の当たり判定
if(this._x <= 0){ this._x = 0;
nRx = Math.floor(Math.random()*(500-1+1))+1;
nRy = Math.floor(Math.random()*(500-1+1))+1;
trad2 = Math.atan2(nRy - this._y,nRx - this._x);
dx2 = Math.cos( trad2 ) * tspeed;
dy2 = Math.sin( trad2 ) * tspeed;
}if(this._y <= 0){ this._y = 0;
nRx = Math.floor(Math.random()*(500-1+1))+1;
nRy = Math.floor(Math.random()*(500-1+1))+1;
trad2 = Math.atan2(nRy - this._y,nRx - this._x);
dx2 = Math.cos( trad2 ) * tspeed;
dy2 = Math.sin( trad2 ) * tspeed;
}if(this._x >= 500){ this._x = 500;
nRx = Math.floor(Math.random()*(500-1+1))+1;
nRy = Math.floor(Math.random()*(500-1+1))+1;
trad2 = Math.atan2(nRy - this._y,nRx - this._x);
dx2 = Math.cos( trad2 ) * tspeed;
dy2 = Math.sin( trad2 ) * tspeed;
}if(this._y >= 500){ this._y = 500;
nRx = Math.floor(Math.random()*(500-1+1))+1;
nRy = Math.floor(Math.random()*(500-1+1))+1;
trad2 = Math.atan2(nRy - this._y,nRx - this._x);
dx2 = Math.cos( trad2 ) * tspeed;
dy2 = Math.sin( trad2 ) * tspeed;
}


// 弾ヒット処理
for(k=0;k<_root.Gun2Max;k++){
if (this.hitTest(_root["ab" + k]._x,_root["ab" + k]._y,true) && _root["ab" + k]._visible){
_root["ab" + k]._visible = false;
_root.Gun2Delete(k);
if(this.enemyHP > 0){
this.enemyHP -= 1;
 this.gotoAndPlay(6);
}if(this.enemyHP <= 0){
this.gotoAndPlay(6);
eorder = new Object();
eorder.x = this._x;
eorder.y = this._y;

_root.EnemyUP(eorder);
this.enemyHP = 5;
Rx = Math.floor(Math.random()*(500-1+1))+1;
nRy = Math.floor(Math.random()*(500-1+1))+1;
trad2 = Math.atan2(nRy - this._y,nRx - this._x);
dx2 = Math.cos( trad2 ) * tspeed;
dy2 = Math.sin( trad2 ) * tspeed;
}
}
}

// 敵との接触判定 
bx = _root["target" ]._x;// 中心 x 座標
by = _root["target" ]._y;// 中心 y 座標
br = _root["target" ]._width / 2;// 半径
var sx = bx - ax;
var sy = by - ay;

var length = Math.sqrt(sx * sx + sy * sy);
if (length < (ar + br))
{
_root.qq = "当たってる";
}
else
{
_root.qq = "当たってない";
}
}
と打ちました。
どこが悪いのかわからないので全文写しました。
長くなってしまいすみません

すると、最初の敵に接触ときのみに「当たっている」が表示されるだけに
なってしまい、どう考えても自分の力では解決することができませんでした。

どうか知恵を貸していただきたいと思います。



2 日付:2013/02/02(土)20:19:48 ID:uqGvQ4UrzKin
デバッガで確認してみましたが、当たりは検出できていると思います。

1つのテキストフィールドに、当たり判定の結果を上書きして出力してるので
最後に動作した敵の結果だけが表示されています。

trace() 関数を使って確認してみて下さい。

// 敵との接触判定 
 ~略~
if (length < (ar + br))
{
//_root.qq = "当たってる";
trace("当たってる");
}
else
{
_root.qq = "当たってない";
}
3 日付:2013/02/02(土)20:45:32 ID:uqGvQ4UrzKin
講座では Math.sqrt() 関数を使っていますが、
Math.sqrt() 関数を使わずに、(ar + br) の方を2乗した方が
高速に計算できてオススメです。

if ((sx * sx + sy * sy) < (ar + br) * (ar + br))
{
trace("当たってる");
}
else
{
_root.qq = "当たってない";
}
4 名前:てつじん 日付:2013/02/03(日)00:01:52 ID:+wf4G83hjjDq
おお!確かに実際のダメージ処理をさせるとちゃんとできました!

ID:uqGvQ4UrzKinさんありがとうございます!
5 名前:状態変更 日付:2013/02/03(日)00:02:01 ID:+wf4G83hjjDq
この質問の状態を『解決』に変更しました。
このスレッドについて
質問の状態 :
解決済み
投稿開始日 :
2013/02/02(土)17:54:41
投稿終了日 :
2013/02/10(日)00:02:12
投稿者 :
てつじん
レス総数 :
5
スレッド番号 :
110
MondoCollectionSystem ver.0x00020000 by Hakuhin