我正在尝试使用Canvas和Javascript为我正在制作的Windows 8应用程序制作一个基本的应用程序。在Visual Studio 2012 for Windows 8 Apps中编译代码后,我收到以下消息
0x800a1391 - JavaScript运行时错误:'app'未定义
我做错了什么以及我用什么代码来解决问题?
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,如果这意味着什么
答案 0 :(得分:0)
app
不是全局的,所以在上面显示的上下文中确实未定义。它在default.js
中的模块(匿名函数)中定义:
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var app = WinJS.Application;
那就是说,你在这段代码中想做什么? oncheckpoint将包含在暂停申请时保持状态的代码;添加一个事件监听器并调用start
在这种情况下并没有用。