gpt4 book ai didi

iphone - 使用 cocos2d 处理 CCSprite 触摸的最佳实践

转载 作者:行者123 更新时间:2023-12-03 18:28:35 25 4
gpt4 key购买 nike

大家好。我刚刚开始研究 cocos2d 库。我听说,如果您习惯使用 ActionScript 进行编程,那么这是一个很容易上手的库,并且我发现很多概念确实相似。

我开始查看示例项目(链接的示例游戏here特别有帮助),我发现触摸处理通常不是在CCSprite中完成的。相反,实例化 CCSprite 的 CCLayer 对触摸事件使用react,并迭代它创建的 Sprite 以检测哪个 CCSprite 被触摸(如果有)。

我希望 CCSprite 能够处理它们自己是否被触摸,并调用 up 来通知它已被触摸(如果需要)。/tests/TouchesTest 下的 Paddle 类就是这样做的 - 它自己处理触摸。

所以,我的问题是:什么是最佳实践?在中心位置处理触摸并遍历子级以查看触摸的内容是否更好?或者每个 child 应该处理自己的触摸事件?还是说没关系?

我希望每个 child 处理自己的触摸事件,但我想遵循这方面的最佳实践(如果存在)。谢谢!

最佳答案

我认为这是一个偏好问题,但我喜欢让 Sprite 检测它是否被子类化 CCSprite 所触及。我在 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

希望这对您有所帮助,祝您编码愉快!

添加勾选方法和解释(针对下面斯蒂芬的问题):

为了检查按钮的状态,我有一个 tick: 方法,它基本上会触发每一帧并检查所有按钮的状态。

    -(void)tick:(ccTime)dt {

do my button checks here....

}

我通过调用 spuButton 类中的 isPressed 或 isNotPressed 函数来检查按钮的状态。

for (spuButton *aButton in _fourButtonsArray) {
if ([aButton isNotPressed]) continue; //this button is not pressed
.....otherwise record that it is pressed.....
}

然后我进行同样的检查,看看它是否已发布并做出相应的响应。我这样做是因为我希望能够对多个按钮按下组合使用react,而且我想在按下按钮时执行某些操作,然后在释放按钮时执行其他操作。我使用 ccTouchBegan 和 ccTouchEnded 来更改纹理( Sprite 图像)并相应地更改状态变量。

关于iphone - 使用 cocos2d 处理 CCSprite 触摸的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2900691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com