0
votes

I'm getting this error when I'm getting an element from an array and trying to use some functions on it:

TypeError: Error #1034: Type Coercion failed: cannot convert jogador$ to jogador. at laser/mover_tiro_baixo()

Sorry it's in portuguese, just like the code i'll paste below, but I think you get it: when I retrieve the element from an array it's of type 'jogador$', and if I try to use it as being of 'jogador' it doesn't work. I'm trying to manually force the coercion, as it was trying to convert the object to a DisplayObject (because I'm trying to use the hit test function), but that also didn't work:

TypeError: Error #1034: Type Coercion failed: cannot convert jogador$ to flash.display.DisplayObject. at laser/mover_tiro_baixo()

Code:

package {

    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.debugger.enterDebugger;
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    import flash.display.DisplayObject;


    public class laser extends MovieClip {

        private var velo: Number;
        private var meuPalco: Stage;
        var dono: MovieClip;
        var inimigoTipo: Number;
        var Inimigos: Array;
        var Dano: Number;
        var Tam:Number;
        var i:Number;

        public function laser(palco: Stage, posX: Number, posY: Number, velocidade: Number, dano: Number, CimaBaixo: Number, Dono: MovieClip, vetJogadores: Array) {
            this.dono = Dono;
            this.Dano = dano;
            if (getClass(this.dono) == "jogador") {
                inimigoTipo = 0;
                Inimigos = jogador(this.dono).VetorInimigos;
            } else {
                inimigoTipo = 1;
                Inimigos = vetJogadores;
            }
            this.meuPalco = palco;
            this.velo = velocidade;
            this.x = posX;
            this.y = posY;
            if (CimaBaixo == 1) {
                this.addEventListener(Event.ENTER_FRAME, mover_tiro_cima);
            } else {
                this.addEventListener(Event.ENTER_FRAME, mover_tiro_baixo);
            }
            meuPalco.addChild(this);
        }


        public function mover_tiro_cima(evt: Event) {
            this.y -= velo;
            if (inimigoTipo == 0) { // Dono do tiro é o player          
                var Tam: Number = Inimigos.length;
                var i: Number = 0;
                while (i < Tam) {
                    if (this.hitTestObject(Inimigos[i])) {
                            inimigo(Inimigos[i]).vida.Diminuir(this.Dano);
                        }
                        i++;
                    }
                } else { // Dono do tiro é um inimigo
                    Tam = Inimigos.length;
                    i = 0;
                    while (i < Tam) {
                        if (this.hitTestObject(Inimigos[i])) {
                                jogador(Inimigos[i]).vida.Diminuir(this.Dano);
                            }
                            i++;
                        }
                    }
                    if (this.y <= 0) {
                        this.removeEventListener(Event.ENTER_FRAME, mover_tiro_cima);
                        meuPalco.removeChild(this);
                    }
                }

                public function mover_tiro_baixo(evt: Event) {
                    this.y += velo;
                    if (inimigoTipo == 0) { // Dono do tiro é o player          
                         Tam = Inimigos.length;
                         i = 0;
                        while (i < Tam) {
                            if (this.hitTestObject(Inimigos[i])) {
                                    inimigo(Inimigos[i]).vida.Diminuir(this.Dano);
                                }
                                i++;
                            }
                        } else { // Dono do tiro é um inimigo
                            Tam = Inimigos.length;
                            i = 0;
                            while (i < Tam) {
                                if (this.hitTestObject(Inimigos[i])) {
                                        jogador(Inimigos[i]).vida.Diminuir(this.Dano);
                                    }
                                    i++;
                                }
                            }
                            if (this.y <= 0) {
                                this.removeEventListener(Event.ENTER_FRAME, mover_tiro_baixo);
                                meuPalco.removeChild(this);
                            }
                        }

                        static function getClass(obj: Object): String {
                            return  String(Class(getDefinitionByName(getQualifiedClassName(obj))));
                        }
                    }

                }

The error happens everytime the laser tests to see if it's hitting an enemy (hittest) in its functions. mover_tiro_baixo() moves the shot down.

Thanks people!

Edit: The way I create the arrays:

var player1:jogador = new jogador(stage,350,700,10,3,1);
var Jogadores:Array = [jogador];
player1.setJogadores(Jogadores);

var inimigo1:et = new et(stage,100,200,Jogadores);
var inimigo2:et = new et(stage,200,100,Jogadores);
var inimigo3:et = new et(stage,350,450,Jogadores);

var todosInimigos:Array = [inimigo1,inimigo2,inimigo3];
player1.DefinirInimigos(todosInimigos);
1

1 Answers

1
votes

I've checked some other stack overflow questions that have similar type conversion errors. Most of the other people with a similar problem were actually filling their array with a Class, rather than objects that were instances of a Class. Are you filling those arrays like this?

for(var i:int = 0; i < 10; i++){
     Inimigos.push(jogador); //incorrect
}

If so, that is the reason the problem is happening. This is the correct way to do it:

for(var i:int = 0; i < 10; i++){
     Inimigos.push(new jogador()); //correct
}

EDIT:

In the new code you added to the first post, this line seems to be the problem:

var Jogadores:Array = [jogador]; //jogador is a class

Flash Actionscript Arrays cannot be "initialized" to only be able to contain a specific type of object. Actionscript Vectors are capable of that, but not Arrays. That line posted above initializes an array in which the first element is a class, not an object.