Jump to content

Weird "attach child" behavior ?


metalNumb
 Share

Recommended Posts

OK !


Here's what is happening !!!


 


http://pixos-metalnumb.c9.io/


 


I created a sprite class for the tiles (ColorTile), and each object of this class will attach a graphics object as a child. 


In its UPDATE part, when sprite is clicked, it updates a colorText , and it checks if the currentColor == this TileColor and if so.... kills sprite.


 


- currentColor = RED.


 


In the main game (MiniGame) i create these tile objects and add them to a grid group .... all by calling the function respawnGrid();


 


2 Weird behaviors...


1- My code is supposed to be the same (before attaching child to sprite), but the grid now has additional spacing between them ( attached children are not positioned same as sprites ? )


2- If you click somewhere in the gaps after first column and before or after second row, it still behaves as if i am clicking a sprite (check the debug text) and if there is a red rectangle nearby, it might disappear... and also i click on some red tiles and they won't disappear. 


 


This is frustrating, is there some invisible sprites or something i must be missing ?


 


I Appreciate help.


Thanks.


Link to comment
Share on other sites

For debugging purposes, i enabled drag on the rectangles, and when i move some rectangles, other rectangles move.

I also let the parent sprite not to be empty ( added an image to it).

 

The behavior makes sense if you see it, but i don't know why it is happening !

 

(Try to drag one of the 'E' sprites, those are the parents )  :D

 

This is weird.  :huh:

Any help ?

Link to comment
Share on other sites

Hey,

use a group to hold both the sprite and the graphics child.  A sprite is not supposed to hold any children. I don't know why pixi is even rendering them - they lose their world transformations so that you see them rendered at a position relative to the parent but absolute to the world.

 

In your case I would try to inherit from a Phaser.Group, not from Phaser.Sprite. Inside that group create whatever content you need. This should fix both of your problems. #1 is caused by the missing world transformations. #2 is happening, because you're clicking on the smaller graphics with the wrong position. Therefore the click is fine but the positioning is wrong due to #1.

 

I hope that helps. The next time you should create a more accessible and reduced codebase with something like jsfiddle. This will increase your chance to get feedback from the community. It's annoying to dig through a website.

 

Regards George

Link to comment
Share on other sites

Thanks George. :lol:

 

But if i want to create a graphics element that behaves same as sprite, what exactly should i add to the group...?

because i can't attachChild() graphics to a sprite....

( What's weird is... why does addChild() even exist in the Sprite class as long as it's not supposed to have added children :huh:,

   or maybe it's just my misunderstanding of what a child is  :rolleyes: ).

 

Anyway your support is very appreciated.

Thanks again. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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