歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> cocos2d中實現觸摸按鈕換圖效果方案

cocos2d中實現觸摸按鈕換圖效果方案

日期:2017/3/1 10:13:18   编辑:Linux編程

主要原理是當TouchBegan時根據按鈕下的坐標把對應按鈕換成按下的效果圖,當TouchMoved時根據移動previousLocationInView坐標取消對應按鈕的按下效果圖,即把按鈕還原成未按下的圖,當TouchEnded時根據抬手的坐標取消對應按鈕的按下效果圖,也即把按鈕還原成未按下的圖,直接上代碼:

  1. -(id) init
  2. {
  3. // always call "super" init
  4. // Apple recommends to re-assign "self" with the "super" return value
  5. if( (self=[super init])) {
  6. [self show];
  7. self.isTouchEnabled=YES;
  8. }
  9. return self;
  10. }
  11. -(void) show
  12. {
  13. CGSize s = [[CCDirector sharedDirector] winSize];
  14. //觸摸數組,用於存放要檢測觸摸的精靈
  15. movableSprites = [[NSMutableArray alloc] init];
  16. [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MineButton.plist"];
  17. //根據一個plist文件名構建CCSpriteFrame對象並添加到內存池中,就是加載要用到的各種圖片
  18. //背景圖
  19. CCSprite *bg = [CCSprite spriteWithFile:@"bg.png"];
  20. bg.position=ccp(s.width/2,s.height/2);
  21. [self addChild:bg z:-1 tag:99];
  22. //levelBt等級按鈕
  23. levelBt=[CCSprite spriteWithSpriteFrameName:@"btnLevel.png"];
  24. levelBt.position=ccp(170,levelBt.contentSize.height/2+50);
  25. [self addChild:levelBt z:1 tag:103];
  26. [movableSprites addObject:levelBt];
  27. //scoreBt按鈕
  28. scoreBt=[CCSprite spriteWithSpriteFrameName:@"btnScore.png"];
  29. scoreBt.position=ccp(230,scoreBt.contentSize.height/2+50);
  30. [self addChild:scoreBt z:1 tag:104];
  31. [movableSprites addObject:scoreBt];
  32. //moreBt按鈕
  33. moreBt=[CCSprite spriteWithSpriteFrameName:@"btnMore.png"];
  34. moreBt.position=ccp(290,moreBt.contentSize.height/2+50);
  35. [self addChild:moreBt z:1 tag:105];
  36. [movableSprites addObject:moreBt];
  37. }
  38. ////////////////////////////////////////////////////////
  39. #pragma mark Touch Method
  40. //更換觸摸協議
  41. -(void) registerWithTouchDispatcher
  42. {
  43. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
  44. //priority參數越小優先級越高
  45. }
  46. -(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
  47. {
  48. BOOL hasTouched = NO;
  49. CGPoint touchlocation = [touch locationInView: [touch view]];
  50. touchlocation =[[CCDirector sharedDirector] convertToGL:touchlocation];
  51. CCSprite * newSprite = nil;
  52. if (newSprite == nil) {
  53. for (CCSprite *sprite in movableSprites) {
  54. if (CGRectContainsPoint(sprite.boundingBox, touchlocation)) {
  55. newSprite = sprite;
  56. break;
  57. }
  58. }
  59. }
  60. int na = [newSprite tag];
  61. switch (na) {
  62. case 103:
  63. CCLOG(@"levelBt");
  64. //換按下的效果圖
  65. [GameHelper changeImageBtn:newSprite imagePath:@"btnLeveled.png"];
  66. hasTouched = YES;
  67. break;
  68. case 104:
  69. CCLOG(@"scoreBt");
  70. //換按下的效果圖
  71. [GameHelper changeImageBtn:newSprite imagePath:@"btnScoreed.png"];
  72. hasTouched = YES;
  73. break;
  74. case 105:
  75. CCLOG(@"moreBt");
  76. //換按下的效果圖
  77. [GameHelper changeImageBtn:newSprite imagePath:@"btnMoreed.png"];
  78. hasTouched = YES;
  79. break;
  80. default:
  81. hasTouched = NO;
  82. break;
  83. }
  84. if (hasTouched==YES) {
  85. CCLOG(@"吞並觸摸事件");
  86. //按中本層的按鈕
  87. return YES;
  88. }
  89. else
  90. {
  91. CCLOG(@"傳遞觸摸事件");
  92. //未按中本層的按鈕
  93. return NO;
  94. }
  95. }
  96. -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
  97. //獲取舊的坐標,即按下時的坐標,移動了就表示取消對應按鈕的動作,那麼就要還原按鈕原圖
  98. CGPoint oldTouchLocation=[touch previousLocationInView:touch.view];
  99. oldTouchLocation=[[CCDirector sharedDirector] convertToGL:oldTouchLocation];
  100. CCSprite * newSprite = nil;
  101. if (newSprite == nil) {
  102. for (CCSprite *sprite in movableSprites) {
  103. if (CGRectContainsPoint(sprite.boundingBox, oldTouchLocation)) {
  104. newSprite = sprite;
  105. break;
  106. }
  107. }
  108. }
  109. int na = [newSprite tag];
  110. switch (na) {
  111. case 103:
  112. CCLOG(@"levelBt");
  113. //換未按下的效果圖
  114. [GameHelper changeImageBtn:newSprite imagePath:@"btnLevel.png"];
  115. break;
  116. case 104:
  117. CCLOG(@"scoreBt");
  118. //換未按下的效果圖
  119. [GameHelper changeImageBtn:newSprite imagePath:@"btnScore.png"];
  120. break;
  121. case 105:
  122. CCLOG(@"moreBt");
  123. //換未按下的效果圖
  124. [GameHelper changeImageBtn:newSprite imagePath:@"btnMore.png"];
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. -(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
  131. {
  132. CGPoint touchlocation = [touch locationInView: [touch view]];
  133. touchlocation =[[CCDirector sharedDirector] convertToGL:touchlocation];
  134. CCSprite * newSprite = nil;
  135. if (newSprite == nil) {
  136. for (CCSprite *sprite in movableSprites) {
  137. if (CGRectContainsPoint(sprite.boundingBox, touchlocation)) {
  138. newSprite = sprite;
  139. break;
  140. }
  141. }
  142. }
  143. int na = [newSprite tag];
  144. switch (na) {
  145. case 103:
  146. CCLOG(@"levelBt");
  147. //換按下的效果圖
  148. [GameHelper changeImageBtn:newSprite imagePath:@"btnLevel.png"];
  149. //執行按鈕觸發的函數
  150. [self showSelectLevelLayer];
  151. break;
  152. case 104:
  153. CCLOG(@"scoreBt");
  154. //換按下的效果圖
  155. [GameHelper changeImageBtn:newSprite imagePath:@"btnScore.png"];
  156. //執行按鈕觸發的函數
  157. [self showScoreLayer];
  158. break;
  159. case 105:
  160. CCLOG(@"moreBt");
  161. //換按下的效果圖
  162. [GameHelper changeImageBtn:newSprite imagePath:@"btnMore.png"];
  163. //執行按鈕觸發的函數
  164. [self showMoreLayer];
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. ///////////////////////////////////////////////////////////
  171. -(void)showSelectLevelLayer
  172. {
  173. }
  174. -(void)showScoreLayer
  175. {
  176. }
  177. -(void)showMoreLayer
  178. {
  179. }
  180. ///////////////////////////////////////////////////////////
以上純屬個人想法,若有更好的方案敬請賜教!
Copyright © Linux教程網 All Rights Reserved