ActionScript 3的错误输出#1034类型强制失败

时间:2013-02-08 18:23:34

标签: actionscript-3 oop type-coercion

我想创建一个角色的控件。不幸的是,我遇到了错误#1304。

这是输出错误:

TypeError: Error #1034: Type Coercion failed: cannot convert class_Boy$ to 
flash.display.MovieClip.
    at class_Boy/mainJump()
    at class_Boy/class_Boy_Move()

闪光灯可以运行但是当我按下空格键时,错误会不断重复并且闪光灯停止。

我认为罪魁祸首是由class_Boy类下的MovieClip(boy_Class)引起的。如果您能找到任何解决方案,请帮助我。感谢。

这是主要课程:

package 
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.Timer;
import flash.text.*;

public class experimentingMain extends MovieClip 
{
    var count:Number = 0;
    var myTimer:Timer = new Timer(10,count);

    var classBoy:class_Boy;

    var leftKey, rightKey, spaceKey, stopAnimation:Boolean;

    public function experimentingMain() 
    {
        myTimer.addEventListener(TimerEvent.TIMER, scoreUp);
        myTimer.start();

        classBoy = new class_Boy();
        addChild(classBoy);

        stage.addEventListener(KeyboardEvent.KEY_DOWN, pressTheDamnKey);
        stage.addEventListener(KeyboardEvent.KEY_UP, liftTheDamnKey);
    }

    public function pressTheDamnKey(event:KeyboardEvent):void
    {
        if (event.keyCode == 37)
        {
            leftKey = true;
            stopAnimation = false;
        }

        if (event.keyCode == 39)
        {
            rightKey = true;
            stopAnimation = false;
        }

        if (event.keyCode == 32)
        {
            spaceKey = true;
            stopAnimation = true;
        }
    }

    public function liftTheDamnKey(event:KeyboardEvent):void
    {
        if (event.keyCode == 37)
        {
            leftKey = false;
            stopAnimation = true;
        }

        if (event.keyCode == 39)
        {
            rightKey = false;
            stopAnimation = true;
        }

        if (event.keyCode == 32)
        {
            spaceKey = false;
            stopAnimation = true;
        }
    }

    public function scoreUp(event:TimerEvent):void 
    {
        scoreSystem.text = String("Score : "+myTimer.currentCount);
    }

  }
}

这是class_Boy的类:

package 
{
import flash.display.*;
import flash.events.*;

public class class_Boy extends MovieClip
{
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    var mainSpeed:Number = 5;
    var mainJumping:Boolean = false;
    var jumpSpeedLimit:int = 40;
    var jumpSpeed:Number = 0;

    var theCharacter:MovieClip;

    var currentX, currentY:int;

    public function class_Boy()
    {
        this.x = 600;
        this.y = 500;

        addEventListener(Event.ENTER_FRAME, class_Boy_Move);
    }

    public function class_Boy_Move(event:Event):void 
    {
        currentX = this.x;

        if (MovieClip(parent).leftKey)
        {
            currentX += mainSpeed;

        }

        if (MovieClip(parent).rightKey)
        {
            currentX -= mainSpeed;
        }

        if (MovieClip(parent).spaceKey)
        {
            mainJump();
        }

        this.x = currentX;
        this.y = currentY;
    }

    public function mainJump():void
    {
        currentY = this.y;

        if(!mainJumping)
        {
            mainJumping = true;
            jumpSpeed = jumpSpeedLimit*-1;
            currentY += jumpSpeed;
        } else {
            if(jumpSpeed < 0){
                jumpSpeed *= 1 - jumpSpeedLimit/250;
            if(jumpSpeed > -jumpSpeedLimit/12){
                jumpSpeed *= -2;
                }
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
        {
            jumpSpeed *= 1 + jumpSpeedLimit/120;
        }
        currentY += jumpSpeed;
        if(currentY >= stage.stageHeight - MovieClip(class_Boy).height)
        {
            mainJumping = false;
            currentY = stage.stageHeight - MovieClip(class_Boy).height;
        }
      }
   }    
}

1 个答案:

答案 0 :(得分:1)

而不是MovieClip(class_Boy),它应该只是this,它是引用运行代码的当前实例的对象。 class_Boy是类本身,用于实例化新对象。

使用以小写(parent)开头的命名变量和方法的常用编码标准以及使用大写(Sprite)的类时,会阻止此类问题。然后,当您看到MovieClip(ClassBoy)时,很明显会出现错误,因为ClassBoy是一种类型。

其他一些可能对您有帮助的事情:

  • 如果您遵循以大写字母启动类名称的标准,则不要使用class启动它,您只需将其命名为Boy

  • 方法不必以类名(class_Boy_Move)开头。事实上,如果他们不这样做会更好,所以你不要与构造函数混淆,最后会缩短