diskrim
diskrim

Reputation: 145

AS3 Remove Child to non public variable

I am making a small game, inside this game I change levels after a certain amount of time. It is a runner, so I create platform sections every 100px, on each platform is a chance to make a banner, coin, or an obstacle to jump over. These are created in a for loop. My problem is when I switch levels, and if there is an obstacle on the stage I want to remove Child, but since the var only exists in the if statement, I can not run _obstacle.removeCild() simply because it doesnt exists in my change level function. Code Below:

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

How can I remove these children? var _obstacle:MC_obstacle = new MC_obstacle(); ? Any help would be great!!! Thanks in advance

NOTE: If i take this var _obstacle:MC_obstacle = new MC_obstacle(); outside of the for loop I get obstacle images dissapearing and double stacked obstacles

Upvotes: 0

Views: 92

Answers (1)

Urosan
Urosan

Reputation: 321

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

or

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

or if u using fp 11+

_platformArray[_platformArray.length - 1].removeChildren()

Upvotes: 1

Related Questions