I have to do something a bit tricky with input. I have a class named DraggableSprite that overrides an sprite and register to listen to input events like onTouchBegin, onTouchDrag, etc… This is working as expected and I can drag it across the screen. The problem is that when I drag it over a given screen zone I need to remove it and pressent another DraggableSprite that needs to be the new input receiver. When I create the new sprite I loose “focus” and this sprite does not receive any input.
Is there anyway to accomplish this? Is there anyway to tell the input system to a node without being the one that received the onTouchBegan? I need this to finish my Drag and drop system…
Could you consider just changing the image on your draggable sprite instead of creating a new one, and maybe set a variable to remember that it changed?
Well, I could just want to create a node hierarchy not just an sprite. My previous post just shown an example. I have for example a DraggableLayout that can contain an hierarchy of nodes. So, no, I can’t just change the image. I need something to send the input to the new created (top) node.
On the other hand I’m considering making the “container” of the sprite to handle input instead of the draggable node. This way, the problem with the dragging node would disappear but all my current design is created around the system I have right now so, I would prefer not to move it to another one right now.
Maybe someone else has an idea, but I looked at the code for Cocos2d handling touch and it looks like there isn’t a function to give touch to a new node. You would have to look into adding this yourself or choosing a method that doesn’t require removing the node you are touching.
Thanks for your time. Before asking I had a look at source and it seemed not to have it. I don’t like to touch cocos2d-x code to be able to upgrade without problems so, I think I should be looking at other options. Let’s see if anyone has dealt with this before and have a solution.
I had another quick look. There’s a function in CCEventListener called setAssociatedNode which would allow you to do what you want. Unfortunately it is protected, so without changing it to public (which means changing Cocos code) you can’t directly access it.
There is one quick hack though; create a class that inherits from the EventListener you want to use and create a function that calls setAssociatedNode. For example class MyEventListenerTouchOneByOne : public cocos2d::EventListenerTouchOneByOne. This does not require editing Cocos2d code, will work fine when upgrading Cocos2d (provided the setAssociatedNode function isn’t removed) and allows you to switch the node you get when you call getCurrentTarget.
This works for the test I did, but I just don’t think it feels nice to create a whole class just to access one protected function haha
Thanks a lot for taking the time to find all this.
To be true, I did find out same options checking the code. At last I have choosen to have a “src node input” and a “target node input” in my d&d system. It is a bit weird for me to do this but works ok, it does something like Input Node -> D&D System -> Target Node. This way it is transparent to the system. Target node could be a parent with attached children too. What allows for multiple item d&d.