0

Faced strange behaviour of CCSprite positioning on a screen. Scenario is simple: change sprite position on touch (dragging item on a screen).

private void HandleInput(List<CCTouch> arg1, CCEvent arg2)
{
    base.Position = arg1.Last().LocationOnScreen;
}

After setting base.Position to LocationOnScreen it simply disappears.


NOTE: cocossharp is a c# port of famous framework and is quite similar in its structure and implementation to cocos2d-x hence adding this tag.
Any help appreciated.

Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54

1 Answers1

2

In cocos2d-x nodes are positioned relative to their parents content area.

Typically, to position a sprite in cocos2d-x using a touch derived (world space) co-ordinate you would use the convertToNodeSpace method on the parent.

In c++ because cocos2d-x:

void setSpritePositionWhenTouched(Node* sprite, const Vec2& touchPos)
{
  auto parent = sprite->getParent();
  auto pos = parent->convertToNodeSpace(touchPos);
  sprite->setPosition(pos);
}
Chris Becke
  • 750
  • 4
  • 10