AS3将Child移除到非公共变量

时间:2013-04-10 09:19:29

标签: actionscript-3

我正在制作一个小游戏,在这个游戏中我会在一定时间后改变等级。它是一个跑步者,所以我每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循环之外我得到了障碍图像消失和双重叠加的障碍

1 个答案:

答案 0 :(得分: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()