我需要帮助,因为我想创建一个显示得分的游戏画面。但是,有一个错误阻止我将得分从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;
}
}
}
答案 0 :(得分:1)
关于您的最新一期(在屏幕上看到游戏中的得分为NaN),请查看游戏结束时您的代码正在执行的操作:
Main.showGameOver()
在Main.showGameOver()中:
NaN
)解决此问题的一种方法:
showGameOver()
方法,使其接受分数Main.showGameOver()
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;
}