2010-10-24 39 views
1

我正在寫一個應用程序的iPhone使用cocos2d在哪裏我有4個按鈕的精靈。我想通過使用2個不同的.png文件代表按下/未按下的狀態來實現按鈕功能。我認爲這會比使用動畫更好。除了檢測何時按下和釋放按鈕,我還需要檢測何時發生雙按鍵組合按鍵(如和絃)。我甚至可能把這個轉發到3和4按鈕組合。我想要實現的另一個功能是檢測是否從按鈕內發出輕彈,並用另一個動作序列作出響應。同步雪碧觸摸檢測 - iPhone和cocos2d

什麼是最佳的方法呢?我會使用四個按鈕的邊界矩形或精靈(它們是否在方形陣列中)併吞下觸摸?那麼我會使用較小的精靈rects來確定觸摸發生在哪一個?或者我會讓單個sprite rects進行檢測,然後讓他們檢查其他按鈕是否也被按下,然後同時釋放?我會使用Bool狀態變量用於這兩種方式嗎?

我已經使用CCMenu和CCMenuitem,通過爲每個按鈕精靈製作一個Menu/Menu-item,然而,我無法弄清楚如何檢測一個菜單中的菜單項是否被按下並釋放,同時按下並釋放了不同菜單中的菜單項。我使用了單獨的菜單,因爲如果我使用多個菜單項相同的菜單,則一次只能按一個菜單項。我說它工作得很好,這意味着它可以在單臺和多臺印刷機上工作,但它會將倍數視爲兩次連續印刷,而不是同時印刷。 IE瀏覽器:單次按下導致運行一個單一的動作序列(按照預期),同時按下按鍵組合導致爲每個按鈕運行單獨的按鈕動作序列,就好像我分別按下它們一樣(我希望它運行一個特定的動作序列到那個按鈕組合)。這就是爲什麼我決定嘗試實現我自己的按鈕系統而不是使用CCMenu系統可能更好。我是否正在擺脫CCMenu?

對不起,很長的描述,但它確實說具體。 B)

任何有關其他人如何實現或將實現這一點的深刻見解將不勝感激!

+0

沒有人有什麼想法嗎?如果可以的話請幫忙。 – Mark7777G 2010-10-30 16:44:14

+0

對不起,我在這裏有點忙。我也可以推薦你嘗試複製並粘貼到gamedev.stackexchange.com這個問題?它會得到更多的遊戲意見:)道歉。 – 2010-11-04 09:31:54

+0

也試過了,謝謝。 – Mark7777G 2010-11-08 01:10:32

回答

2

下面是我如何實現這一點,以防其他人試圖做到這一點。

spuButton.h(一CCSprite子類)

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

typedef enum tagButtonState { 
    kButtonStatePressed, 
    kButtonStateNotPressed 
} ButtonState; 

typedef enum tagButtonStatus { 
    kButtonStatusEnabled, 
    kButtonStatusDisabled 
} ButtonStatus; 

@interface spuButton : CCSprite <CCTargetedTouchDelegate> { 
@private 
    ButtonState buttonState; 
    CCTexture2D *buttonNormal; 
    CCTexture2D *buttonLit; 
    ButtonStatus buttonStatus; 
} 

@property(nonatomic, readonly) CGRect rect; 

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture; 

- (void)setNormalTexture:(CCTexture2D *)normalTexture; 
- (void)setLitTexture:(CCTexture2D *)litTexture; 
- (BOOL)isPressed; 
- (BOOL)isNotPressed; 
- (void)makeDisabled; 
- (void)makeEnabled; 
- (BOOL)isEnabled; 
- (BOOL)isDisabled; 
- (void)makeLit; 
- (void)makeNormal; 
- (void)dealloc; 

@end 

spuButton.m

#import "spuButton.h" 
#import "cocos2d.h" 

@implementation spuButton 

- (CGRect)rect { 
    CGSize s = [self.texture contentSize]; 
    return CGRectMake(-s.width/2, -s.height/2, s.width, s.height); 
} 

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture { 
    return [[[self alloc] initWithTexture:normalTexture] autorelease]; 
} 

