wusiquan Posted June 2, 2018 Share Posted June 2, 2018 I found sprite as a gameObject has events methods, like on, off, emit... So when a sprite knows something, eg. it moves out of a space, how to notify its 'parent' object? for instance, the group or the scene even the game. Though it could be bound when the sprite was created, but this need to bind listener to every sprite in the group, it seems not a good practice // --- class GameScene --- this.pipes = this.add.group() let onespirte = this.pipes.create(0, 0, 'onespirte') // error, this.pipes doesn't has `on` method this.pipes.on('iamout', function(onespirte) { // operations... onespirte.destroy() // .... }) // --- class OneSprite --- onespirte.emit('iamout') Link to comment Share on other sites More sharing options...
B3L7 Posted June 4, 2018 Share Posted June 4, 2018 I think you will want to use scene events. So: // --- class GameScene --- this.pipes = this.add.group(); let onespirte = this.pipes.create(0, 0, 'onespirte') this.events.on('iamout', function(onespirte) { onespirte.destroy(); }) // --- class OneSprite --- this.scene.events.emit('iamout') //this refers to OneSprite (assumes you have passed scene through config object to class and made it a property of the class) Link to comment Share on other sites More sharing options...
wusiquan Posted June 5, 2018 Author Share Posted June 5, 2018 Previous, I forgot, the `scene` property is common in a scene. And communicate with events is better than calling methods directly, sometimes In a word, It seems a good solution. I had tried, it worked well. Thanks to you. B3L7 1 Link to comment Share on other sites More sharing options...
Recommended Posts