Jump to content

[Help] Phaser Groups and Classes


JLevstein
 Share

Recommended Posts

Hi there!

 

I'm having an issue with trying to figure out best practices with creating multiple sprites on a screen. I've been using this blog post as a reference for how to structure my game into classes: http://toastedware.com/?p=258 and so far it's been great. I like how neat it leaves my code.

 

However, I've been trying to create multiple instances of a class and whenever a new instance appears, the previous one loses all of its properties and just drops into the ether.

 

Any advice on how to move forward? I've tried creating a group, but I'm not sure how to pass the class into it.

 

For reference, here is my game so far: https://github.com/JLevstein/tigertail/tree/master/js

Link to comment
Share on other sites

hi JLevstein,

 

The problem is the following:

 

You have one global variable named "tiger".

 

You assign a new Tiger Instance to this variable (in index.html preload).

 

Now your level.js calls the tiger.create() function this happens:

 

the one global variable tiger has one sprite variable, which is initially null.

So on the first call, your one global tiger creates a sprite, saves it in the empty sprite variable, and goes on to run through the level happily.

 

But now level.js calls create again (we want another tiger!) .. but it calls this on the one global tiger..

 

So we enter the create function of this (already happy active tiger) and you create another sprite and assign it to the sprite variable.. (which already has a sprite in it).

So the new tiger sprite is now managed by the tiger routines.

What happens to the old sprite? - it's just a sprite, it does not know better then to fall through the screen (since no collision is called for it any longer, since it's not referenced by the one global tiger anymore.)

 

Okay, I hope you get the picture.

 

Now for the fix:

 

Let's not have one global tiger - but a list of all our tigers.

 

I'll fork your game and create a patch, hopefully it will work out.. I'll post back when I am done.

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