0

我想创建一个角色的控件。不幸的是,我遇到了错误 #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()

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

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

这是主要课程:

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;
        }
      }
   }    
}
4

1 回答 1

1

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

parent当使用以小写 ( )开头的变量和方法命名以及以大写 ( ) 开头的类的通用编码标准时,可以避免此类问题Sprite。然后,当您看到MovieClip(ClassBoy)很明显存在错误时,因为ClassBoy是一种类。

其他几件事也可能对您有所帮助:

  • 如果您遵循以大写开头的类名的标准,则不要以 开头class,您可以将其命名Boy

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

于 2013-02-08T19:10:35.747 回答