B
B
BonBon Slick2019-12-22 19:59:17
Cocos2d
BonBon Slick, 2019-12-22 19:59:17

How to get position of DrawNode?

DrawNode* circleInner = DrawNode::create();
  circleInner->drawDot(
    Vec2(100, 100),
    55,
    Color4F::BLUE
  );
  circleInner->setTag(1);
  this->addChild(circleInner);


  CCLOG("%d", circleInner->getPositionX()); // 0
  CCLOG("%d", circleInner->getPosition()); // 0
  CCLOG("%d", circleInner->getPositionNormalized()); // 0

All zeros, why?
Because of this, checking whether the user has touched the sprite does not pass the circle.
auto circleInner  = this->getChildByTag(1);
  if (circleInner->getBoundingBox().containsPoint(Vec2(100, 100))) {
    CCLOG(" is circle pressed");
  }

// or

bool HelloWorldScene::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
  auto circleInner  = this->getChildByTag(1);
  if (!circleInner) {
    return true;
  }
  CCLOG(__FUNCTION__);
  CCLOG(" getLocation x  %d", touch->getLocation().x);
  CCLOG(" getLocation y %d", touch->getLocation().y);

  if (circleInner->getBoundingBox().containsPoint(touch->getLocation())) {
    CCLOG(" is circle pressed");
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question