2012-09-07 83 views
2

嘿家伙不完全确定我做错了什么。我玩过几款HTML5游戏,而且他们似乎也遇到了不同的问题。图画滞后于运动,看起来很奇怪。这似乎并非如此。Laggy运动与帆布游戏

在我的游戏中,绘画看起来不错,但是在他移动时它每秒都落后(移动是方向键)。它也没有箭头键,如果我让他自动移动,所以我不认为这是一个关键的检测问题。

这几乎就好像垃圾收集器每秒都在运行一样。尽管如此,我并不认为我会喷出许多物体。

我使用的是Chrome浏览器21(MacOSX的),也火狐14

http://tempdrew.dreamhosters.com/spine/

这里是JS拨弄相关的代码。

http://jsfiddle.net/ju3ag/

这是在Chrome金丝雀罚款。我不知道这是不是因为javascript在标准铬合金中的速度非常快。这在最新的Firefox中很糟糕。我不确定我做错了什么。我正在根据时间更新运动。如果我把它拿出来,尽管它仍然不好。

我只是想知道是否有什么会突出给任何人。谢谢你的帮助。

sg = Class.extend({ 


}); 

sg.entities = []; 
sg.buttonStates = []; 

sg.createEntity = function (entity) {  
    this.entities.push(entity);  
}; 


window.requestAnimFrame = (function() { 
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || 
    function (callback, element) { 
     window.setTimeout(callback, 1000/60); 
    }; 

})(); 


(function defineUtil() { 

    sg.util = {}; 

    sg.util.getRandomInt = function getRandomInt(min, max) { 
     return Math.floor(Math.random() * (max - min + 1)) + min; 
    }; 

    sg.util.getRandomNumber = function getRandomNumber(min, max) { 
     return Math.random() * (max - min) + min; 
    }; 

})(); 


/*************************/ 
(function createEntity() { 

    var Entity = Class.extend({ 

     init: function (x, y) { 

      this.name = 'Entity'; 
      this.health = 100; 

      this.pos = { 
       x: x, 
       y: y 
      }; 

      this.vel = { 
       x: 0, 
       y: 0 
      }; 

      this.accel = { 
       x: 0, 
       y: 0 
      } 

      console.log(this.name + ' created ' + x + ' ' + y); 
     }, 

     update: function (elapsed) { 

     }, 

     draw: function (ctx) { 

     } 

    }); 

    sg.Entity = Entity; 

})(); 





/************************/ 

// -- player.js 
(function createPlayer() { 

    var Player = sg.Entity.extend({ 

     x: 0, 
     y: 0, 
     moveLeft: false, 
     moveRight: false, 
     speed : 5, 

     init: function (x, y) { 

      this.x = x; 
      this.y = y; 
      this.name = 'Player'; 
     }, 

     draw: function (ctx) { 

      var x = this.x, 
       y = this.y; 

      ctx.beginPath(); 
      ctx.rect(x, y, 40, 50); 
      ctx.fillStyle = 'white'; 
      ctx.fill(); 
      ctx.lineWidth = .5; 
      ctx.strokeStyle = 'rgba(0,0,0,.3)'; 
      ctx.stroke(); 
      ctx.fillStyle = 'rgba(0,0,0,.5)'; 
      ctx.fillRect(x + 25, y + 15, 5, 5); 
     }, 

     update: function (elapsed) { 
      var distance = (60/1000) * elapsed; 
      if (this.moveLeft) { 
       this.x += this.speed * distance; 
      } else if (this.moveRight) { 
       this.x -= this.speed * distance; 
      } 
     }, 

     keyDown: function (e) { 

      if (e.keyCode === 39) { 
       this.moveLeft = true; 
      } else if (e.keyCode === 37) { 
       this.moveRight = true; 
      } else { 
       this.moveLeft = false; 
       this.moveRight = false; 
      } 
     }, 

     keyUp: function (e) { 

      if (e.keyCode === 39) { 
       this.moveLeft = false; 
      } else if (e.keyCode === 37) { 
       this.moveRight = false; 
      } 
     } 

    }); 


    sg.Player = Player; 

})(); 




/**********************************/ 


(function createGame() { 

    var Game = Class.extend({ 

     canvas: null, 
     context: null, 
     width: null, 
     height: null, 

     init: function (width, height) { 

      this.canvas = document.getElementById('canvas'); 
      this.context = this.canvas.getContext('2d'); 

      this.width = width || 800; 
      this.height = height || 600; 

      this.canvas.width = this.width; 
      this.canvas.height = this.height; 
     }, 

     clear: function() { 
      this.context.clearRect(0, 0, this.width, this.height); 
     }, 

     draw: function() { 

      this.clear();  

      for (var i = 0; i < sg.entities.length; i++) { 
       sg.entities[i].draw(this.context); 
      } 
     }, 

     update: function (elapsed) { 

      for (var i = 0; i < sg.entities.length; i++) { 
       sg.entities[i].update(elapsed); 
      } 
     }, 

     keyDown: function (e) { 

      for (var i = 0; i < sg.entities.length; i++) { 

       if (typeof sg.entities[i].keyDown === 'function') { 
        sg.entities[i].keyDown(e); 
       } 
      } 
     }, 


     keyUp: function (e) { 

      for (var i = 0; i < sg.entities.length; i++) { 
       if (typeof sg.entities[i].keyUp === 'function') { 
        sg.entities[i].keyUp(e); 
       } 
      } 
     } 

    }); 

    sg.Game = Game; 

    var game = sg.currentGame = new sg.Game(800, 600); 


    var player = new sg.Player(200, 459); 

    sg.createEntity(player); 

    function update(elapsed) { 

     game.update(elapsed); 
    } 

    var lastUpdate = Date.now(); 

    function draw() { 

     var now = Date.now(); 
     var elapsed = (now - lastUpdate); 
     lastUpdate = now; 

     game.draw(); 
     update(elapsed); 
     requestAnimFrame(draw); 
    } 

    window.addEventListener('keydown', sg.currentGame.keyDown, false); 
    window.addEventListener('keyup', sg.currentGame.keyUp, false); 

    draw(); 

})(); 
+1

您是否尝试过在Chrome调试工具剖析内存?我想一个失控的内存泄漏可能比你需要更频繁地触发GC。 –

+0

刚刚添加的是,它在最新的Chrome和Firefox(Windows 7)上对我来说绝对正常运行 –

+0

前几天我做了一个CPU配置文件,但没有什么突出的。内存配置文件也不突出。这是我在任何其他帆布游戏中看到的典型特征。记忆力上升,然后GC命中。从我所看到的GC来看,每隔30秒左右就会有一次。至少有一个大的GC命中。我只是想知道,如果你不注意,因为Windows上的GPU加速画布。我不认为目前的Chrome浏览器具有MacOSX的功能。也许加那利呢。 –

回答

0

keydown/keyup事件正在一个和另一个press之间等待一小段时间。

这是我如何解决我的情况(不知道这是你的问题);每隔20ms增加一个setInterval检查是否存在关键点(对角线运动不止一个)。

​​

希望帮助:)

+0

有关关键事件延误的有趣理论......但我看不出这将如何解决它? –

+0

是的。感谢您的帮助,但是如果没有按键,移动也会出现问题。 –

+0

对不起我的错,我读得太快了:) – ianaz