2012-05-24 114 views
0

我正在做一个小应用程序,你可以拖放一个球。当你用这个球打出一个球时,你会得到一个点并且球会移动到一个随机的坐标上。我的问题是,第一次击球后,它会改变位置,但你可以再次击球。下面是代码(不包括拖放)AS3 - 抓球游戏

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

回答

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

非常感谢,固定:) – Zoske