0

基本的に、プレイヤーが特定のアイテムをかわす必要があるゲームを作成しようとしていますが、これまでのところ、3 つのサメをランダムにステージに追加するコードがあります。

アイデアは、プレーヤーがサメにぶつかると、開始位置に戻ると、サメ​​の速度、速度などを含むアクションスクリプトファイルがあり、プログラムが実行されるたびにサメが表示されます別の場所。

ただし、サメの衝突テストを実行しようとすると、サメ​​の 1 つだけが応答し、3 つすべてのサメがプレイヤー (square_mc) に影響を与えるようにする方法がわかりません。どんな助けでも大歓迎です。

//Pirate game, where you have to avoid particular object and get to the finish line to move onto the final level.

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveMode ); 
function moveMode(e:KeyboardEvent):void {

//movements for the pirate ship, this will allow the ship to move up,down,left and right.

if (e.keyCode == Keyboard.RIGHT) {
    trace("right"); 
square_mc.x = square_mc.x + 25;
} 
else if (e.keyCode == Keyboard.LEFT) {
    trace("left"); 
square_mc.x = square_mc.x - 25;
} 
else if (e.keyCode == Keyboard.UP) {
    trace("up"); 
square_mc.y = square_mc.y - 25;
} 
else if (e.keyCode == Keyboard.DOWN) {
    trace("down");
square_mc.y = square_mc.y + 25;
}
}   

//for.fla
//this program uses a for loop to create my Sharks
//a second for loop displays the property values of the sharks

function DisplayShark():void{
for (var i:Number=0;i<3;i++)
{
    var shark:Shark = new Shark(500);
    addChild(shark);

    shark.name=("shark"+i);
    shark.x=450*Math.random();
    shark.y=350*Math.random();

}
}
DisplayShark();

for(var i=0; i<3;i++){
var currentShark:DisplayObject=getChildByName("shark"+i);

trace(currentShark.name+"has an x position of"+currentShark.x+"and a y position  of"+currentShark.y);
}



//here we will look for colliosion detection between the two move clips.

addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void { 

if (square_mc.hitTestObject(currentShark))
{ 
trace("The Square has hit the circle");
    square_mc.x=50
    square_mc.y=50  //these lines of code return the square back to it's     original location
}

}

4

1 に答える 1

0

for ループを ENTER_FRAME に移動するだけです。

addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void { 

    for(var i=0; i<3;i++){    
        var currentShark:DisplayObject=getChildByName("shark"+i);
        if (square_mc.hitTestObject(currentShark))
        { 
            trace("The Square has hit the circle");
            square_mc.x=50;
            square_mc.y=50;
        }
    }

}

for ループを 1 回だけ実行して currentShark 変数を設定することはできません。衝突テストを実行するたびに、1 匹のサメに対してテストすることになります。むしろ、衝突をチェックするたびに、すべてのサメをループして衝突テストを行う必要があります。

于 2012-08-15T14:34:22.717 に答える