Jump to content

Changing a group size or maxSize from registry.get()


scope2229
 Share

Recommended Posts

Hi i have a player prefab that imports a bullet prefab. inside my player prefab i call the bullet like so

this.bullets = this.scene.add.group({
        classType: Bullet,
        maxSize: 1,
        runChildUpdate: true
    });

now maxSize i want to change on the fly. Should i place the group inside update instead and then have this

 

this.magSize = this.scene.registry.get('magSize');
    this.bullets = this.scene.add.group({
        classType: Bullet,
        maxSize: this.magSize,
        runChildUpdate: true
    });

or another way i can think of us using an update event from the registry itself something like

this.registry.events.on('changedata', this.updateData, this);

then create the group inside the updateData function ????

Link to comment
Share on other sites

Or could i get rid of the maxSize and use some thing like

this.magSize = this.registry.get('magSize')
this.magBal = this.bulletGroup.countAlive()

if(!magBal == magSize){
  if(check if fire button is pressed and time since last shot > intervalShot){
    fire the bullet using the normal way maxSize will always be 10 as to many otherwise
  }
}

 

Link to comment
Share on other sites

I found a very rudimentary way of achiving this if anyone knows of a better way be great to see but basically its like this.

any more than 10 bullets is a waste of resources so ill keep maxSize at 10.

then set an object equal to the groups active count.

just just below the update for your cursors ask if active count is <= maxSize and then continue with your creation

 

this.magBal = this.bullets.countActive();
    if(this.fireButton.isDown  && time > this.lastFired){
      if(this.magBal < this.magSize){
        console.log("fire button down");
        let bullet = this.bullets.get();
        if (bullet){
          bullet.fireWeapon(this.x, this.y);
          this.lastFired = time + 100;
        }
      }
    }

just as a tester rather than creating elements to collect as this is as easy as updating the registry data simply set a key to add 1 every time its pushed and you will see the bullet fire with the magSize.

Later on something i can see that would be good is to change the fire rate and speed depending on the magSize or even add a reload function

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...