人物坐标乱跑。。求大神

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)//12-设置在输出所点击的坐标
{
//auto touchLocation = touch->getLocation();
//touchLocation = this->convertToWorldSpace(touchLocation);
log(“the location is: %lf,%lf !”, touch->getLocationInView().x, touch->getLocationInView().y);
auto sprite_1 = this->getChildByTag(1);
//sprite_1->setPosition(Point(touch->getLocation().x, touch->getLocation().y));
sprite_1->runAction(MoveTo::create(3.0f, Point((touch->getLocation().x), (touch->getLocation().y))));
//setViewPointCenter(sprite_1->getPosition());
//----------------------//

return true;
}
只能在屏幕左下角一大块地走来走去。
出了左下角就开始乱跑。。
帮我看看错了哪里。。。 Classes.rar (4 KB)

我认为:

你将精灵 和 tilemap层都加入 了 this 中。
sprite2 和 tilemap是并列的级别。

因为在左下角移动,setViewPointCenter 函数中的 actualPosition 都是 屏幕的中心点,就是 viewPoint 偏移位置都为0 ,所以 this 还是原来的位置, 但是当 viewPoint 有变化时,你移动的 是 this,, this上面的 sprite2 和 tilemap 也会一起移动,因为他们是 this的孩子,所以,当sprite2 去你点击的位置时,移动的应该是 tilemap层,而不是this。

setViewPointCenter 可以使用定时器,传入 sprite2的 getPosition 来设置 tilemap的位置。