简体   繁体   English

未捕获的类型错误:不是函数

[英]Uncaught Type error: is not a function

I am making a javascript game, using Canvas. 我正在使用Canvas做一个JavaScript游戏。 It works well superficially, but "Uncaught TypeError: game_state.Update is not a function" keeps going. 从表面上看,它运行良好,但是“ Uncaught TypeError:game_state.Update不是函数”一直存在。 I cannot know the reason for all day...How can I solve the problem? 我一整天都不知道原因...如何解决这个问题?

error image1 , error image2 错误image1错误image2

Suspected files are below. 可疑文件如下。

gfw.js gfw.js

function onGameInit()
{
    document.title = "Lion Travel";

    GAME_FPS = 30;
    debugSystem.debugMode = true;

    //resourcePreLoading
    resourcePreLoader.AddImage("/.c9/img/title_background.png");
    resourcePreLoader.AddImage("/.c9/img/title_start_off.png");
    resourcePreLoader.AddImage("/.c9/img/title_start_on.png");
    resourcePreLoader.AddImage("/.c9/img/title_ranking_off.png");
    resourcePreLoader.AddImage("/.c9/img/title_ranking_on.png");
    resourcePreLoader.AddImage("/.c9/img/game_background_sky.png");

    soundSystem.AddSound("/.c9/background.mp3", 1);

    after_loading_state = new TitleState();
    game_state = TitleState;

    setInterval(gameLoop, 1000 / GAME_FPS);
}

window.addEventListener("load", onGameInit, false);

GameFramework.js GameFramework.js

window.addEventListener("mousedown", onMouseDown, false);
window.addEventListener("mouseup", onMouseUp, false);

var GAME_FPS;
var game_state;

function onMouseDown(e) 
{
    if(game_state.onMouseDown != undefined)
        game_state.onMouseDown(e);

    // alert("x:" + inputSystem.mouseX + " y:" + inputSystem.mouseY);
}

function onMouseUp(e)
{
    if(game_state.onMouseUp != undefined)
        game_state.onMouseUp(e);
}

function ChangeGameState(nextGameState)
{
    if(nextGameState.Init == undefined)
        return;

    if(nextGameState.Update == undefined)
        return;

    if(nextGameState.Render == undefined)
        return;

    game_state = nextGameState;
    game_state.Init();
}

function GameUpdate() 
{
    timerSystem.Update();
    **game_state.Update();**
    debugSystem.UseDebugMode();
}

function GameRender()
{
    var theCanvas = document.getElementById("GameCanvas");
    var Context = theCanvas.getContext("2d");

    game_state.Render();

    if(debugSystem.debugMode)
    {
        Context.fillStyle = "#ffffff";
        Context.font = '15px Arial';
        Context.textBaseline = "top";
        Context.fillText("fps: "+ frameCounter.Lastfps, 10, 10);
    }
}

function gameLoop()
{
    game_state = after_loading_state;

    GameUpdate();
    GameRender();

    frameCounter.countFrame();
}

RS_Title.js RS_Title.js

function TitleState() 
{
    this.imgBackground = resourcePreLoader.GetImage("/.c9/img/title_background.png");
    this.imgButtonStartOff = resourcePreLoader.GetImage("/.c9/img/title_start_off.png");
    this.imgButtonStartOn  = resourcePreLoader.GetImage("/.c9/img/title_start_on.png");
    this.imgButtonRankingOff = resourcePreLoader.GetImage("/.c9/img/title_ranking_off.png");
    this.imgButtonRankingOn  = resourcePreLoader.GetImage("/.c9/img/title_ranking_on.png");

    soundSystem.PlayBackgroundMusic("/.c9/background.mp3");

    return this; 
}

TitleState.prototype.Init = function()
{
    soundSystem.PlayBackgroundMusic("/.c9/background.mp3");
};

TitleState.prototype.Render = function()
{
    var theCanvas = document.getElementById("GameCanvas");
    var Context = theCanvas.getContext("2d");
    Context.drawImage(this.imgBackground, 0, 0);

    //drawing button
    if(inputSystem.mouseX > 170 && inputSystem.mouseX < 170+220 
    && inputSystem.mouseY > 480 && inputSystem.mouseY < 480+100)
    {
        Context.drawImage(this.imgButtonStartOn, 170, 480);
        this.flagButtonStart = true;
    }
    else
    {
        Context.drawImage(this.imgButtonStartOff, 170, 480);
        this.flagButtonStart = false;
    }

    if(inputSystem.mouseX > 420 && inputSystem.mouseX < 420+220 
    && inputSystem.mouseY > 480 && inputSystem.mouseY < 480+100)
    {
        Context.drawImage(this.imgButtonRankingOn, 420, 480);
        this.flagButtonRanking = true;
    }
    else
    {
        Context.drawImage(this.imgButtonRankingOff, 420, 480);
        this.flagButtonRanking = false;
    }
};

TitleState.prototype.Update = function()
{

};

TitleState.prototype.onMouseDown = function()
{
    if(this.flagButtonStart)
        ChangeGameState(new PlayGameState());
        after_loading_state = PlayGameState;
        game_state = PlayGameState;

    if(this.flagButtonRanking)
        ChangeGameState();
};

RS_PlayGame.js RS_PlayGame.js

function PlayGameState()
{
    this.imgBackgroundSky = resourcePreLoader.GetImage("/.c9/img/game_background_sky.png");
}

PlayGameState.prototype.Init = function()
{
    var theCanvas = document.getElementById("GameCanvas");
    var Context = theCanvas.getContext("2d");
    Context.drawImage(this.imgBackgroundSky, 0, 0);
};

PlayGameState.prototype.Render = function()
{
    var theCanvas = document.getElementById("GameCanvas");
    var Context = theCanvas.getContext("2d");
    Context.drawImage(this.imgBackgroundSky, 0, 0);
};

PlayGameState.prototype.Update = function()
{
    var theCanvas = document.getElementById("GameCanvas");
    var Context = theCanvas.getContext("2d");
    Context.drawImage(this.imgBackgroundSky, 0, 0);
};

As mentioned by the others, in the onMouseDown method you are assigning after_loading_state and game_state to PlayGameState which is a function and not an object. 正如其他人所提到的, onMouseDown方法,则需要分配after_loading_stategame_statePlayGameState这是一个功能,而不是一个对象。 So later on when you want to access the Update method, it simply doesn't exist, because it is defined over the object prototype and not the function. 因此,稍后要访问Update方法时,它根本不存在,因为它是在对象原型而非函数上定义的。 You might want to do something like this so that you also avoid instantiating (calling) PlayGameState multiple times: 您可能需要执行以下操作,以便避免多次实例化(调用) PlayGameState

game_state = new PlayGameState();
ChangeGameState(game_state);
after_loading_state = game_state;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM