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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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