为什么 getCursorX() 无法获取窗口区域外的场景的值?

当光标点击这个位置时,节点移动到这里,这里没有问题,图片和代码如下:

void HelloWorld::onMouseDown(Event* event){
		node3_3=Sprite::create("ty.png");
		node3_3->setPosition(node2->getPosition());
		this->addChild(node3_3);
		this->p_3=(EventMouse*)event;	
		auto moveTo = MoveTo::create(1, Vec2(p_3->getCursorX(),p_3->getCursorY()));
		node3_3->runAction(moveTo);
}

然后节点向右移动,
场景向左移动一定距离后,光标停留在红圈上并点击,但是节点没有移动到红圈的地方,这是为什么?图片及代码如下:

bool HelloWorld::init(){
	auto s = Director::getInstance()->getWinSize();
	auto pFollow=Follow::create(node2,Rect(0, 0, s.width+480, s.height));
	this->runAction(pFollow);
}
void HelloWorld::onMouseDown(Event* event){
		node3_3=Sprite::create("ty.png");
		node3_3->setPosition(node2->getPosition());
		this->addChild(node3_3);
		this->p_3=(EventMouse*)event;	
		auto moveTo = MoveTo::create(1, Vec2(p_3->getCursorX(),p_3->getCursorY()));
		node3_3->runAction(moveTo);
}

场景有位移了呗,需要考虑场景的位移吧

  • 光标的是世界坐标
  • 你ty.png是场景子节点(猜测)

把光标坐标转移到场景坐标系看看

是不是用convertToNodeSpace函数将光标的世界坐标系转换为场景坐标?
但是使用后还是不行,
代码如下:

void HelloWorld::onMouseDown(Event* event){
		node3_3=Sprite::create("ty.png");
		node3_3->setPosition(node2->getPosition());
		this->addChild(node3_3);
		SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
		this->p_3=(EventMouse*)event;	
		Vec2 pos=node3_3->convertToNodeSpace(Vec2(p_3->getCursorX(),p_3->getCursorY()));
		auto moveTo = MoveTo::create(1, Vec2(pos)); 
		node3_3->runAction(moveTo);
}

Vec2 pos=this->convertToNodeSpace(Vec2(p_3->getCursorX(),p_3->getCursorY()));

切换到当前的节点父节点的坐标系
你试试

改成父节点后可以了,
我想问下另外一个问题,既然光标是世界坐标系,如果把node3_3节点也转换成世界坐标系,这样可不可以?

node3_3 在场景Scene下的,Scene的位置如果为(0,0,0) 则node3_3所在坐标系等同于世界坐标系

你可以添加一个层Layer,移动Layer而不是Scene