Drag and Drop objects from one CCLayer to another

I have three layers in my CCScene. MainLayer which contains GameLayer and HUDLayer. This is what it looks like now:

[ z-Order        ] | [ Layer ]
==============================
  3 ( Top Most )   | HUDLayer
  2 ( Middle )     | GameLayer
  1 ( Bottom )     | MainLayer 

I have added touch listeners (ccTouchBegan, ccTouchMoved, and ccTouchEnded) on my MainLayer which calls GameLayer's method getTouchedObject( CCPoint p_Loc ) which returns the touched object in that layer.

I am now able to successfully 'touch' a specific object in the MainLayer. Now, what I want to do is to drag the object touched and put it into the HUDLayer. When I was working with just one layer, I was able to drag and drop different objects with no problem but the need to implement CCLayers came so I divided the main layer into three different layers.

Answers doesn't need to be cocos2d-x. I understand a bit of Obj-C and can translate Obj-C into C++ so cocos2d answers are welcome. Thanks in advance.

Answers


The easiest way to do it, is probably:

  1. Add a new CCLayer on the very top called HoverLayer or something. This is for modularity.
  2. As soon as a sprite is touched on any layer, remove it from that layer, and add it as a child of HoverLayer at the same position. The user will not "see" anything changed.
  3. On touch up, after dragging the sprite, detect if the sprite is within the HUDLayer or not.
  4. Remove the sprite from the HoverLayer and add it to the appropriate layer.

NOTES:

  • You can use the HUDLayer instead of the HoverLayer, but your class will be messy.
  • Of course, if you keep the touched sprite at the GameLayer while dragging, it will end up behind the HUDLayer at some point, so moving the sprite to a top layer is a must.
  • The code to implement the given solution is trivial. Nothing special to do, really.

Need Your Help

In Java, does read only access to Strings need to be synchronized?

java multithreading thread-safety

Is it required to synchronize read only access to Strings in Java?