一段时间后游戏速度减慢,潜在数据泄漏?

时间:2016-06-18 04:00:31

标签: javascript memory-leaks p5.js

我正在使用p5和p5.play进行javascript游戏,并且由于某些原因,在模拟运行超过20秒后,整个事情真的变慢了。我认为这可能是数据泄漏但不确定什么是真正危害性能。

更新:经过一番探索之后,我发现我成功地从普通组中删除了精灵,但是有一些名为allSprites的其他对象正在收集精灵,无论如何。我如何从这个对象中删除所说的精灵仍然是未知的。

更新:提出了从对象中删除它们的方法,仍然没有修复数据泄漏。

var players;
var deadly;
var powerUp;
var plain;
var penguinSheet;
var penguinAnimation;
var willLoad;
var harmless;
var score = 0;

function preload(){
  penguinSheet = loadSpriteSheet('./assets/BoxPenguinSprites.png', 64, 64, 3);
  penguinAnimation = loadAnimation(penguinSheet);
}

function setup() {
    createCanvas(700, 600);
    // players = new Group();
    // deadly = new Group();
    // powerUp = new Group();
    // harmless = new Group();
    plain = new Group();
    useQuadTree(true);
    player = createSprite(100, 100, 20, 20);
    player.debug = true;
  }

function update(){
  for (var i = 0; i < 10; i++) {
    var c = createSprite(
      random(width), 0,
      random(25, 75), random(10, 30));
    c.addSpeed(random(1,3), 90);
    c.shapeColor = color(random(30, 50), random(180, 220), random(80, 130));
    plain.add(c);
  }
  // for (var k = 0; k < 3; k++) {
  //   var d = createSprite(
  //     random(width), 0,
  //     random(25, 75), random(10, 30));
  //   d.addSpeed(random(1,3), 90);
  //   d.shapeColor = color(random(0, 30), random(150, 160), random(50, 80));
  //   harmless.add(d);
  // }
}

function cleanUp(){
  for (var j = 0; j < plain.length; j++){
    if (plain[j].newPosition.y > height){
      plain.remove(plain[j]);
    }
  }
  // for (j = 0; j< harmless.length; j++){
  //   if (harmless[j].newPosition.y > height){
  //     harmless.remove(harmless[j]);
  //   }
  // }
}

function pixleBuffer(){
  var offsetX = 0;
  var offsetY = 0;
  if (keyIsDown(LEFT_ARROW)){
    offsetX = -3;
  }
  if (keyIsDown(RIGHT_ARROW)){
    offsetX = 3;
  }
  if (keyIsDown(UP_ARROW)){
    offsetY = -3;
  }
  if (keyIsDown(DOWN_ARROW)){
    offsetY = 3;
  }
  player.setCollider("rectangle", offsetX , offsetY, 20, 20);
}
//
function draw() {

  if (frameCount == 1 || frameCount % 100 == 0){
    update();
  }
  // player.overlap(harmless);
  // plain.collide(harmless);
  // harmless.collide(plain);
  // harmless.collide(harmless);
  player.collide(plain);
  plain.collide(plain);
  if(keyIsDown(LEFT_ARROW)){
    player.position.x -= 3
  }
  if(keyIsDown(RIGHT_ARROW)){
    player.position.x += 3
  }
  if(keyIsDown(UP_ARROW)){
    player.position.y -=3
  }
  if(keyIsDown(DOWN_ARROW)){
    player.position.y += 3
  }
  pixleBuffer();
  clear();
  cleanUp();
  drawSprites();
}

0 个答案:

没有答案