- (void)setNormalTexture:(CCTexture2D *)normalTexture { 
    buttonNormal = normalTexture; 
} 
- (void)setLitTexture:(CCTexture2D *)litTexture { 
    buttonLit = litTexture; 
} 

- (BOOL)isPressed { 
    if (buttonState== kButtonStateNotPressed) return NO; 
    if (buttonState== kButtonStatePressed) return YES; 
    return NO; 
} 

- (BOOL)isNotPressed { 
    if (buttonState== kButtonStateNotPressed) return YES; 
    if (buttonState== kButtonStatePressed) return NO; 
    return YES; 
} 

- (void)makeDisabled { 
    buttonStatus = kButtonStatusDisabled; 
    buttonState= kButtonStateNotPressed; 
    [self makeNormal]; 
} 
- (void)makeEnabled { 
    buttonStatus = kButtonStatusEnabled; 
    buttonState= kButtonStateNotPressed; 
    [self makeNormal]; 
} 

- (BOOL)isEnabled { 
    if (buttonStatus== kButtonStatusDisabled) return NO; 
    if (buttonStatus== kButtonStatusEnabled) return YES; 
    return NO; 
} 

- (BOOL)isDisabled { 
    if (buttonStatus== kButtonStatusEnabled) return NO; 
    if (buttonStatus== kButtonStatusDisabled) return YES; 
    return YES; 
} 

- (void)makeLit { 
    [self setTexture:buttonLit]; 
} 

- (void)makeNormal { 
    [self setTexture:buttonNormal]; 
} 

- (id)initWithTexture:(CCTexture2D *)aTexture { 
    if ((self = [super initWithTexture:aTexture])) {  
     buttonState = kButtonStateNotPressed; 
     buttonStatus = kButtonStatusEnabled; 
    } 
    return self; 
} 

- (void)onEnter { 
    if (buttonStatus == kButtonStatusDisabled) return; 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO]; 
    [super onEnter]; 
} 

- (void)onExit { 
    if (buttonStatus == kButtonStatusDisabled) return; 
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; 
    [super onExit]; 
} 

- (BOOL)containsTouchLocation:(UITouch *)touch { 
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]); 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    if (buttonStatus == kButtonStatusDisabled) return NO; 
    if (buttonState== kButtonStatePressed) return NO; 
    if (![self containsTouchLocation:touch]) return NO; 

    buttonState= kButtonStatePressed; 
    [self makeLit]; 

    return YES; 
} 

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { 
    // If it weren't for the TouchDispatcher, you would need to keep a reference 
    // to the touch from touchBegan and check that the current touch is the same 
    // as that one. 
    // Actually, it would be even more complicated since in the Cocos dispatcher 
    // you get NSSets instead of 1 UITouch, so you'd need to loop through the set 
    // in each touchXXX method. 

    if (buttonStatus == kButtonStatusDisabled) return; 
    if ([self containsTouchLocation:touch]) return; 

    buttonState= kButtonStateNotPressed; 
    [self makeNormal]; 
} 

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 
    if (buttonStatus == kButtonStatusDisabled) return; 

    buttonState= kButtonStateNotPressed; 
    [self makeNormal]; 
} 

- (void)dealloc { 
    [buttonNormal release]; 
    [buttonLit release]; 
    [super dealloc]; 
} 

@end 

HelloWorldScene.m中(只是我滴答:方法來讓我的其他功能,從混淆的例子)

