scrollView(下)

这篇继续写scroll,不过这次不是用scrollView这个控件,而是自己编写一段代码实现拖动图片。假想有一张很大的地图,然后屏幕只能显示它的一角,这时候就要通过拖动来查看其他部分。本来是要用scrollView实现这个功能,可用起来始终那么别扭,这不,上午一气之下自己搞了个可以拖动图片的代码。


一、首先在init中添加自己要实现拖动到图片。

  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         CC_BREAK_IF(! CCLayer::init());  
  7.           
  8.         CCSize size = CCDirector::sharedDirector()->getWinSize();  
  9.   
  10.         //选择的图片一定要比screan 大,要不然就会出问题。不过话说回来,你的图片如果比屏幕小,你还拖动干嘛?找茬?  
  11.         sprite = CCSprite::create("map_bg.png");  
  12.         this->addChild(sprite);  
  13.         sprite->setPosition(ccp(size.width/2,size.height/2));  
  14.         sprite->setAnchorPoint(ccp(0.5,0.5));  
  15.   
  16.         //开启触摸  
  17.         setTouchEnabled(true);  
  18.   
  19.         bRet = true;  
  20.     } while (0);  
  21.   
  22.     return bRet;  
  23. }  

二、真正实现拖动图片的代码:
  1. void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)  
  2. {  
  3.     CCSetIterator it = pTouches->begin();  
  4.     CCTouch* touch = (CCTouch*)(*it);  
  5.   
  6.     //获取触摸开始的坐标  
  7.     m_tBeginPos = touch->getLocation();      
  8. }  
  9.   
  10. void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)  
  11. {  
  12.     CCSetIterator it = pTouches->begin();  
  13.     CCTouch* touch = (CCTouch*)(*it);  
  14.   
  15.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  16.   
  17.     CCPoint touchLocation = touch->getLocation();  
  18.     float nMoveY = touchLocation.y - m_tBeginPos.y;  
  19.     float nMoveX = touchLocation.x - m_tBeginPos.x;  
  20.   
  21.     //sprite 的当前坐标  
  22.     CCPoint curPos  = sprite->getPosition();  
  23.     //下一个坐标就是sprite当前坐标+鼠标移动到距离  
  24.     CCPoint nextPos = ccp(curPos.x + nMoveX, curPos.y + nMoveY);  
  25.   
  26.     //最大移动距离!  
  27.     float maxMoveY = sprite->getContentSize().height/2 - size.height/2;  
  28.     float maxMoveX = sprite->getContentSize().width/2 - size.width/2;  
  29.   
  30.     sprite->setPosition(nextPos);  
  31.     m_tBeginPos = touchLocation;  
  32.   
  33.     //判断图片是否超出边界,切记,这下面两句一定要放在上面两句之后  
  34.     HelloWorld::judgeHV(nextPos,maxMoveY);  
  35.     HelloWorld::judgeLR(nextPos,maxMoveX);    
  36. }  

三、judgeHV跟judgeLR是为了防止图片被拖动出边界,出现黑边。
  1. void HelloWorld::judgeHV(CCPoint nextPos,float max)  
  2. {  
  3.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  4.   
  5.     //因为在if里面return是不会影响外部的,只是函数停止执行而已,函数停止了,外面的代码照常运行~  
  6.     if (nextPos.y <(size.height/2-max))  
  7.     {  
  8.         //当移动到最顶端,Y坐标为maxMoveY,不让它动  
  9.         sprite->setPositionY(size.height/2-max);  
  10.         return;  
  11.     }  
  12.   
  13.     if(nextPos.y > (size.height/2 + max))  
  14.     {  
  15.         sprite->setPositionY(size.height/2 + max);  
  16.         return;  
  17.     }  
  18. }  
  19.   
  20. void HelloWorld::judgeLR(CCPoint nextPos,float max)  
  21. {  
  22.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  23.   
  24.     if(nextPos.x < (size.width/2 - max))  
  25.     {  
  26.         sprite->setPositionX(size.width/2 - max);  
  27.         return;  
  28.     }  
  29.   
  30.     if(nextPos.x > (size.width/2 + max))  
  31.     {  
  32.         sprite->setPositionX(size.width/2 + max);  
  33.         return;  
  34.     }     
  35. }  

恩,这样就成功了。简单吧。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值