0
votes

I am creating a horizontal platform game in Flash, and I'm trying to create a function that removes the MovieClip that contains the obstacles which you can crash into, before adding the entire map again, making a kind of a restart.

blockArr contains half of the obstacles.

flameArr contains the other half.

These arrays are filled in the Map1-function.

The function:

    function removeBlocks(TimerEvent):void{
for (var t:int = 0; t < blockArr.length; t++){
    if(blockArr[t] == null){
        blockArr = [];
        }
    if(blockArr[t] != null){
        if(contains(blockArr[t])){
        blockArr[t].parent.removeChild(blockArr[t]);
        blockArr[t] = null;
        }
    }
    if(flameArr[t] == null){
        flameArr[t] = [];
    }
    if(flameArr[t] != null){
        if(contains(flameArr[t])){
        flameArr[t].parent.removeChild(flameArr[t]);
        flameArr[t] = null;
        }
    }
    trace(blockArr);
    if(blockArr == []){
        Map1();
        removeMapTimer.stop();
        blockMoveTimer.start();
    }
}
}

However, this code returns this error:

TypeError: Error #1034: Type Coercion failed: cannot convert []@2754c641 to flash.display.DisplayObject.
at ImpGameWork14_fla::MainTimeline/removeBlocks() [ImpGameWork14_fla.MainTimeline::frame2:98]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

The intention for this code is to remove and delete the content of these arrays, as that is the obstacles/map. Afterwards, it will check whether the main obstacle array is empty if(blockArr == []) and if it is, deploy the Map1-function, which adds obstacles to arrays and to stage (addChild(obstacle)). Any help on how to achieve that goal?

1
Your code is first not full and second not self-explainatory. I cannot understand what's going on, but this is truly strange for me: blockArr = [];. Anyways - find the line with the error and check what's wrong. Also, if contains(blockArr[t]), this means you can just use removeChild(blockArr[t]) instead of this freaky .parent.removeChild...Andrey Popov
Thanks! I was in a rush while creating this question, and yes, I'm a beginner-coder, so it's absolutely not self-explanatory! I thought I got rid of another error-code with that freaky code, but must have been something else, got rid of it now.FluidSense
I tried to find a way to say that if I have already removed the obstacles, ensure that the array are empty. It did not do what I hoped for it though. Thus, I could merely make an if-statement that checked if blockArr == [] to check if the entire array was removed from stageFluidSense
I don't understand your current question. If you want to check if the array is empty, use if (blockArr.length == 0).Andrey Popov
I don't want to go OT but if i where you i would use a single Array. If you use 2 Array you should run 2 "for". Last thing you should separate the responsibilities and take out the code that loads the map again.SharpEdge

1 Answers

0
votes

Following the comment here a possible solution:

public function Reset(ev:TimerEvent):void
    {
        //Remove the Blocks
        removeBlocks();

        reloadMap();

    }

    private function removeBlocks():void
    {
        //Cycle each block
        for each(var block : DisplayObject in blockArr)
        {
            //If it is in the DisplayList 
            if(this.contains(block))
                this.removeChild(block); //Remove it                
        }

        //Clear the Array
        blockArr.length = 0;
    }

    private function reloadMap():void
    {
        Map1();
        removeMapTimer.stop();
        blockMoveTimer.start();
    }

About the Error #1034 in your code, the compiler is just telling you that "contains(DisplayObject)" need a DisplayObject and the type of "blockArr[i]" is Object. To remove the Error the solution is to tell the compiler that the type of blockArr[i] is DisplayObject (Type Casting):

this.contains(blockArr[i] as DisplayObject);