Phaser:删除先前使用drawCircle绘制的圆

时间:2016-02-14 22:14:57

标签: phaser-framework

在Phaser(2.4.x)中,我在拖动精灵时画了一个圆圈:

function dragStart(sprite, pointer, dragX, dragY) {
    var graphics = game.add.graphics(0, 0);
    graphics.lineStyle(6, 0x909090, 0.3);
    graphics.drawCircle(dragX, dragY, 200);
}

工作正常,但现在我需要在拖动结束时移除圆圈,我无法弄清楚那部分:

function dragStop() {
    // ?
}

是否可以删除图形?是否有更好或更简单的选项来绘制圆圈并在以后删除它?

1 个答案:

答案 0 :(得分:1)

你可以kill()对象

但要注意你想要杀死的var的范围(你在函数内部定义它)。

或者您可以创建图形,然后根据您的事件显示或隐藏(拖动)

我给你们两个解决方案的一个非常简单的例子:



var game = new Phaser.Game(500, 500, Phaser.AUTO, 'game');
var mainState = { 
create:function(){
    var graphics = game.add.graphics(0, 0);
    graphics.lineStyle(6, 0x909090, 0.3);
    graphics.drawCircle(game.world.centerX+100,game.world.centerY+100, 200);
    console.log(graphics);
    setTimeout(function(){
    	graphics.kill();
      },2000);
    this.graphics2 = game.add.graphics(0, 0);
    this.graphics2.lineStyle(6, 0xff0000, 1);
    this.graphics2.drawCircle(game.world.centerX-100,game.world.centerY-100, 200);
    this.graphics2.visible = false;
    this.show_later = game.time.now + 2000;
    this.hide_again = game.time.now + 4000;
    
},

update:function(){
	if(this.show_later < game.time.now){
  	this.graphics2.visible = false;
  }
  if(this.hide_again < game.time.now){
  	this.graphics2.visible = true;
  }
},
};
game.state.add('main', mainState);  
game.state.start('main');
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.min.js"></script>
<div id="game"></div>
&#13;
&#13;
&#13;