Error #1034 on if(contains(array))

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?

Answers


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

Need Your Help

Spring programmatic transaction with parameter

java hibernate spring transactions

I'm working on some data access logic with Spring, my question has to do with transactions. The transaction documentation http://static.springsource.org/spring/docs/2.5.x/reference/transaction.html...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.