Jump to content

how does a sprite communicate with group


wusiquan
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...