防止 craftyJS 中的对象剪切

发布于 2025-01-02 23:06:21 字数 596 浏览 0 评论 0原文

在 CraftyJS 中,如何阻止我的玩家实体剪辑到其他实体中?

这是我的对象:

        Crafty.c("Mushroom", {
        init: function() {
            this.addComponent("collision");
            this.collision(new Crafty.polygon([[8,8],[24,8],[24,24],[8,24]]));
        }
    });

    var mushroom = Crafty.e("2D, canvas, mushroomRed, Mushroom")
    .attr({x: 200, y: 150, z:1, w: 32, h: 32});

这是我的播放器 onHit:

.onhit("mushroomRed", function() {
            this.x += this._speed;
            this.stop();
        }

只有当我从某个角度接近它时它才起作用,否则它就会失控。

建议?

In CraftyJS, how do i stop my player entity from clipping into other entities?

This is my object:

        Crafty.c("Mushroom", {
        init: function() {
            this.addComponent("collision");
            this.collision(new Crafty.polygon([[8,8],[24,8],[24,24],[8,24]]));
        }
    });

    var mushroom = Crafty.e("2D, canvas, mushroomRed, Mushroom")
    .attr({x: 200, y: 150, z:1, w: 32, h: 32});

and this is my player onHit:

.onhit("mushroomRed", function() {
            this.x += this._speed;
            this.stop();
        }

It works only when i approach it from certain angle, otherwise, it goes haywire.

Advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-01-09 23:06:21

看起来您正在使用

this.x += this._speed;

在碰撞后将玩家移离蘑菇。但是,由于您仅沿 x 方向移动它,因此如果从顶部或底部碰撞,它将不起作用。这是你的问题吗?

如果您使用 Multiway 或 Fourway 组件,您可以这样做:

.bind('Moved', function(from) {
    if(this.hit('mushroomRed')){
        this.attr({x: from.x, y:from.y});
    }
}).

编辑:完整示例

// Init Crafty:
Crafty.init();
Crafty.canvas.init();

var player = Crafty.e("2D, Canvas, Color, player, Multiway, Collision")
   .attr({x: 0, y: 0, w: 50, h: 50})
   .color("rgb(0,255,0)")
   .multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180})
   .bind('Moved', function(from) {
       if(this.hit('mushroomRed')){
           this.attr({x: from.x, y:from.y});
        }
    });

var mushroom = Crafty.e("2D, Canvas, mushroomRed, Color")
    .attr({x: 200, y: 150, z:1, w: 32, h: 32})
    .color("red");

运行于 < a href="http://jsfiddle.net/PzKVh/" rel="nofollow">http://jsfiddle.net/PzKVh/

这是使用最新版本的 Crafty 0.4.5。有一些重大更改和很多改进,所以我建议您使用这个版本。

另外,请随时在 https://groups.google.com/forum 的论坛中提问/#!forum/craftyjs 我认为您更有可能在那里找到帮助:-)

It looks like you are using

this.x += this._speed;

to move the player away from the mushroom after they have collided. But as you are only moving it in the x direction it wont work if you collide from top or bottom. Is that your problem?

If you use the Multiway or Fourway components you could do this instead:

.bind('Moved', function(from) {
    if(this.hit('mushroomRed')){
        this.attr({x: from.x, y:from.y});
    }
}).

Edit: complete example

// Init Crafty:
Crafty.init();
Crafty.canvas.init();

var player = Crafty.e("2D, Canvas, Color, player, Multiway, Collision")
   .attr({x: 0, y: 0, w: 50, h: 50})
   .color("rgb(0,255,0)")
   .multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180})
   .bind('Moved', function(from) {
       if(this.hit('mushroomRed')){
           this.attr({x: from.x, y:from.y});
        }
    });

var mushroom = Crafty.e("2D, Canvas, mushroomRed, Color")
    .attr({x: 200, y: 150, z:1, w: 32, h: 32})
    .color("red");

Running at http://jsfiddle.net/PzKVh/

This is using the latest version of Crafty 0.4.5. There has been a few breaking changes and a lot of improvements, so i would suggest you use this version.

Also, feel free to ask in the forums at https://groups.google.com/forum/#!forum/craftyjs I think you are much more likely to find help there :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文