我認爲這是一個偏好問題,但我希望通過繼承CCSprite來讓Sprite檢測它是否被觸及。我在我的CCSprite子類中創建了一個getter方法,它從子類中檢索狀態變量,然後主程序可以相應地執行。
這裏是我的CCSprite子 「spuButton」 的一個例子頭文件:
#import "cocos2d.h"
typedef enum tagButtonState {
kButtonStatePressed,
kButtonStateNotPressed
} ButtonState;
typedef enum tagButtonStatus {
kButtonStatusEnabled,
kButtonStatusDisabled
} ButtonStatus;
@interface spuButton : CCSprite <CCTargetedTouchDelegate> {
@private
ButtonState state;
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;
@end
,這裏是.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 (state == kButtonStateNotPressed) return NO;
if (state == kButtonStatePressed) return YES;
return NO;
}
- (BOOL)isNotPressed {
if (state == kButtonStateNotPressed) return YES;
if (state == kButtonStatePressed) return NO;
return YES;
}
- (id)initWithTexture:(CCTexture2D *)aTexture
{
if ((self = [super initWithTexture:aTexture])) {
state = kButtonStateNotPressed;
}
return self;
}
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit
{
[[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 (state == kButtonStatePressed) return NO;
if (![self containsTouchLocation:touch]) return NO;
if (buttonStatus == kButtonStatusDisabled) return NO;
state = kButtonStatePressed;
[self setTexture:buttonLit];
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 ([self containsTouchLocation:touch]) return;
//if (buttonStatus == kButtonStatusDisabled) return NO;
state = kButtonStateNotPressed;
[self setTexture:buttonNormal];
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
state = kButtonStateNotPressed;
[self setTexture:buttonNormal];
}
@end
希望這有助於和編碼愉快!
加成剔方法和解釋(下面斯蒂芬的問題)的:
要檢查我有一個勾號按鈕的狀態:方法基本上激發每一幀,並檢查我的所有按鈕的狀態。
-(void)tick:(ccTime)dt {
do my button checks here....
}
我通過調用isPressed或isNotPressed功能那是我spuButton類的一部分,檢查我的按鈕的狀態。
for (spuButton *aButton in _fourButtonsArray) {
if ([aButton isNotPressed]) continue; //this button is not pressed
.....otherwise record that it is pressed.....
}
然後我做同樣的檢查,看它是否被釋放並作出相應的響應。我這樣做是因爲我想能夠對多個按鈕組合進行反應,另外我想在按下按鈕時執行某些操作,然後在釋放時執行其他操作。我使用ccTouchBegan和ccTouchEnded來更改紋理(精靈圖像)並相應地更改狀態變量。
我想我應該在一般情況下添加一些最佳實踐(例如,我們應該在父級調用「schedule」還是讓每個子級自己處理)。 – donkim 2010-05-24 22:54:32