AS3使用静态类型调用可能未定义的方法类错误我无法修复

时间:2014-01-31 03:39:38

标签: actionscript-3

我正在关注游戏教程但做了一些修改。添加多个级别,我添加了实例,但这搞砸了更多,然后我可以解决。

每个等级点都可以制作,如果你死了,这些点应该在游戏屏幕上看到。

级别代码

    package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.media.SoundMixer;
import flash.media.Sound;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.net.URLLoader;


public class LevelEen extends MovieClip 
{
    public var gerardJoling : GerardJoling;
    public var geerTimer:Timer;
    public var geerGame : GeerGame;
    public var geer_score:GeerScore;
    public static var instance:LevelEen;

    public var gameOverScherm:GameOverScherm;
    public var levelEen:LevelEen;

    public var pattys:Array;        

    public var enemy:VrouwVijand;

    public var volumeAdjust:SoundTransform = new SoundTransform();
    public var channelNogniet:SoundChannel = new SoundChannel();
    public var channelScore:SoundChannel = new SoundChannel();

    public var Nogniet:URLRequest = new URLRequest("Sounds/Gerard Joling - Het is nog niet voorbij.mp3");
    public var Score:URLRequest = new URLRequest("Sounds/10046_1361186725.mp3");
    public var noMoreNogniet:Sound = new Sound(Nogniet);
    public var noMoreScore:Sound = new Sound(Score);

    public function LevelEen() 
    {
        SoundMixer.stopAll();
        volumeAdjust.volume = 1;
        channelNogniet = noMoreNogniet.play(0,10);
        channelNogniet.soundTransform = volumeAdjust;

        gerardJoling = new GerardJoling();
        addChild(gerardJoling);
        gerardJoling.x = mouseX;
        gerardJoling.y = mouseY;

        pattys = new Array();

        geerTimer = new Timer( 25 );
        geerTimer.addEventListener( TimerEvent.TIMER, geerInteractie);
        geerTimer.start();
    }

    public function geerInteractie( timerEvent:TimerEvent ):void 
    {
        if ( Math.random() < 0.04 )
        {
            var randomX:Number = Math.random() * 550;
            var vrouwVijand:VrouwVijand = new VrouwVijand( randomX, -40 );
            pattys.push(vrouwVijand);
            addChild(vrouwVijand);
            geer_score.addToValue( 0 );
        }

        gerardJoling.x = mouseX;
        gerardJoling.y = mouseY;

        var i:int = pattys.length - 1;
        var enemy:VrouwVijand;
        var finalScore:Number = getFinalScore();

        while ( i > -1 )
        {
            enemy = pattys[i];
            enemy.dalen();
            if ( gerardJoling.hitTestObject(enemy) ) 
            {
                geerTimer.stop();
                SoundMixer.stopAll();
                GameOverScherm.setFinalScore( finalScore );
                GeerGame.instance.switchScreen( "GameOverScherm" );
                trace (geer_score.currentValue);
            }

            if ( enemy.y > 400 )
            {
                removeChild( enemy );
                pattys.splice( i, 1 );
                geer_score.addToValue( 10 );
                channelScore = noMoreScore.play(0,1);
            }

            if (geer_score.currentValue > 40)
            {
                geerTimer.removeEventListener(TimerEvent.TIMER, geerInteractie);
                GeerGame.instance.switchScreen( "LevelTwee" );
            }

            i = i - 1;
        }

    }
    public function getFinalScore():Number
    {
        return geer_score.currentValue;
    }
}}

这里的分数是在另一个类别中计算的,但是setFinalScore en getFinalscore也必须在那里完成工作以获得最终分数并将其发送到gameoverscherm

    package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import flash.media.Sound;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.media.SoundMixer;


public class GameOverScherm extends MovieClip
{
    public var gameOverScherm:GameOverScherm;
    public var levelEen:LevelEen;
    public var volumeAdjust:SoundTransform = new SoundTransform();
    public var channelGeer:SoundChannel = new SoundChannel();

    public var Geer:URLRequest = new URLRequest("Sounds/Gerard Joling heeft er geen kracht meer voor1.mp3");
    public var noMoreGeer:Sound = new Sound(Geer);

    public var finalScore:TextField;

    public function GameOverScherm()
    {
        opnieuw_button.addEventListener( MouseEvent.CLICK, onClickOpnieuw );
        menu_button.addEventListener( MouseEvent.CLICK, onClickMenu );
        volumeAdjust.volume = 1;
        channelGeer = noMoreGeer.play(0,1);
        channelGeer.soundTransform = volumeAdjust;

    }

    public function onClickOpnieuw( mouseEvent:MouseEvent ):void
    {
        SoundMixer.stopAll();
        GeerGame.instance.switchScreen( "LevelEen" );
        gameOverScherm = null;
    }

    public function onClickMenu( mouseEvent:MouseEvent ):void
    {
        SoundMixer.stopAll();
        GeerGame.instance.switchScreen( "StartScherm" );
        gameOverScherm = null;
    }
    public function setFinalScore( scoreValue:Number ):void
    {
        finalScore.text = scoreValue.toString();
    }
}}

这是我得到的错误,我似乎无法弄清楚如何修复它并使最终得分出现在gameoverscreen上

C:\Users\Ian-w\Dropbox\game\GeerGame\LevelEen.as, Line 87   1061: Call to a possibly undefined method setFinalScore through a reference with static type Class.

我真的希望有人能在这里帮助我,已经坚持了几天

NEWWWW

进行了更改,现在如果我在GameOverScherm类中跟踪(scoreValue)函数setFinalScore,我得到一个具有正确分数的响应,但是

    public function setFinalScore( scoreValue:Number ):void
{
    finalScore.text = scoreValue.toString();
    trace(scoreValue)
}

在GameOverScherm屏幕中,动态tekst字段名称:finalScore不会更改值...

因为我无法发布图片所以我只发布了链接http://s29.postimg.org/jwxu6rlfr/Untitled.jpg

我做了什么冤屈?将分数推向现场后,我是否需要更新屏幕?

2 个答案:

答案 0 :(得分:0)

您的代码中存在多个错误,请使用此方法:

GameOverScherm.setFinalScore( finalScore );

您必须更改类“GameOverScherm”中的几项内容才能使用函数“setFinalScore”作为静态方法。我不认为你想要什么,我看到你有:

public var gameOverScherm:GameOverScherm;

这似乎是你想创造某种“单身模式”。如果是这样,你需要把这个变量设置为静态,那么“gameOverScherm”应该在你班级的某个地方设置为“gameOverScherm = this”。这样你就可以改变“LevelEen”中的行:

GameOverScherm.gameOverScherm.setFinalScore(finalScore);

根据您的代码(使用或不使用静态方法/变量),有很多方法可以解决这个问题,但是您的错误表明没有名为“setFinalScore”的静态函数。我建议你看一下静态词的含义以及使用它的方式/原因。

答案 1 :(得分:0)

您正试图从setFinalScore()访问GameOverScherm,就像它是公开可见的静态功能一样。由于该名称没有公开可用的功能,因此返回undefined。

Here很好地介绍了静态函数的工作原理以及何时使用它们。

您似乎在gameOverScherm中有一个LevelEen变量,但尚未创建GameOverScherm类的新实例。您可以使用此变量来实现您尝试执行的操作。您需要在适当的位置创建GameOverScherm的新实例,如下所示:

gameOverScherm = new GameOverScherm();

所以你可以像这样访问它的公共函数:

gameOverScherm.setFinalScore(finalScore);