将事件绑定到狡猾.js中的实体

Binding events to entities in crafty.js

本文关键字:js 实体 事件 绑定      更新时间:2023-09-26

我目前正在尝试学习如何使用狡猾.js来创建游戏。现在正方形精灵被绘制出来了,但我无法让它做任何事情。这是我的游戏代码:

Game = {
  // Initialize our game
  start: function() {
    // Start crafty and set a background color
    Crafty.init(640, 480);
    Crafty.background('green');
    Crafty.sprite(16, "assets/square.png", {square:[0,0]});
    Crafty.c('SquareControls', {
             init: function() {
               this.bind('enterframe', function() {
                 this.x = this.x+2;
               });
             return this;
           }
         });
    //// SCENES ////
    Crafty.scene("main", function() {
      var square = Crafty.e("2D, Canvas, square, SquareControls")
                         .attr({x:32, y:32, width:16, height:16});
    });
    Crafty.scene("main");
  } // end start()
} // end Game class

事件名称区分大小写。您拼错了 EnterFrame 事件。

this.bind('EnterFrame', function() {
    this.x = this.x+2;
});