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

时间:2013-06-30 04:16:55

标签: actionscript-3 oop reference pass-by-reference coercion

我需要帮助,因为我想创建一个显示得分的游戏画面。但是,有一个错误阻止我将得分从theplayclass.as转移到thegameoverclass.as。有没有办法将值传递给另一个动画片段而不会导致任何错误。

我参考了这个网站的源代码:http://www.emanueleferonato.com/2008/12/17/designing-the-structure-of-a-flash-game-as3-version/

这是错误

  

C:\ Users \ xxx \ Downloads \ Migrate \ test \ theplayclass.as,第54行,第41栏1067:将类型的值隐式强制转换为不相关的类型main。

main.as

 package  
 {

 import flash.display.MovieClip;
 import flash.events.Event;

public class main extends MovieClip
{
    public var playClass:theplayclass;
    public var gameOverClass:thegameoverclass;

    public function main() 
    {
        showWin();

    }

    public function showWin()
    {           
        playClass = new theplayclass(this);
        addChild(playClass);            
    }

    public function showGameOver()
    {
        gameOverClass = new thegameoverclass(this);
        addChild(gameOverClass);

        removeChild(playClass);
        playClass = null;
    }
    }
}

theplayclass.as

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

public class theplayclass extends MovieClip 
{
    private var mainClass:main;
    var gameScore:Number;
    var gameOverScore:thegameoverclass;

    public function theplayclass(passedClass:main)
    {           
        mainClass = passedClass;

        scoreText.text ="0";

        gameScore = 0;



        win.addEventListener(MouseEvent.CLICK, showwinFunction);
        next.addEventListener(MouseEvent.CLICK, showgameoverFunction);

        addEventListener(Event.ADDED_TO_STAGE, addToStage);
        addEventListener(Event.ENTER_FRAME, changeScore);

    }

    public function addToStage(e:Event):void
    {
        this.x = 0;
        this.y = 0;
    }

    private function showwinFunction(e:MouseEvent):void
    {
        gameScore+=50;
    }

    private function changeScore(e:Event):void
    {
        scoreText.text =""+gameScore;
    }


    public function showgameoverFunction(e:MouseEvent)
    {
        mainClass.showGameOver();

        gameOverScore = new thegameoverclass(this);
        gameOverScore.setTextScore(gameScore);

    }

    }
 }

thegameoverclass.as

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

public class thegameoverclass extends MovieClip 
{
    var mainClass:main;
    var scorePoints:Number;

    public function thegameoverclass(passedClass:main) 
    {
        mainClass = passedClass;
        finalScore.text = "test";
    }

    public function setTextScore(textToSet:Number)
    {
        finalScore.text = ""+scorePoints;
    }

    }

  }

1 个答案:

答案 0 :(得分:1)

关于您的最新一期(在屏幕上看到游戏中的得分为NaN),请查看游戏结束时您的代码正在执行的操作:

  • Player类调用Main.showGameOver()
  • 玩家类通过屏幕制作新游戏
  • 玩家类通过屏幕设置新游戏的最终得分

在Main.showGameOver()中:

  • 你在屏幕上制作一个新游戏并将其添加到舞台上,但现在屏幕上的游戏与播放器类中的游戏不同
  • 这个屏幕上的新游戏从未有过最终得分(因此得分为NaN

解决此问题的一种方法:

  • 从您的播放器类中删除所有对游戏的引用
  • 修改main中的showGameOver()方法,使其接受分数
  • 修改Player类,以便使用Main.showGameOver()
  • 传递最终得分
  • 修改Main中的showGameOver()方法,以便在屏幕上设置游戏分数

您修改的代码可能如下所示:

玩家类:

public function showgameoverFunction(e:MouseEvent)
{
    mainClass.showGameOver(gameScore.toString());
}

主要课程:

public function showGameOver(finalScore:String)
{
    gameOverClass = new thegameoverclass(this);
    gameOverClass.setScoreText(finalScore);
    addChild(gameOverClass);
    removeChild(playClass);
    playClass = null;
}