我才刚刚开始学习 Javascript,所以我在 EaselJs 中的动画遇到了一些麻烦。我试图在鼠标点击时移动对象(这没关系,正如我所期望的那样),但是当我尝试以正确的方式添加移动动画时,我的动画不起作用,只是在对象移动时停止在一帧. 这是 main.js 的代码和问题示例:http ://rustem.ucoz.com/onMouseRun/index.html
var canvas;
var stage;
var imgCharRun = new Image();
var imgCharIdle = new Image();
var bmpAnimation;
var canvasx;
var isClicked = false;
function init() {
canvas = document.getElementById('canvas');
canvasx = canvas.offsetLeft;
imgCharRun.src = "monsterRun.png";
imgCharIdle.src = "monsterIdle.png";
startGame();
}
function startGame(){
stage = new createjs.Stage(canvas);
stage.enableMouseOver(10);
var spriteSheet = new createjs.SpriteSheet({
images: [imgCharRun, imgCharIdle],
frames: {width: 64, height: 64, regX: 32, regY: 32},
animations: {
walk: [0, 9, "walk"],
idle: [10, 20, "idle"]
}
});
bmpAnimation = new createjs.BitmapAnimation(spriteSheet);
bmpAnimation.gotoAndPlay ("idle");
bmpAnimation.scaleX = 1;
bmpAnimation.x = canvas.width/2;
bmpAnimation.y = canvas.height-100;
bmpAnimation.currentFrame = 9;
stage.addChild(bmpAnimation);
createjs.Ticker.addListener(window);
createjs.Ticker.useRAF = true;
createjs.Ticker.setFPS(30);
}
document.onmousedown = function(e) {
isClicked = true;
window.mousex = e.clientX-canvasx;
}
function isMouseClicked() {
if (isClicked){
moveToMouse();
}
}
function moveToMouse() {
if(bmpAnimation.x<mousex) {
bmpAnimation.x+=5;
bmpAnimation.gotoAndPlay("walk");
bmpAnimation.scaleX = -1;
}
if(bmpAnimation.x>mousex) {
bmpAnimation.x-=5;
bmpAnimation.gotoAndPlay("walk");
bmpAnimation.scaleX = 1;
}
}
function tick() {
stage.update();
isMouseClicked();
}
请帮我解决这个问题: