Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Having trouble with groups


Yora
 Share

Recommended Posts

I've moved a TS083 project over to the new JS1,00 and have everything functional besides the new Group giving me some problems.

 

While in TS083 I could have something like this in my update function:

slimeGroup.forEach(function (item) {            item.x -= 1.8;            if (item.x)                if (item.x < -60) {                    slimeGroup.remove(item);                }        });

It will not work in 1.00 unless there's actually an object present in the group(Cannot read property 'exists' of undefined), which initially I solve by changing the code to this to start after the first object has been added to the group:

if (slimeExists) {            slimeGroup.forEach(function (item) {                              item.x -= 1.8;                if (item.x < -60) {                    slimeGroup.remove(item);                }            }, this, true);        }

Which will work fine until slimeGroup.remove is called, then I'm given this error: Cannot read property 'exists' of null since the object has been removed.

 

I'm sure I'm just doing something silly or incorrect and I can't identify a function in the new Group to solve this, so I appreciate any help.  ^_^

 

 

Link to comment
Share on other sites

Aaah nevermind, after enough browsing through examples I found the correct way to use groups in the groupvsgroup collision example, namely this:

for (var i = 0; i < 10; i++)        {            var b = bullets.create(0, 0, 'bullet');            b.name = 'bullet' + i;            b.exists = false;            b.visible = false;            b.events.onOutOfBounds.add(resetBullet, this);        }

I had been thinking all I really needed to solve the problem was for all the objects to exist initially through create() which bothered me since I wouldn't be using them right away, yet it never occurred to me I could just set them all to exists=false, haha.  :lol:   

 

Also was nice to find the onOutOfBounds method.

Link to comment
Share on other sites

:) I think it would make a useful parameter Group.create to set the initial state of the objects (alive vs. dead).

 

But you made a good find really, so I've added an extra check into every function to ensure the Group has contents before doing anything.

Yes indeedy as that's a property I'm always going to be setting when creating groups now.  And I'm glad I could be of some help, no matter how small.  ^_^

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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