如何给某个精灵增加事件
发布于 4 年前 作者 langyunbao 2222 次浏览 来自 分享

<!DOCTYPE html> <html lang=“en”> <head> <mead charset=“UTF-8”> <title>example</title> <script type=“text/javascript” src=“phaser.js”></script> </head> <body> <script type=“text/javascript”> var game = new Phaser.Game(800,600, Phaser.AUTO, ‘’); var main = { init:function(){ game.stage.disableVisibilityChange = true; }, preload:function(){ game.load.image(‘bg’, ‘assets/sky.png’); game.load.spritesheet(‘player’, ‘assets/baddie.png’, 32,32,4);

			game.load.spritesheet('man', 'assets/spaceman.png', 16,16);
			game.load.spritesheet('dude', 'assets/dude.png', 32,32);

			game.load.onFileComplete.add(function(){
			});
			game.load.onLoadComplete.add(function(){
			});
		},
		create:function(){

			this.bunny = game.add.sprite(game.world.centerX,game.world.centerY,'dude');
			this.bunny.alpha = 0.5;
			this.bunny.anchor.set(0.5);
			this.bunny.inputEnabled  = true;
			this.bunny.events.onInputDown.add(this.aaa,this);
			this.bunny.events.onInputUp.add(this.bbb,this);

		},
		aaa:function(){
			this.bunny.alpha = 1;
		},
		bbb:function(){
			this.bunny.alpha = 0.5;
		},
		update:function(){
		},
		render:function(){

		}
	};

	game.state.add('main', main);
	game.state.start('main');
</script>

</body>

</html>

1 回复

很棒,这几句是关键代码

this.bunny.inputEnabled = true; this.bunny.events.onInputDown.add(this.aaa,this); this.bunny.events.onInputUp.add(this.bbb,this);

回到顶部