首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Craftyjs动画未正确更改

Craftyjs动画未正确更改
EN

Stack Overflow用户
提问于 2012-11-02 05:20:42
回答 1查看 958关注 0票数 1

我在Github游戏关闭时使用了http://craftyjs.com/,我在动画方面遇到了一些麻烦。当我第一次启动动画时(当我在主场景中初始化播放器实体时),它就工作了。但是当我通过我的CustomControls组件设置动画时,它只播放动画的第一帧。

我认为问题出在CustomControls组件中,所以下面是代码:https://gist.github.com/3992392

如果你想克隆它并进行测试,下面是所有的代码:https://github.com/RylandAlmanza/dragons-suck

如果有人知道问题出在哪里,请让我知道。谢谢!

编辑:这是一个最小的jsfiddle示例,除了移动和应该是动画之外的所有东西都去掉了:http://jsfiddle.net/x7FDF/

代码语言:javascript
复制
var SPRITE_SIZE = 32;
Crafty.init();
Crafty.canvas.init();
Crafty.sprite(SPRITE_SIZE, "http://i.imgur.com/9sN9V.png", {
    player_east_1: [0, 0],
    player_east_2: [1, 0],
    player_east_3: [2, 0],
    player_west_1: [0, 1],
    player_west_2: [1, 1],
    player_west_3: [2, 1],
    player_south_1: [0, 2],
    player_south_2: [1, 2],
    player_south_3: [2, 2],
    player_north_1: [0, 3],
    player_north_2: [1, 3],
    player_north_3: [2, 3]
});

Crafty.scene("loading", function() {
    Crafty.background("#000");
    Crafty.e("2D, DOM, Text")
    .attr({
        w: 100,
        h: 20,
        x: 150,
        y: 120
    })
    .text("Loading")
    .css({"text-align": "center"});
    Crafty.load(["http://i.imgur.com/9sN9V.png"], function() {
        Crafty.scene("main");
    });
});

Crafty.scene("main", function() {
    Crafty.background("#000");

    var player = Crafty.e("2D, DOM, SpriteAnimation, player_east_1, Collision, TileCollision, CustomControls")
    .attr({
        x: 0,
        y: 0,
        x_velocity: 0,
        y_velocity: 0,
        w: SPRITE_SIZE,
        h: SPRITE_SIZE
    })
    .animate("stand_east", 0, 0, 0)
    .animate("stand_west", 0, 1, 0)
    .animate("stand_south", 0, 2, 0)
    .animate("stand_north", 0, 3, 0)
    .animate("walk_east", [[0, 0], [1, 0], [0, 0], [2, 0]])
    .animate("walk_west", [[0, 1], [1, 1], [0, 1], [2, 1]])
    .animate("walk_south", [[0, 2], [1, 2], [0, 2], [2, 2]])
    .animate("walk_north", [[0, 3], [1, 3], [0, 3], [2, 3]])
    .animate("stand_east", 45, -1)
    .CustomControls();
});


Crafty.c("CustomControls", {
    CustomControls: function() {
        this.bind("EnterFrame", function() {
            var up = Crafty.keydown[Crafty.keys.UP_ARROW];
            var down = Crafty.keydown[Crafty.keys.DOWN_ARROW];
            var left = Crafty.keydown[Crafty.keys.LEFT_ARROW];
            var right = Crafty.keydown[Crafty.keys.RIGHT_ARROW];
            if (up) {
                this.y_velocity = -2;
                if (!this.isPlaying("walk_north")) {
                    this.stop().animate("walk_north", 45, -1);
                }
            }
            if (down) {
                this.y_velocity = 2;
                if (!this.isPlaying("walk_south")) {
                    this.stop().animate("walk_south", 45, -1);
                }
            }
            if (left) {
                this.x_velocity = -2;
                if (!this.isPlaying("walk_west")) {
                    this.stop().animate("walk_west", 45, -1);
                }
            }
            if (right) {
                this.x_velocity = 2;
                if (!this.isPlaying("walk_east")) {
                    this.stop().animate("walk_east", 45, -1);
                }
            }
            if (!left && !right) {
                this.x_velocity = 0;
                this.stop();
            }
            if (!up && !down) {
                this.y_velocity = 0;
                this.stop();
            }
            this.x += this.x_velocity;
            this.y += this.y_velocity;
        });
    }
});

Crafty.scene("loading");​
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-02 22:08:07

"enterFrame"的末尾,你有:

代码语言:javascript
复制
if (!left && !right) {
    this.x_velocity = 0;
    this.stop();
}
if (!up && !down) {
    this.y_velocity = 0;
    this.stop();
}

因此,动画始终会停止,然后在下一帧从头开始重新开始。例如,当你的播放器向下移动时,(!left && !right)计算结果为true,动画就会停止。

一种解决方案是只有在所有4个方向都为假的情况下才停止动画。

代码语言:javascript
复制
if (!left && !right) {
    this.x_velocity = 0;
}
if (!up && !down) {
    this.y_velocity = 0;
}
if (!(up | down | left | right)) {
    this.stop();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13185644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档