• 学习笔记之cocos2dx2.1.1实现多个精灵的拖动


    
    

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

    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); 
    }



  • 相关阅读:
    springmvc,springboot单元测试配置
    uboot中ftd命令
    在根文件系统中查看设备树(有助于调试)
    协议类接口
    网络设备接口
    块设备驱动框架
    i2c子系统
    触摸屏
    input子系统
    原子访问、自旋锁、互斥锁、信号量
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2989544.html
Copyright © 2020-2023  润新知