سؤال

I have the following function constantly running to check for collision between a player-controlled ball (mcBall) and a number of walls (aptly named mcWall1, mcWall2, etc.): (the var numberofwalls gets its own value in each different frame where there is a different number of movieclips.)

function checkcollision(evt:Event) : void {
        for(var i = 1; i <= numberofwalls; i++){
            if (mcBall.hitTestObject("mcWall"+i) == true){
            killball()
            }
        }
    }
}

However when I do this, I get the following error upon compiling:

Scene 1, Layer 'Actions', Frame 1, Line 89 1067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject.

Line 89 is this:

if (mcBall.hitTestObject("mcWall"+i) == true){

My understanding is that it is trying to add a string and an int, but I don't see why it doesn't just add the integer to the end of the string (eg: mcWall1 where i = 1).

I cant use ("mcWall"[i]) as I do not have an array set up and don't think it necessary for the amount of walls I will be using.. each wall is given an instance name statically on the stage, and not in my code.

Any suggestions on how I can make it test for mcWall(i) ?

Cheers in advance.

هل كانت مفيدة؟

المحلول

It's true what compiler says. hiTestObject() function gets DisplayObject as an argument, but you are passing a string "mcWall"+i. Try:

if (mcBall.hitTestObject(this.getChildByName("mcWall"+i)) == true){
    killBall();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top