将 Number 类型的值隐式强制转换为不相关的类型 flash.display:MovieClip

Posted

技术标签:

【中文标题】将 Number 类型的值隐式强制转换为不相关的类型 flash.display:MovieClip【英文标题】:Implicit coercion of a value of type Number to an unrelated type flash.display:MovieClip 【发布时间】:2016-09-24 05:17:16 【问题描述】:

当我尝试通过函数传递 MovieClip 时出现错误:

“”

房间生成类:

public var generatedRoom:Array;

public function enemyPack():void
    
        //trace(generatedRoom[createdRooms]);
        var random:int = Main.getRandom(0,5);

        switch(random)
        
            case 0:
                Main.spawnEnemy(roomX+Main.getRandom(50, room.width-50),generatedRoom[createdRooms],roomY+Main.getRandom(50, room.height-50));
                break;
        
    

敌人等级:

public function spawn(setX:int,setY:int, inRoom:MovieClip ,b:int = 0):void
    
        timeToSlow = Main.getRandom(500,1500)
        type = b;
        isDestroyed = false;
        switch(type)
        
            case 0:
                life = 100 + (Math.pow(Main.dific,4));

                maxLife = life;
                model = new MC_EnemigoD();
                bulletSpeed = Main.getRandom(3,6);
                timeToMove = 400;
                bulletModel = new MC_BulletE();
                speed = 1 + (Main.nivel)/2;
                aggro = 250;
                break;
            case 1:
                life = 125 + (Math.pow(Main.dific,4));
                maxLife = life;
                model = new MC_EnemigoC();
                speed = 0.5 /*+ (Main.nivel)/2*/;
                break;
            case 2:
                life = 500 + (Math.pow(Main.dific,4));
                maxLife = life;
                model = new MC_BossM();
                speed = 1 + (Main.nivel)/2;
                timeToMove = Main.getRandom(400,600);
                bulletModel = new MC_BulletC();
                bulletSpeed = Main.getRandom(6,9);
                timeToShoot = Main.getRandom(1500,2000);
                aggro = 400;
                break;
            case 3:
                life = 400 + (Math.pow(Main.dific,4));
                maxLife = life;
                model = new MC_Troll();
                speed = 0.6 /* + (Main.nivel)/2*/;
                break;
            case 4:
                life = 1000 + (Math.pow(Main.dific,4));
                maxLife = life;
                model = new MC_BossB();
                model.scaleX = 0.5;
                model.scaleY = 0.5;
                speed = 3 + (Main.nivel)/2;
                break;
        
        model.addChild(shadow);


        //Main.render.
        Main.render.addChild(model);
        model.gotoAndPlay("spawn")
        model.y = setY;
        model.x = setX;
        realHeight = model.height;
        realWidth = model.width;

        shadow.scaleX = model.scaleX + 0.1;
        shadow.scaleY = model.scaleY;
        shadow.x = (model.hitBox.width/2)+6-(shadow.width/2);
        shadow.y = (model.hitBox.height)-9;

        model.setChildIndex(shadow, 0);

        model.addChild(hpBar);
        hpBar.addChild(hpBarColor);
        hpBarColor.scaleY = 0.9;
        hpBarColor.scaleX = 0.97;
        hpBarColor.y = 1;
        hpBarColor.x = 1;
        hpBar.scaleY = 0.7;
        hpBar.x = (model.hitBox.width/2)+5-(hpBar.width/2);
        hpBar.y = (model.hitBox.height)+(hpBar.height);

        model.hitBox.visible = false;
    

主类:

public static function spawnEnemy(setX:int,setY:int, inRoom:MovieClip, t:int = 0):void
    
        //trace(setX,setY)
        var temp:Enemy = new Enemy();
        temp.spawn(setX,setY, inRoom, t);
        allEnemies.push(temp);
    

当我跟踪变量时,它返回 [object MC_"myMovieClip"]

【问题讨论】:

提示:不要在Main 上将所有内容都设为static 函数。数学效用函数应该是***函数或在包含类似效用功能的单独类中。生成一个敌人最终会改变一个对象的状态,它根本不应该是静态的。 编辑添加功能。敌人生成几乎是一次性代码,我想先让它工作。 现在添加发生错误的行和堆栈跟踪。 【参考方案1】:

如果我猜到了导致你的错误的那一行 看起来像你的spawnEnemy 电话

Main.spawnEnemy(roomX+Main.getRandom(50, room.width-50),//int
                generatedRoom[createdRooms],//a MovieClip?
                roomY+Main.getRandom(50, room.height-50)//int
                );//(int, MovieClip(?), int)

没有正确的参数顺序:

spawnEnemy(setX:int,
           setY:int, 
           inRoom:MovieClip, 
           t:int = 0)//(int, int, MovieClip)

【讨论】:

哦,非常感谢,是的,我以错误的顺序调用了参数。

以上是关于将 Number 类型的值隐式强制转换为不相关的类型 flash.display:MovieClip的主要内容,如果未能解决你的问题,请参考以下文章

1067:将 void 类型的值隐式强制转换为不相关的类型 Function

如何使用 LinqToSql 将字符串值隐式转换为枚举?

强制类型转换和隐式类型转换

java 强制类型转换的规则是啥?

JS强制类型转换,隐式类型转换, == 和===的区别

Js隐式转换