我測試了你的代碼,經過一些調試後發現你的bug是什麼:你的圖片高度在spritesheet太高。您將142像素設置爲高度,但您的PNG圖像爲117像素。如果您更改它,圖像將顯示。看起來,如果你給一個精靈高度大於圖像,那麼createjs會崩潰。
順便說一句,你也有錯誤的位置:
Ticker.setFPS(60);
Ticker.addListener(stage);
您需要更改此爲
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener(stage);
我看到了很多的代碼,你應該改變:
反正我是你的代碼的一些refacto顯示播放,它與比較實際代碼明白,爲什麼我這樣做,並在你的遊戲開發的樂趣,HTML5和createjs是真的很有趣玩:d
var context;
var queue;
var WIDTH = 1024;
var HEIGHT = 768;
var mouseXPosition;
var mouseYPosition;
var batImage;
var stage;
var animation;
var crossHair;
window.onload = function()
{
/*
* Set up the Canvas with Size and height
*
*/
var canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
context.canvas.width = WIDTH;
context.canvas.height = HEIGHT;
stage = new createjs.Stage("myCanvas");
/*
* Set up the Asset Queue and load sounds
*
*/
queue = new createjs.LoadQueue(false);
queue.installPlugin(createjs.Sound);
queue.on("complete", queueLoaded, this);
createjs.Sound.alternateExtensions = ["ogg"];
queue.loadManifest([
{id: 'backgroundImage', src: 'assets/background.png'},
{id: 'crossHair', src: 'assets/crosshair.png'},
{id: 'shot', src: 'assets/shot.mp3'},
{id: 'background', src: 'assets/countryside.mp3'},
{id: 'batSpritesheet', src: 'assets/batSpritesheet.png'},
]);
queue.load();
}
function queueLoaded(event)
{
// Add background image
var backgroundImage = new createjs.Bitmap(queue.getResult("backgroundImage"))
stage.addChild(backgroundImage);
// Play background sound
createjs.Sound.play("background", {loop: -1});
// Create bat spritesheet
var spriteSheet = new createjs.SpriteSheet({
"images": [queue.getResult('batSpritesheet')],
"frames": {"width": 198, "height": 117},
"animations": { "flap": [0,4] }
});
// Create bat sprite
animation = new createjs.Sprite(spriteSheet, "flap");
animation.x = 100;
animation.y = 100;
animation.gotoAndPlay("flap");
stage.addChild(animation);
// Create crosshair
crossHair = new createjs.Bitmap(queue.getResult("crossHair"));
stage.addChild(crossHair);
// Add ticker
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener('tick', stage);
// Set up events AFTER the game is loaded
window.onmousemove = handleMouseMove;
window.onmousedown = handleMouseDown;
}
function handleMouseMove(event)
{
crossHair.x = event.clientX-45;
crossHair.y = event.clientY-45;
}
function handleMouseDown(event)
{
createjs.Sound.play("shot");
}
它總是那麼明顯的東西!我很欣賞重構。代碼是一團糟 - 所以我也很欣賞額外的提示。 –
很高興如果它是有用的,請不要忘記接受答案,請問:) –