Cocos2d x 在c+中交换两个精灵的条件+;cocos2d的?

Cocos2d x 在c+中交换两个精灵的条件+;cocos2d的?,cocos2d-x,Cocos2d X,我正在用cocos2d-x开发一个游戏。在那个游戏中,我有两个精灵,当我一个接一个地点击一个精灵时,它应该交换。我不知道这方面的代码,请帮助。我使用以下代码创建了精灵 CCSprite *splash=CCSprite::create("misc_textur111.jpg"); splash->setPosition(ccp(500,300)); this->addChild(splash,1); CCSprite *splash1=CCSprite::c

我正在用cocos2d-x开发一个游戏。在那个游戏中,我有两个精灵,当我一个接一个地点击一个精灵时,它应该交换。我不知道这方面的代码,请帮助。我使用以下代码创建了精灵

  CCSprite *splash=CCSprite::create("misc_textur111.jpg");

   splash->setPosition(ccp(500,300));

    this->addChild(splash,1);


CCSprite *splash1=CCSprite::create("misc_textur222.jpg");

splash1->setPosition(ccp(300,600));

this->addChild(splash,1)
现在我需要交换两个图像的位置。请帮助。

您可以像这样做:

CCPoint position(splash->getPosition());
splash->setPosition(splash1->getPosition());
splash1->setPosition(position);

如果可以的话,我建议您升级到更新版本的Cocos2d-x。版本2.23甚至更好,版本3.v。使用新的
EventDispatcher
拖动精灵非常简单,您可以为每个精灵创建一个
侦听器

例如:

//Create a "one by one" touch event listener (processes one touch at a time)
auto listener1 = EventListenerTouchOneByOne::create();
// When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it.
listener1->setSwallowTouches(true);

//Trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event){
    auto target = static_cast<Sprite*>(event->getCurrentTarget());
    //Move the position of current button sprite
    target->setPosition(target->getPosition() + touch->getDelta());
};
//创建一个“逐个”触摸事件侦听器(一次处理一次触摸)
auto listener1=EventListenerTouchOneByOne::create();
//当“swallow touch”为true时,从OnTouchBegined方法返回“true”将“吞咽”触摸事件,防止其他侦听器使用它。
listener1->SetWallowTouchs(真);
//移动触摸时触发
listener1->onTouchMoved=[](触摸*触摸,事件*事件){
自动目标=static_cast(事件->getCurrentTarget());
//移动当前按钮精灵的位置
目标->设置位置(目标->getPosition()+触摸->getDelta());
};

查看Cocos2d-xwiki:

请更详细地介绍交换。只是位置还是全部价值?我需要交换位置?