当前位置:编程学习 > wap >>

学习笔记之cocos2d-x2.1.1实现多个精灵的拖动

[cpp] view plaincopyprint? 

场景中存在多个精灵,需要移动以安放在合适的位置,简单的move会出现精灵区域重叠的情况,稍微在ccTouchBegan函数中做修改就可以解决这个问题,每次移动当前ccTouchBegan点击下的那个精灵。需要开启ccTouchBegan和ccTouchMoved的代理,不然单单的setTouchEnabled(true);是没有效果的,还是直接看代码吧

 

[cpp]
bool HelloWorld::init() 

    bool bRet = false; 
    do  
    { 
        CC_BREAK_IF(! CCLayer::init()); 
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create( 
            "CloseNormal.png", 
            "CloseSelected.png", 
            this, 
            menu_selector(HelloWorld::menuCloseCallback)); 
        CC_BREAK_IF(! pCloseItem); 
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 500)); 
 
        CCMenuItemImage *pSaveItem = CCMenuItemImage::create( 
            "save_1.png", 
            "save_2.png", 
            this, 
            menu_selector(HelloWorld::menuSaveCallback)); 
        CC_BREAK_IF(! pSaveItem); 
        pSaveItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 100, 100)); 
 
        CCMenu* pMenu = CCMenu::create(pCloseItem,pSaveItem, NULL); 
        pMenu->setPosition(CCPointZero); 
        CC_BREAK_IF(! pMenu); 
        this->addChild(pMenu, 1); 
 
        CCSize s = CCDirector::sharedDirector()->getWinSize(); 
 
        CCSprite* bg = CCSprite::create("bg.png"); 
        CC_BREAK_IF(!bg); 
        bg->setPosition(ccp(s.width/2,s.height/2)); 
        this->addChild(bg); 
        //////////////////////////////////////////////////////////////////////////  
        TagCount = 0; 
 
        CCSprite* sprite=CCSprite::create("1.png");  
        CC_BREAK_IF(!sprite);  
        sprite->setPosition(ccp(100,200)); 
        this->addChild(sprite,1,++TagCount);  
 
        CCSprite* sprite2=CCSprite::create("2.png");  
        CC_BREAK_IF(!sprite2);  
        sprite2->setPosition(ccp(500,200)); 
        this->addChild(sprite2,2,++TagCount);  
 
        CCDirector* pDirector = CCDirector::sharedDirector(); 
        pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
 
        setTouchEnabled(true); 
        bRet = true; 
    } while (0); 
    return bRet; 

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
{  
    int tag ; 
    for (tag = 1;tag<=TagCount;tag++) 
    { 
        CCSprite* sprite= (CCSprite*)this->getChildByTag(tag);  
 
        CCPoint touchPoint = pTouch->getLocationInView(); 
        touchPoint = CCDirector::sharedDirector()->convertToGL( touchPoint ); 
 
        CCRect rc1 = sprite->boundingBox(); 
        if (rc1.containsPoint(touchPoint)) 
        {  
            pSprite = sprite; 
            return true; 
        } 
    } 
    return false;  
}  
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)  

    CCPoint beginPoint=pTouch->getLocationInView();  
    beginPoint=CCDirector::sharedDirector()->convertToGL(beginPoint);  
 
    CCPoint endPoint=pTouch->getPreviousLocationInView();  
    endPoint=CCDirector::sharedDirector()->convertToGL(endPoint);  
 
    CCPoint offSet =ccpSub(beginPoint,endPoint);  
    CCPoint toPoint=ccpAdd(pSprite->getPosition(),offSet);  
    pSprite->setPosition(toPoint);  

bool HelloWorld::init()
{
 bool bRet = false;
 do
 {
  CC_BREAK_IF(! CCLayer::init());
  CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
   "CloseNormal.png",
   "CloseSelected.png",
   this,
   menu_selector(HelloWorld::menuCloseCallback));
  CC_BREAK_IF(! pCloseItem);
  pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 500));

  CCMenuItemImage *pSaveItem = CCMenuItemImage::create(
   "save_1.png",
   "save_2.png",
   this,
   menu_selector(HelloWorld::menuSaveCallback));
  CC_BREAK_IF(! pSaveItem);
  pSaveItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 100, 100));

  CCMenu* pMenu = CCMenu::create(pCloseItem,pSaveItem, NULL);
  pMenu->setPosition(CCPointZero);
  CC_BREAK_IF(! pMenu);
  this->addChild(pMenu, 1);

  CCSize s = CCDirector::sharedDirector()->getWinSize();

  CCSprite* bg = CCSprite::create("bg.png");
  CC_BREAK_IF(!bg);
 &n

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,