문제

I need help because I want to create a gameover screen that display score. However, there's an error that prevent me from transferring the score from theplayclass.as to thegameoverclass.as. Are there ways to pass a value to another movieclip without causing any errors.

I refer the source code from this website : http://www.emanueleferonato.com/2008/12/17/designing-the-structure-of-a-flash-game-as3-version/

Here's the error

C:\Users\xxx\Downloads\Migrate\test\theplayclass.as, Line 54, Column 41 1067: Implicit coercion of a value of type theplayclass to an unrelated type 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;
    }

    }

  }
도움이 되었습니까?

해결책

Regarding your latest issue (seeing NaN for the score on the game over screen), look at what your code is doing when the game ends:

  • the Player class calls Main.showGameOver()
  • Player class makes a new game over screen
  • Player class sets the final score on the new game over screen

In Main.showGameOver():

  • you make a new game over screen and add it to the stage, but this is now a different game over screen than the one in your Player class
  • this new game over screen never had the final score set (and therefore the score is NaN)

One way to address this:

  • Remove all references to the game over screen from your Player class
  • Modify the showGameOver() method in main so that it accepts the score
  • Modify the Player class so that is passes the final score using Main.showGameOver()
  • Modify the showGameOver() method in Main so that it sets the score on game over screen

Your modified code might look like this:

Player class:

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

Main class:

public function showGameOver(finalScore:String)
{
    gameOverClass = new thegameoverclass(this);
    gameOverClass.setScoreText(finalScore);
    addChild(gameOverClass);
    removeChild(playClass);
    playClass = null;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top