0

ボールをドラッグ アンド ドロップできる小さなアプリを作成しています。このボールを球体に当てるとポイントが得られ、ボールはランダムな座標に移動します。私の問題は、最初に球を打った後、位置が変わりますが、もう一度打つことができるということです。これがコードです(ドラッグアンドドロップなし)

ball.addEventListener(Event.ENTER_FRAME, hit);

var randX:Number = Math.floor(Math.random() * 540);
var randY:Number = Math.floor(Math.random() * 390);

function hit(event:Event):void
{
if (ball.hitTestObject(s)){ //s is the sphere
    s.x = randX + s.width;
    s.y = randY + s.width;
}
}
4

1 に答える 1

0

あなたのrandXandrandY変数は一度しか評価されないようです。

したがって、ボールのヒット テストが true を返した場合、球の x/y 座標は最初は変更されますが、2 回目以降は変更されません。これを試してみてはどうですか:

function hit(event:Event):void
{
    if (ball.hitTestObject(s))
    {
        //s is the sphere
        s.x = Math.floor(Math.random() * 540) + s.width;
        s.y = Math.floor(Math.random() * 390) + s.height; // note I changed width to height
    }
}
于 2012-05-24T19:01:30.730 に答える