0

我正在制作一个小游戏,在这个游戏中我会在一段时间后改变关卡。它是一个跑步者,所以我每 100px 创建一个平台部分,在每个平台上都有机会制作横幅、硬币或跳过的障碍物。这些是在 for 循环中创建的。我的问题是当我切换级别时,如果舞台上有障碍物我想删除 Child,但由于 var 仅存在于 if 语句中,我无法运行 _obstacle.removeCild() 只是因为它不存在于我的更改级别功能。下面的代码:

//Go through all the sections and add obstacles or banners or coins based on a chance value. If an obstacle is created in a section, a coin can't be created in the same section
for ( _indexA = 0 ; _indexA < _indexB ; _indexA++ )
{
    if ( Math.random() < _bannerChance && _bannerDelay == 0 )
    {
//Create banner
        var _banner:MC_banner = new MC_banner();
        _platformArray[_platformArray.length - 1].addChild(_banner);
        _banner.x = _platformArray[_platformArray.length - 1].getChildAt(_indexA).x + _platformArray[_platformArray.length - 1].getChildAt(_indexA).width * 0.5;
        _banner.gotoAndStop(Math.ceil(Math.random() * _banner.totalFrames));
        _banner.rotation = int( Math.random() * 4 + 0.5) - 2;
        _banner.cacheAsBitmap = true;
        _bannerDelay = 100;
    }
    else if ( Math.random() < _obstacleChance )
    {
        var _obstacle:MC_obstacle = new MC_obstacle();
        var _obstacle2:MC_obstacle2 = new MC_obstacle2();
//CUSTOM LEVEL CHANGE
            if (_currentLevel == 1){
                _platformArray[_platformArray.length - 1].addChild(_obstacle);
                _obstacle.x = _platformArray[_platformArray.length - 1].getChildAt(_indexA).x + _platformArray[_platformArray.length - 1].getChildAt(_indexA).width * 0.5;
                _obstacle.gotoAndStop(Math.ceil(Math.random() * _obstacle.totalFrames));
                _obstacle._state = 0;

            }

            if (_currentLevel == 2){
                _platformArray[_platformArray.length - 1].addChild(_obstacle2);
                _obstacle2.x = _platformArray[_platformArray.length - 1].getChildAt(_indexA).x + _platformArray[_platformArray.length - 1].getChildAt(_indexA).width * 0.5;
                _obstacle2.gotoAndStop(Math.ceil(Math.random() * _obstacle2.totalFrames));
                _obstacle2._state = 0;
            }
    }
    else if ( Math.random() < _coinChance )
    {
//Create coin
        var _coin:MC_coin = new MC_coin();
        _platformArray[_platformArray.length - 1].addChild(_coin);
        _coin.x = _platformArray[_platformArray.length - 1].getChildAt(_indexA).x + _platformArray[_platformArray.length - 1].getChildAt(_indexA).width * 0.5;          
        _coinArray.push(_coin);
    }
}

我怎样才能删除这些孩子?var _obstacle:MC_obstacle = new MC_obstacle(); ? 任何帮助都会很棒!!!提前致谢

注意:如果我使用这个 var _obstacle:MC_obstacle = new MC_obstacle(); 在 for 循环之外,我得到障碍物图像消失和双重堆叠障碍物

4

1 回答 1

1
_platformArray[_platformArray.length - 1].removeChildAt(0)

或者

for(var a:int = 0;a<_platformArray[_platformArray.length - 1].numChildren;a++){
_platformArray[_platformArray.length - 1].removeChildAt(a)
}

或者如果你使用 fp 11+

_platformArray[_platformArray.length - 1].removeChildren()
于 2013-04-10T09:29:56.190 回答