캐릭터의 컨트롤을 만들고 싶습니다. 불행히도 # 1304 오류가 발생했습니다.ActionScript 3의 오류 출력 # 1034 유형 강제 변환이 실패했습니다.
다음은 출력 오류입니다 :
이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)
으로 인해 발생한다고 생각합니다. 어떤 해결책을 찾을 수 있으면 도와주세요. 감사.
이 메인 클래스입니다 : 이것은 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);
}
}
}
대신 MovieClip(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;
}
}
}
}
MovieClip으로 변환 할 필요가 없습니다. 부모 클래스에있는 class_Boy 클래스의 인스턴스이기 때문에'this' 만 사용할 수 있습니다. 또한,'class_Boy'는 클래스의 이름이므로 어떤 것에 던져 넣는 것은 의미가 없습니다. – inhan
고마워요! – Minelava
문제 없습니다. 또한 사용자가 혼동을 피하기 위해 제안한대로 적절한 명명 규칙을 적용 할 수 있습니다 (기본 설정 문제 임). – inhan