2013-12-13 24 views
0

所以我一直有一些問題與cctouchesEnded當一觸之間結束它觸發了所有我的按鈕結束行動。我明白爲什麼會發生這種情況,但爲了解決這個問題,我不確定要做什麼。如果我要爲按鈕創建一個類併爲每個新按鈕創建一個新類的實例,那麼每個按鈕都將是分開的嗎?如果是的話,有沒有一個很好的例子來說明如何使用類在cocos2d中創建簡單的按鈕?任何幫助,將不勝感激。這是我目前如何使用按鈕。ccTouchesEnded已結束所有操作

-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 


    for(UITouch *touch in [event allTouches]) { 
     CGPoint loc = [touch locationInView: [touch view]]; 
     loc = [[CCDirector sharedDirector] convertToGL:loc]; 

     if (CGRectContainsPoint(button2.boundingBox, loc)) {    //Left button pressed 
      [self.character stopAction:self.walkAction]; 
      [self.character setScaleX:-2]; 
      isLeft = YES; 
      isRight = NO; 
      right = NO; 
      left = YES; 
      if(!isRunning && !isJumping && !isSpin) 
      { 
       // [self.character stopAction:self.jumpAction]; 
       [self.character runAction:self.runAction]; 
       isRunning = YES; 
      } 
      else if(isSpin) 
      { 
       [self.character stopAction:self.runAction]; 
       [self.character stopAction:self.walkAction]; 
       [self.character stopAction:self.jumpAction]; 
       isRunning = NO; 
       isStanding = NO; 
      } 

     } 
     if (CGRectContainsPoint(button.boundingBox, loc)) {    //Right button pressed 
      [self.character stopAction:self.walkAction]; 
      [self.character setScaleX:2]; 
      isLeft = NO; 
      isRight = YES; 
      right = YES; 
      left = NO; 
      if(!isRunning && !isJumping && !isSpin) 
      { 
       [self.character stopAction:self.jumpAction]; 
       [self.character runAction:self.runAction]; 
       isRunning = YES; 
       isStanding = NO; 
      } 
      else if(isSpin) 
      { 
       [self.character stopAction:self.runAction]; 
       [self.character stopAction:self.walkAction]; 
       [self.character stopAction:self.jumpAction]; 
       isRunning = NO; 
       isStanding = NO; 
      } 

     } 

    } 
} 

-(void)ccTouchesMoved:(NSSet *)touchesT withEvent:(UIEvent *)event 
{ 

    NSMutableArray *touches = [NSMutableArray arrayWithCapacity:100]; 
    for (UITouch *tmpTouch in [event allTouches]) { 
     [touches addObject:tmpTouch]; 
    } 

    CGPoint locOne = [[touches objectAtIndex:0] locationInView: [[touches objectAtIndex:0] view]]; 
    locOne = [[CCDirector sharedDirector] convertToGL:locOne]; 

    CGPoint locTwo = ccp(-100, -100); 
    if ([touches count] > 1) { 
     locTwo = [[touches objectAtIndex:1] locationInView: [[touches objectAtIndex:1] view]]; 
     locTwo = [[CCDirector sharedDirector] convertToGL:locTwo]; 
    } 

    CGPoint locThree = ccp(-100, -100); 
    if ([touches count] > 2) { 
     locThree = [[touches objectAtIndex:2] locationInView: [[touches objectAtIndex:2] view]]; 
     locThree = [[CCDirector sharedDirector] convertToGL:locThree]; 
    } 

    if(!(((CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree)))|| ((CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree))))) 
    { 
     if(!isJumping && !isSpin && !isStanding){ 
     [self.character stopAction:self.jumpAction];   //No buttons 
     [self.character stopAction:self.runAction]; 
     [self.character runAction:self.walkAction]; 
     isRunning = NO; 
     isStanding = YES; 
     } 
    } 

    if (!(CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree))) { 
     left = NO; 
    } else {                 //Left button 
     [self.character setScaleX:-2]; 
     left = YES; 
     isLeft = YES; 
     isRight = NO; 
     if(!isRunning && !isJumping && !isSpin && !isShooting) 
     { 
      [self.character stopAction:self.jumpAction]; 
      [self.character stopAction:self.walkAction]; 
      [self.character runAction:self.runAction]; 
      isRunning = YES; 
      isStanding = NO; 
     } 
     else if(isSpin) 
     { 
      [self.character stopAction:self.runAction]; 
      [self.character stopAction:self.walkAction]; 
      [self.character stopAction:self.jumpAction]; 
      isRunning = NO; 
      isStanding = NO; 
     } 

    } 

    if (!(CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree))) { 
     right = NO; 
    } else { 
     [self.character setScaleX:2];          //Right button 
     right = YES; 
     isRight = YES; 
     isLeft = NO; 
     if(!isRunning && !isJumping && !isSpin) 
     { 
      [self.character stopAction:self.jumpAction]; 
      [self.character stopAction:self.walkAction]; 
      [self.character runAction:self.runAction]; 
      isRunning = YES; 
      isStanding = NO; 
     } 
     else if(isSpin) 
     { 
      [self.character stopAction:self.runAction]; 
      [self.character stopAction:self.walkAction]; 
      [self.character stopAction:self.jumpAction]; 
      isRunning = NO; 
      isStanding = NO; 
     } 


    } 

} 
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    right = NO; 
    left = NO;                   //Touch ended 
    isRunning = NO; 
    if(!isJumping){ 
     [self.character stopAction:self.runAction]; 
     [self.character stopAction:self.jumpAction]; 
     if(isStanding && !isSpin){ 
      [self.character stopAction:self.walkAction]; 
      isStanding = NO; 
     } 
     if(!isStanding && !isSpin){ 
      [self.character stopAction:self.runAction]; 
      [self.character runAction:self.walkAction]; 
      isStanding = YES; 
     } 
    } 

} 
+0

