math.random在产生敌人时给予2879.95

时间:2013-05-27 20:34:24

标签: actionscript-3 flash actionscript

我有这段代码:

for(i = 0; i < mySharedObject.data.randomspawns; i++)
{
    var slobbytwenty  = new Slob();
    slobbytwenty.gotoAndStop(2);
    var Highx:Number = stage.stageWidth + (stage.stageWidth / 2);
    var Lowx:Number = stage.stageWidth - stage.stageWidth - (stage.stageWidth / 2);
    var Highy:Number = stage.stageWidth + (stage.stageWidth / 2);
    var Lowy:Number = stage.stageWidth - stage.stageWidth - (stage.stageWidth / 2);
    slobbytwenty.x = Math.floor(Math.random()*(1+Highx-Lowx))+Lowx;
    slobbytwenty.y = Math.floor(Math.random()*(1+Highy-Lowy))+Lowy;
    addChild(slobbytwenty);
    slobbytwenty.addEventListener(TouchEvent.TOUCH_BEGIN, shootingwhoop);
    slobbytwenty.addEventListener(TouchEvent.TOUCH_MOVE, movewhoop);
    slobarray.push(slobbytwenty);
}

但是一个数组项总是给出一个2879.95的X坐标。这是为什么?

1 个答案:

答案 0 :(得分:0)

我非常愚蠢,我发现我有以下内容:

if(slobbytwenty.x > fencing.sides.width)
            {
                slobbytwenty.x = fencing.sides.width;
            }
            if(slobbytwenty.x < fencing.sides.width - fencing.sides.width)
            {
                slobbytwenty.x = fencing.sides.width;
            }
            if(slobbytwenty.y > fencing.sides.height)
            {
                slobbytwenty.y = fencing.sides.height;
            }
            if(slobbytwenty.y < fencing.sides.height - fencing.width)
            {
                slobbytwenty.x = fencing.sides.width;
            }

......这搞砸了我的游戏。 :P

事实证明条件中的边界不正确,反正我也不需要代码。