I most likely am doing something wrong, but I can’t quite figure how to update the position of a DrawNode correctly.
I created a bare bones project to simulate this.
I have a sprite, and a drawn node. I create the Sprite, set its position and add it to the scene. Next I create the DrawNode, use the rect origin of the of the sprites bounding box, and for the destination I use the sprites position.
Now if I don’t call drawNode->setPosition(), the drawnode is where I would expect it, but as soon as call drawNode->setPosition(), the drawnode is offset quite a bit.
Also, the drawNode is one quarter of the size I would expect…
Any ideas?
Heres a link to a quick video demonstrating. video
Heres the simple code
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
this->sprite = Sprite::create("16x16_pixel.png");
sprite->setPosition(Vec2(100, 100));
this->addChild(this->sprite);
this->drawNode = DrawNode::create();
drawNode->drawRect(sprite->getBoundingBox().origin, sprite->getPosition(), Color4F(1, 0, 0, 1));
this->addChild(this->drawNode);
this->scheduleUpdate();
return true;
}
void HelloWorld::update(float delta)
{
this->sprite->setPositionX(this->sprite->getPositionX() + 0.1);
this->drawNode->setPositionX(this->sprite->getPositionX());
}
