zoukankan      html  css  js  c++  java
  • 学习笔记之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); 
    }



  • 相关阅读:
    全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
    网易云课堂_程序设计入门-C语言_第六章:数组_2鞍点
    arcgis api for silverlight开发系列之二:缓存图层与动态图层及图层总结 .
    VS2010程序打包操作(超详细的)
    地图三要素
    创业建议
    写代码时,必须注意“异常处理”
    WPF——RenderTransform特效
    MVVM特点、源(数据)与目标(如:控件等)的映射
    使用触发器定义 WPF 控件的行为
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2989544.html
Copyright © 2011-2022 走看看