0

I am curious if anyone knows how to detect when a SpriteBatchNode has been touched since it's BoundingBox is always null. This is how I detect touch for single sprites.

Node *parentNode = event->getCurrentTarget();
Vector<Node *> children = parentNode->getChildren();
Point touchPosition = parentNode->convertTouchToNodeSpace(touch);

for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
    childObject = *iter;

if (childObject->getBoundingBox().containsPoint(touchPosition)){

   //do something
}

But in most cases I want my sprites to be animated hence using SpriteBatchNode. Any ideas? Can I get the BoundingBox of the grandchildren since they are a series of sprites?

ctapp1
  • 556
  • 1
  • 5
  • 19

2 Answers2

0

Depends on which method you are using.

  1. Armature skeletal animation: Are you using the cocostudio skeletal animation tool to create your animations? If you use that you will get a node with the correct bounding box wrapping your sprite tightly and adjusting when bones change position

  2. Sprite sheet animation: If you are using a sprite sheet with a .plist file you can inspect the size reflected in the .plist file and set your batchNode size to the biggest one you find, or dynamically adjust it based on the sprite currently being shown. I think cocos does this by default.

  3. Loading sprite frames: If you are loading individual sprites using spriteFrames, you can inspect the contentSize of the spriteFrame and set your bounding box manually.

I have used all 3 and were always able to get the boundingBox size. Let me know if this helps. I used this article to learn sprite sheet animations and just played around with cocos skeletal animation and figured that out as I was experimenting with it.

Filled Stacks
  • 4,116
  • 1
  • 23
  • 36
0

Well I figured it out by getting the BoundingBox of the grandchild, which is a sprite. I was then able to do whatever I wanted to the spritebatchnode.

ctapp1
  • 556
  • 1
  • 5
  • 19