Cocos2d x 在COCOS2D2.0.4中拖放精灵 你能用CasCO2D2.0.4.4/在C++中用鼠标拖动精灵位置来帮助我吗? CCSprite *splash=CCSprite::create("Level3.png"); splash->setPosition((200,500)); this->addChild(splash,1); CCSprite *splash1=CCSprite::create("Level2.png"); splash1->setPosition((300,500)); this->addChild(splash,1); CCSprite *splash=CCSprite::create("Level3.png"); splash->setPosition((200,500)); this->addChild(splash,1); CCSprite *splash1=CCSprite::create("Level2.png"); splash1->setPosition((300,500)); this->addChild(splash,1);

Cocos2d x 在COCOS2D2.0.4中拖放精灵 你能用CasCO2D2.0.4.4/在C++中用鼠标拖动精灵位置来帮助我吗? CCSprite *splash=CCSprite::create("Level3.png"); splash->setPosition((200,500)); this->addChild(splash,1); CCSprite *splash1=CCSprite::create("Level2.png"); splash1->setPosition((300,500)); this->addChild(splash,1); CCSprite *splash=CCSprite::create("Level3.png"); splash->setPosition((200,500)); this->addChild(splash,1); CCSprite *splash1=CCSprite::create("Level2.png"); splash1->setPosition((300,500)); this->addChild(splash,1);,cocos2d-x,Cocos2d X,使用鼠标拖放操作进行交换我想你知道如何处理触摸。要进行交换,请执行以下步骤 从任何开始拖动的位置复制该精灵。(您在CCSprite中有getTexture,可以使用它创建新的CCSprite) 无论你在哪里放置该精灵纹理的存储地址 通过拖动纹理更改sprite的纹理(再次使用getTexture和setTexture方法) 无论您在哪里开始拖动,请在第2步中使用使用存储的纹理地址的setTexture方法 最后,您必须移除正在拖动的精灵 对于Cocos2d-x2.0.4,由于速度问题,不建议在

使用鼠标拖放操作进行交换

我想你知道如何处理触摸。要进行交换,请执行以下步骤

  • 从任何开始拖动的位置复制该精灵。(您在CCSprite中有getTexture,可以使用它创建新的CCSprite)
  • 无论你在哪里放置该精灵纹理的存储地址
  • 通过拖动纹理更改sprite的纹理(再次使用getTexture和setTexture方法)
  • 无论您在哪里开始拖动,请在第2步中使用使用存储的纹理地址的setTexture方法
  • 最后,您必须移除正在拖动的精灵

对于Cocos2d-x2.0.4,由于速度问题,不建议在每个单独的
cococos2D::CCSprite*
上创建事件

一种解决方案是将
cocos2d::CCSprites*
存储在
std::vector
中,并让
cocos2d::Layer
响应触摸事件。当
cocos2d::Layer*
接收并处理事件时,通过
std::vector
循环,查看哪个
cocos2d::CCSprite*
包含触摸,然后对其进行操作。(选中边界框)

通过子类化
public cocos2d::CCLayer
cocos2d::CCTargetedTouchDelegate
以及重写以下几个函数,您可以将触摸添加到
cococos2d::Layer*

#include "cocos2d.h"
using namespace cocos2d;
class MyLayer : public cocos2d::CCLayer, public cocos2d::CCTargetedTouchDelegate 
{
    public:
        virtual void onEnter();
        virtual void onExit();
        virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
        virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
        virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
};