-(void)tick:(ccTime)dt { 
    if ([[_otherControlsArray objectAtIndex:0] wasPressed]) { 
     [[_otherControlsArray objectAtIndex:0] setWasPressed:NO]; 
     [self removeChild:[_otherControlsArray objectAtIndex:0] cleanup:YES]; 
     [self addChild:[_otherControlsArray objectAtIndex:1]]; 
     NSLog(@"Play"); 

     _gameHasNotBeenPlayedYet = NO; 
     Snarfle_s_PowerUPAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
     [delegate makeNotPaused]; 
     [self gameLogic]; 
    } 

    if (_gameHasNotBeenPlayedYet) { 
     return; 
    } 

    if (_buttonsPressedAndReleased > 0) { //respond to button(s) released and reset 
     NSLog(@"Buttons Pressed and Released-->%d",_buttonsPressedAndReleased); 
     if ([self checkButtons:_buttonsPressedAndReleased]); 
     _buttonsPressed = 0; 
     _buttonsPressedAndReleased = 0; 

     return; 
    } 
    if (_buttonsPressed <= 4) { // two buttons have not already been pressed 
     for (spuButton *aButton in _fourButtonsArray) { 
      if ([aButton isNotPressed]) continue; //this button is not pressed 
      if (_buttonsPressed == 0) { //this button is pressed and no other buttons have been pressed 
       _buttonsPressed = aButton.tag; 
       continue; 
      } 
      //this button is pressed while another has been pressed 
      //figure out which two buttons have been pressed 
      if (_buttonsPressed == 1) { //red plus another 
       switch (aButton.tag) { 
        case 2: //blue 
         _buttonsPressed = 5; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 3: //green 
         _buttonsPressed = 6; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 4: //yellow 
         _buttonsPressed = 7; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         break; 
        default: 
         _buttonsPressed = 1; 
         break; 
       } 
      } 
      if (_buttonsPressed == 2) { //blue plus another 
       switch (aButton.tag) { 
        case 1: //red 
         _buttonsPressed = 5; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 3: //green 
         _buttonsPressed = 8; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 4: //yellow 
         _buttonsPressed = 9; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         break; 
        default: 
         _buttonsPressed = 2; 
         break; 
       } 
      } 
      if (_buttonsPressed == 3) { //green plus another 
       switch (aButton.tag) { 
        case 1: //red 
         _buttonsPressed = 6; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 2: //blue 
         _buttonsPressed = 8; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
         break; 
        case 4: //yellow 
         _buttonsPressed = 10; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         break; 
        default: 
         _buttonsPressed = 3; 
         break; 
       } 
      } 
      if (_buttonsPressed == 4) { //yellow plus another 
       switch (aButton.tag) { 
        case 1: //red 
         _buttonsPressed = 7; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         break; 
        case 2: //blue 
         _buttonsPressed = 9; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
         break; 
        case 3: //green 
         _buttonsPressed = 10; 
         [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
         [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
         break; 
        default: 
         _buttonsPressed = 4; 
         break; 
       } 
      } 
      if (_buttonsPressed > 4) break; //more than one has been pressed and identified 
     } 
    } 
    //now we know what buttons have been pressed now check to see if they have been released 
    //if more than one has been pressed disable the other two 
    //also if more than one has been pressed and one of them gets released disable the released one but keep it lit 
    switch (_buttonsPressed) { 
     case 1: //red 
      if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) _buttonsPressedAndReleased = 1; 
      break; 
     case 2: //blue 
      if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) _buttonsPressedAndReleased = 2; 
      break; 
     case 3: //green 
      if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) _buttonsPressedAndReleased = 3; 
      break; 
     case 4: //yellow 
      if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) _buttonsPressedAndReleased = 4; 
      break; 
     case 5: //red & blue 
      if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:1] isNotPressed])) _buttonsPressedAndReleased = 5; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:0] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:1] makeLit]; 
       } 
      } 
      break; 
     case 6: //red & green 
      if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 6; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:0] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:2] makeLit]; 
       } 
      } 
      break; 
     case 7: //red & yellow 
      if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 7; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:0] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:0] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:3] makeLit]; 
       } 
      } 
      break; 
     case 8: //blue & green 
      if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 8; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:1] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:2] makeLit]; 
       } 
      } 
      break; 
     case 9: //blue & yellow 
      if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 9; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:1] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:1] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:3] makeLit]; 
       } 
      } 
      break; 
     case 10: //green & yellow 
      if (([[_fourButtonsArray objectAtIndex:2] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 10; 
      else { 
       if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:2] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:2] makeLit]; 
       } 
       if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) { 
        [[_fourButtonsArray objectAtIndex:3] makeDisabled]; 
        [[_fourButtonsArray objectAtIndex:3] makeLit]; 
       } 
      } 
      break; 
     default: 
      _buttonsPressedAndReleased = 0; 
      break; 
    } 
}