0

我正在尝试使用 Canvas 和 Javascript 为我正在制作的 Windows 8 应用程序制作一个有点基本的应用程序。在为 Windows 8 应用程序编译 Visual Studio 2012 中的代码后,我收到以下消息

0x800a1391 - JavaScript 运行时错误:“应用程序”未定义

我做错了什么,我用什么代码来解决这个问题?

HTML

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Untitled</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <!-- references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
    <!-- CreateJS -->
    <script src="js/createJS/easeljs-0.5.0.min.js" type="text/javascript"></script>
    <script src="js/createJS/preloadjs-0.2.0.min.js" type="text/javascript"></script>
</head>
<body>
    <canvas id="gameCanvas"></canvas>
</body>
</html>

JS

var canvas, content, stage;
var bgimage, b1Image, p2Image, ammoImage, p1Lives, p2Lives, title, endGameImage;
var bgbitmap, p1bitmap, p2bitmap, ammoBitmap;
var preload;

// Scale Factor
var SCALE_X = window.innerWidth / 800;
var SCALE_Y = window.innerHeight / 480;
var MARGIN = 25;
var GROUND_Y = 390 * SCALE_Y;


function initialize() {
    canvas = document.getElementById("gameCanvas");
    canvas.width = window.innWidth
    canvas.height = window.innerHeight;
    context = canvas.getContext("2d");

    function prepareGame() {
        bgimage = preload.getResult("screenImage").result;
        bgbitmap = new Bitmap(bgimage);
        bgbitmap.scaleX = SCALE_X
        bgbitmap.scaleY = SCALE_Y
        stage.addChild(bgBitmap);

        stage.update();
    }

    //PreloadJS
    preload = new PreloadJS();
    preload.onComplete = prepareGame;
    var manifest = [
    { id: "screenImage", src: "images/Backgrounds/gameplay_screen.png" },
    { id: "redImage", src: "images/Catapults/Red/redIdle/redIdle.png" },
{ id: "blueImage", src: "images/Catapluts/Blue/blueIdle/blueIdle.png" },
{ id: "ammoImage", src: "images/Ammo/rock_ammo.png" },
{ id: "winImage", src: "images/Backgrounds/victory.png" },
{ id: "loseImage", src: "images/Backgrounds/defeat.png"  },
{ id: "blueFire", src: "images/Catapults/Blue/blueFire/blueCatapult_fire.png" },
{ id: "redFire", src: "images/catapults/Red/redFire/redCatapult_fire.png" }
];
preload.loadManifest(manifest);

stage = new Stage(canvas);
}

function gameLoop() {
    update();
    draw();
}

function update() {

}

function draw() {

}

app.oncheckpoint = function  (args) { 
    document.addEventListener("DOMContentLoaded", initialize, false);
    app.start();
};

另外,如果这意味着什么,我正在使用 CreateJS

4

1 回答 1

0

app不是全局的,因此在您上面显示的上下文中确实是未定义的。它在一个模块(匿名函数)中定义default.js

(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;

也就是说,你想在这段代码中做什么? oncheckpoint将包含在您的应用程序暂停时保持状态的代码;在这种情况下,添加事件侦听器和调用start并没有真正意义。

于 2012-10-30T19:20:29.873 回答