4

我是 Metro 风格编程的新手,所以我在http://www.sitepoint.com/creating-a-simple-windows-8-game-with-javascript-game-basics-createjseaseljs/中学习使用 createJS 的游戏的教程. 这里的代码

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

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

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

这是javascript代码

// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
"use strict";

var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
WinJS.strictProcessing();


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

    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/Catapults/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 prepareGame() {
    bgImage = preload.getResult("screenImage").result;
    bgBitmap = new Bitmap(bgImage);
    bgBitmap.scaleX = SCALE_X;
    bgBitmap.scaleY = SCALE_Y;
    stage.addChild(bgBitmap);

    stage.update();
}

function gameLoop() {

}

function update() {

}

function draw() {

}

var canvas, context, stage;
var bgImage, p1Image, p2Image, ammoImage, p1lives, p2lives, title, endGameImage;
var bgBitmap, p1Bitmap, p2Bitmap, ammoBitmap;
var preload;

// Current Display Factor. Because the orignal assumed a 800x480 screen
var SCALE_X = window.innerWidth / 800;
var SCALE_Y = window.innerHeight / 480;
var MARGIN = 25;
var GROUND_Y = 390 * SCALE_Y;

app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.launch) {
        if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
            // TODO: This application has been newly launched. Initialize
            // your application here.
        } else {
            // TODO: This application has been reactivated from suspension.
            // Restore application state here.
        }
        args.setPromise(WinJS.UI.processAll());
    }
};

app.oncheckpoint = function (args) {
    // TODO: This application is about to be suspended. Save any state
    // that needs to persist across suspensions here. You might use the
    // WinJS.Application.sessionState object, which is automatically
    // saved and restored across suspension. If you need to complete an
    // asynchronous operation before your application is suspended, call
    // args.setPromise().
};

document.addEventListener("DOMContentLoaded", initialize, false);

app.start();
})();

现在的问题是,每当我尝试编译代码时,它都会给我 Preload is undefined 错误。我不明白我将脚本包含在 HTML 中并在我的项目中包含文件的原因。有人请帮助这个问题在过去的一个小时里一直在杀死我,我只想制作一个简单的游戏。

4

3 回答 3

3

原因是这些库已放置在“createjs”命名空间下。为了实例化 PreloadJS 对象,请执行以下操作:

var preload = new createjs.PreloadJS();

那应该让你回到快乐的道路上。

于 2012-10-26T22:40:25.387 回答
2

(在 PreloadJS 0.3.0 发布后于 2013 年 2 月尝试)

我刚刚使用最新版本的 EaselJS 尝试了来自 sitepoint.com 的教程(http://www.sitepoint.com/creating-a-simple-windows-8-game-with-javascript-game-basics-createjseaseljs/)和 PreloadJS,但一开始我无法让代码在没有错误的情况下运行。

新版 PreloadJS (v0.3.0) 已将旧的 PreloadJS 类重命名为 LoadQueue。

请参阅以下链接中的文档:

但是在使用 v0.3.0 的同时简单地使用新类并不能修复项目。较新的版本有额外的不兼容性(例如 image.width 无效),可能可以通过反复试验来修复。

我必须下载旧的 PreloadJS v0.2.0:

简而言之:如果我使用 SitePoint 教程,我必须坚持使用 PreloadJS v0.2.0 并使用 createjs 命名空间。

注意:之前的回复中提到的示例项目也不起作用,因为它的 WinJS 引用无效。手动添加引用似乎也不起作用。

于 2013-03-01T04:17:12.290 回答
0

Move your default.js in the HTML to be after the preloadjs & easel code.

This is because the order of inclusion is important to JavaScript

于 2012-10-21T16:18:20.390 回答