AS3 - 抓住球赛

时间:2012-05-24 18:56:28

标签: actionscript-3 flash

我正在制作一个可以拖放球的小应用程序。当你用这个球击中球体时,你得到一个点,球移动到一个随机的坐标。我的问题是,在你第一次击中球体之后,它会改变位置,但是你可以再次击中它。这是代码(没有拖放)

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;
}
}

1 个答案:

答案 0 :(得分:0)

您的randXrandY变量似乎只会被评估一次。

因此,如果球的命中测试返回true,则球体的x / y坐标将在第一次更改,但永远不会再次更改。尝试这个怎么样:

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
    }
}