Alright, in case you still haven’t figured it out, here’s a debug version that outputs position info and then shows placing a new sprite both on the root scene (which scrolls left along with the background) as well as placing it as attached to the this->node2
sprite (which then looks as if it stays in place at the mouse location).
(All these are in HelloWorldScene.cpp - I don’t believe I changed anything in the .h header file)
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
// 1. super init first
if (!Scene::init())
{
return false;
}
this->_mouseListener_3->onMouseDown=CC_CALLBACK_1(HelloWorld::onMouseDown,this);
_eventDispatcher_3->addEventListenerWithSceneGraphPriority(_mouseListener_3,this);
auto s = Director::getInstance()->getWinSize();
// Background
auto bg = Sprite::create("q.png");
bg->setPosition(0,0);
bg->setPosition(1000,50);
this->addChild(bg,0);
// "Player" or something?
this->node2=Sprite::create("ty.png");
node2->setPosition(Vec2(100,50));
this->addChild(node2,4);
// Run Test Actions
auto pFollow = Follow::create(node2,Rect(0, 0, s.width+480, s.height));
this->runAction(pFollow);
// slowed this one down to more easily see what's going on
auto moveTo = MoveTo::create(80, Vec2(1000,50));
node2->runAction(moveTo);
return true;
}
// ----------------------------------------------------------------------
// A quick and dirty log to avoid repetative typing for logging
#define CCLOG_VEC2(varname) CCLOG_Vec2(#varname, varname)
static void CCLOG_Vec2(std::string name, Vec2 v) {
CCLOG("%s: {%.2f, %.2f}", name.c_str(), v.x, v.y);
}
// ----------------------------------------------------------------------
void HelloWorld::onMouseDown(Event* event){
auto eventMouse = (EventMouse *)event;
// The mouse location in Screen Coordinates
const Vec2 mouseLocation = Vec2(eventMouse->getCursorX(), eventMouse->getCursorY());
CCLOG_VEC2(mouseLocation);
// ----------------------------------------------------------------------
// Here we attach to the scene itself (ie: the root Node), so
// we need to convert the mouseLocation to the "world" position
// of the scene (or root node).
Vec2 worldOriginOffset = this->getPosition();
CCLOG_VEC2(worldOriginOffset);
// This is essentially a "screen to world" conversion
// ("world" is usually root node of the game layer/scene)
Vec2 mouseLocationInWorldCoords = mouseLocation - worldOriginOffset;
CCLOG_VEC2(mouseLocationInWorldCoords);
auto nodeMouse1 = Sprite::create("ty.png");
nodeMouse1->setPosition(mouseLocationInWorldCoords);
nodeMouse1->setColor(cocos2d::Color3B::GREEN);
this->addChild(nodeMouse1, 5);
// ----------------------------------------------------------------------
// Here we attach to `node2`, which would keep it "attached" to node2 if node2 moves
// Need to convert the location
Vec2 node2pos = this->node2->getPosition();
CCLOG_VEC2(node2pos);
// This is a test only, only affects logging the coord,
// not used for anything else in this example.
Vec2 node2PosInWorldCoords = this->node2->convertToWorldSpace(node2pos);
CCLOG_VEC2(node2PosInWorldCoords);
Vec2 screenOriginInWorldCoords = Vec2(0,0) - worldOriginOffset;
Vec2 node2PosInScreenCoords = node2pos - screenOriginInWorldCoords;
CCLOG_VEC2(node2PosInScreenCoords);
Vec2 node2anchor = this->node2->getAnchorPoint();
CCLOG_VEC2(node2anchor);
// When we set the position on a new node it's positioned off the lower-left corner of the ..
// .. Node/Sprite's "content" rect (the default rect is defined by the size of the Sprite's texture)
Vec2 anchor = this->node2->getAnchorPoint();
Size s = this->node2->getContentSize();
Vec2 node2CenterOffset = Vec2(anchor.x * s.width, anchor.y * s.height);
CCLOG_VEC2(node2CenterOffset);
// This is essentially a "screen to node" conversion
Vec2 mouseLocationInNode2Coords = mouseLocation - node2PosInScreenCoords + node2CenterOffset;
CCLOG_VEC2(mouseLocationInNode2Coords);
// Create and position a new sprite at the mouse location, but attached to `this->node2`
auto nodeMouse2 = Sprite::create("ty.png");
nodeMouse2->setPosition(mouseLocationInNode2Coords);
nodeMouse2->setColor(cocos2d::Color3B::MAGENTA);
this->node2->addChild(nodeMouse2, 5);
// ----------------------------------------------------------------------
// // Here's a way to get the same node without an class data member (ie: this->node2)
// Node* node = this->getChildByTag(1234);
// if (!node) {
// node = Sprite::create("ty.png");
// node->setTag(1234);
// this->addChild(node,3);
// }
// node->setPosition(mouseLocationInWorldCoords);
// ----------------------------------------------------------------------
CCLOG("----------------------------------------");
}