一個很好的例子是CCMenuItem沒有?雖然我不明白爲什麼你不能在這種情況下使用CCMenu和三個CCMenuItems ...你有源代碼...應該很容易擴展一個類,或者創建一個擁有良好起點的自己的CCMenu。 – YvesLeBorg

回答

1

我要張貼在一個項目中使用今年早些時候(cocos2d的,Objective-C的),「雙觸摸按鍵」的一個例子。您在創建時將其傳遞給CCNode(例如CCSprite),當您按下它並釋放它時,它將執行一系列操作(請參閱.mm文件)。它會執行向上和向下的選擇器(我正在使用它作爲一個火災按鈕...需要支持按下按鈕)。如果您需要這種方式,您應該可以修改它以執行單一操作。

DoubleActionButton.h

#import <Foundation/Foundation.h> 
    #import "cocos2d.h" 

     // This button executes an action on the NODE, when the button is pressed down and when the 
     // button is released. 

    @interface DoubleActionButton : CCNode <CCTargetedTouchDelegate> 

    +(DoubleActionButton*)buttonWithTarget:(id)target andDownSelector:(SEL)selD andUpSelector:(SEL)selU andNode:(CCNode*)node; 

    @end 

DoubleActionButton.mm

#import "DoubleActionButton.h" 
    #import "SimpleAudioEngine.h" 

    #import "CommonSTL.h" 
    #import "UIConstants.h" 
    #import "UIUtilities.h" 

    @interface DoubleActionButton() 
    { 
     id _target; 
     SEL _selUp; 
     SEL _selDown; 
     CCAction * _actUp; 
     CCAction * _actDown; 
     BOOL _down; 
     BOOL _animating; 
    } 

    @property (nonatomic, assign) id _target; 
    @property (nonatomic, assign) BOOL _down; 
    @property (nonatomic, assign) SEL _selUp; 
    @property (nonatomic, assign) SEL _selDown; 
    @property (nonatomic, retain) CCAction * _actUp; 
    @property (nonatomic, retain) CCAction * _actDown; 
    @property (nonatomic, retain) CCNode * _node; 
    @property (nonatomic, assign) BOOL _animating; 

    @end 

    @implementation DoubleActionButton 

    @synthesize _target; 
    @synthesize _actDown; 
    @synthesize _actUp; 
    @synthesize _selDown; 
    @synthesize _selUp; 
    @synthesize _animating; 
    @synthesize _down; 
    @synthesize _node; 

    #pragma mark Setters 


    #pragma mark Life Cycle 

    -(CCAction*)createDownAction 
    { 
     // specify the visual actions that should occur when a user interacts with the button 
     CCFiniteTimeAction* action0 = [CCCallFunc actionWithTarget:self selector:@selector(buttonDownStart)]; 
     CCFiniteTimeAction* action1 = [CCScaleTo actionWithDuration:0.1 scale:1.1f]; 
     CCFiniteTimeAction* action2 = [CCCallFunc actionWithTarget:self selector:@selector(buttonDownEnd)]; 
     CCSequence* seq = [CCSequence actions:action0,action1,action2,nil]; 
     return seq; 
    } 

    -(CCAction*)createUpAction 
    { 
     // specify the visual actions that should occur when a user interacts with the button 
     CCFiniteTimeAction* action0 = [CCCallFunc actionWithTarget:self selector:@selector(buttonUpStart)]; 
     CCFiniteTimeAction* action1 = [CCScaleTo actionWithDuration:0.1 scale:1.0f]; 
     CCFiniteTimeAction* action2 = [CCCallFunc actionWithTarget:self selector:@selector(buttonUpEnd)]; 
     CCSequence* seq = [CCSequence actions:action0,action1,action2,nil]; 
     return seq; 
    } 


    -(DoubleActionButton*)init 
    { 
     self = [super init]; 
     if(self != nil) 
     { 
      _animating = NO; 
      _down = NO; 
      self._actUp = [self createUpAction]; 
      self._actDown = [self createDownAction]; 
      self._selUp = nil; 
      self._selDown = nil; 
      self._node = nil; 
     } 
     return self; 
    } 

    -(DoubleActionButton*)initButtonWithTarget:(id)target 
          andDownSelector:(SEL)selD 
          andUpSelector:(SEL)selU 
           andNode:(CCNode*)node 
    { 
     self = [super init]; 
     if(self != nil) 
     { 
      self._actUp = [self createUpAction]; 
      self._actDown = [self createDownAction]; 
      self._target = target; 
      self._selDown = selD; 
      self._selUp = selU; 
      self.anchorPoint = ccp(0.5,0.5); 
      self._node = node; 
      [self addChild:node z:100]; 
     } 
     return self; 
    } 



    +(DoubleActionButton*)buttonWithTarget:(id)target 
          andDownSelector:(SEL)selD 
          andUpSelector:(SEL)selU 
           andNode:(CCNode*)node 
    { 
     DoubleActionButton* button = [[DoubleActionButton alloc] initButtonWithTarget:target andDownSelector:selD andUpSelector:selU andNode:node]; 
     [button autorelease]; 
     return button; 
    } 

    -(void)dealloc 
    { 
     self._actDown = nil; 
     self._actUp = nil; 
     self._node = nil; 
     [super dealloc]; 
    } 

    #pragma mark Cocos2D Life Cycle 

    -(void)onEnterTransitionDidFinish 
    { 
     [super onEnterTransitionDidFinish]; 
     [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
    } 

    -(void)onExitTransitionDidStart 
    { 
     [super onExitTransitionDidStart]; 
     [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self]; 
    } 

    #pragma mark General Stuff 
    -(void)buttonDownStart 
    { 
     _animating = YES; 
     _down = YES; 
     if(_selDown && _target) 
     { 
      [_target performSelector:_selDown]; 
     } 

     [[SimpleAudioEngine sharedEngine] playEffect:[UIConstants FILE_AUDIO_BUTTON_CLICK]]; 
    } 

    -(void)buttonDownEnd 
    { 
     _animating = NO; 
    } 

    -(void)buttonUpStart 
    { 
     if(_selUp && _target) 
     { 
      [_target performSelector:_selUp]; 
     } 
     _animating = YES; 
    } 

    -(void)buttonUpEnd 
    { 
     _down = NO; 
     _animating = NO; 
    } 

    #pragma mark Touch events 

    - (BOOL) touchInButton:(UITouch*)touch 
    { 
     CCNode* node = self._node; 
     if(node) 
     { 

      // Is the touch inside node0's AABB? 
      CGPoint touchPt = [UIUtilities TouchToScreen:touch]; 
      CGPoint local = [node convertToNodeSpace:touchPt]; 
      CGRect rect = CGRectMake(touchPt.x, touchPt.y, node.contentSize.width, node.contentSize.height); 
      rect.origin = CGPointZero; 

      if(CGRectContainsPoint(rect, local)) 
      { // Not in the AABB of node0. 
      return YES; 
      } 
     } 
     return NO; 
    } 

    - (BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event 
    { 
     if (!_animating && !_down && [self touchInButton:touch]) 
     { 
      if (_actDown) 
      { 
       [self stopAllActions]; 
       [self runAction:_actDown]; 
      } 
      return YES; 
     } 

     return NO; 
    } 

    - (void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event 
    { 
     // Finger moved off the button? 
     if (_down && ![self touchInButton:touch]) 
     { // Looks like it. 
      if (_actUp) 
      { 
       [self stopAllActions]; 
       [self runAction:_actUp]; 
      } 
     } 
    } 


    - (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
     if (_actUp && _down) 
     { 
      [self stopAllActions]; 
      [self runAction:_actUp]; 
     } 
    } 

    -(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
     if (_actUp && _down) 
     { 
      [self stopAllActions]; 
      [self runAction:_actUp]; 
     } 
    } 


    @end 

下面是使用它的一個例子:

// Button 1 
    DoubleActionButton* dblBtn; 
    node0 = [CCSprite spriteWithSpriteFrameName:[UIConstants FILE_IMAGE_FIRE_BUTTON_UP]]; 
    [UIUtilities SetSpriteSize:(CCSprite*)node0 toSize:btnMaxSize]; 
    dblBtn = [DoubleActionButton buttonWithTarget:self andDownSelector:@selector(button1Down) andUpSelector:@selector(button1Up) andNode:node0]; 
    dblBtn.position = ccp(leftEdge,botEdge); 
    [self addChild:dblBtn]; 

隨意使用盡可能多的您需要的代碼。

對您有幫助嗎?

+0

謝謝,這正是我需要的! – user1569940