Question

I want to create a character's controls. Unfortunately, I have encounter an error #1304.

Here's the output error :

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

The flash can run but when I pressed Space bar, the error keeps repeating and the flash stopped.

I believe that the culprit is caused by MovieClip(boy_Class) under the class_Boy's class. Please help me if you can find any solution. Thanks.

This is the main 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);
    }

  }
}

This is the class of 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;
        }
      }
   }    
}
Was it helpful?

Solution

Instead of MovieClip(class_Boy), it should have been simply this, which is the object that refers to the current instance running the code. class_Boy is the class itself, used to instantiate new objects.

This kind of problem is prevented when using the common coding standard of naming variables and methods starting with lowercase (parent) and classes with uppercase (Sprite). Then when you see MovieClip(ClassBoy) it's clear there's an error because ClassBoy is a type of class.

A couple other things that may also help you:

  • if you follow the standard of starting class names with uppercase, you don't start it with class, and you can just name it Boy

  • methods don't have to start with the class name (class_Boy_Move). In fact it's better if they don't, so you don't confuse with the constructor and they end up shorter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top