我第一次做一个小游戏,只是为了练习。正如你在我的小提琴中看到的那样,当屏幕上有很多子弹和/或敌人时,它会在一段时间后开始掉帧。有没有办法通过以某种方式缓存它来减少这种情况?
我读过一些关于预渲染的东西。但由于我使用的是 Kinetic 库,所以我认为这不是我能做的。是否有一些提示和技巧来减少它的丢帧?还是一种使用 KineticJS 进行预渲染的方法?
这是我的小提琴:http: //jsfiddle.net/3nEUv/3/
我认为子弹的绘制是一个真正的交易破坏者:(小提琴中的第 713 行)
function Enemybullet(destinationX, destinationY, enemySprite) {
this.id = 'enemyBullet';
this.x = enemySprite.getX()+(enemySprite.getWidth()/2);
this.y = enemySprite.getY()+(enemySprite.getHeight()/2);
this.damage = enemy.damage;
//The targetX and Y are compensated by the player width and height. Subtract or add the halve of the player accordingly
if (this.x > player.sprite.x) {
var targetX = (destinationX - this.x)-(player.sprite.getWidth()/2);
targetY = (destinationY - this.y)-(player.sprite.getHeight()/2);
} else {
var targetX = (destinationX - this.x)+(player.sprite.getWidth()/2);
targetY = (destinationY - this.y)+(player.sprite.getHeight()/2);
}
var distance = Math.sqrt(targetX * targetX + targetY * targetY);
this.velX = (targetX / distance) * enemyAttackSpeed;
this.velY = (targetY / distance) * enemyAttackSpeed;
this.finished = false;
this.sprite = new Kinetic.Circle({
x: this.x,
y: this.y,
radius: 2,
fill: 'black',
name: 'enemyProjectile'
});
this.draw = function(indexEnemy, indexBullet) {
var mayDelete = false;
this.x += this.velX;
this.y += this.velY;
this.sprite.setAbsolutePosition(this.x, this.y);
//console.log(this.sprite.getX());
if(collisionDetection(this, player) == true) {
player.collide(this);
mayDelete = true;
}
if (bulletLeftField(this.sprite) == true) {
mayDelete = true;
}
if (mayDelete == true) {
delete this;
this.sprite.remove();
//console.log(enemies[indexEnemy].bullets);
if (enemies[indexEnemy]) {
enemies[indexEnemy].bullets.splice(indexBullet, 1);
}
}
ammoLayer.draw();
}
}
提前致谢!