防止 craftyJS 中的对象剪切
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在使用
在碰撞后将玩家移离蘑菇。但是,由于您仅沿 x 方向移动它,因此如果从顶部或底部碰撞,它将不起作用。这是你的问题吗?
如果您使用 Multiway 或 Fourway 组件,您可以这样做:
编辑:完整示例
运行于 < 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
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:
Edit: complete example
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 :-)