Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用cocos2d处理CCSprite触摸的最佳实践_Iphone_Ipad_Cocos2d Iphone - Fatal编程技术网

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

Iphone 使用cocos2d处理CCSprite触摸的最佳实践,iphone,ipad,cocos2d-iphone,Iphone,Ipad,Cocos2d Iphone,大家好。我刚开始查看cocos2d库。我听说,若你们习惯于用ActionScript编程,那个么这是一个很容易进入的库,我发现很多概念确实是相似的 我开始查看示例项目(链接的示例游戏特别有用),我发现触摸处理通常不是在CCSprite中完成的。相反,实例化CCSprite的CCLayer会对触摸事件作出反应,并通过创建的sprite进行迭代,以检测触摸了哪个CCSprite(如果有) 我希望ccsprite自己处理是否被触摸过,并打电话通知它被触摸过(如果需要)。在/tests/TouchesT

大家好。我刚开始查看cocos2d库。我听说,若你们习惯于用ActionScript编程,那个么这是一个很容易进入的库,我发现很多概念确实是相似的

我开始查看示例项目(链接的示例游戏特别有用),我发现触摸处理通常不是在CCSprite中完成的。相反,实例化CCSprite的CCLayer会对触摸事件作出反应,并通过创建的sprite进行迭代,以检测触摸了哪个CCSprite(如果有)

我希望ccsprite自己处理是否被触摸过,并打电话通知它被触摸过(如果需要)。在/tests/TouchesTest下找到的
patle
类就是这样做的-它自己处理触摸

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


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

我认为这是一个偏好的问题,但我喜欢让精灵检测它是否被子类化CCSprite所触动。我在CCSprite子类中创建了一个getter方法,该方法从子类中检索状态变量,然后主程序可以相应地执行操作

下面是我的CCSprite子类“spuButton”的头文件示例:

希望这有助于和快乐的编码

增加勾选方法和解释(针对Stephan的以下问题):

要检查按钮的状态,我有一个勾号:方法,它基本上触发每一帧并检查我所有按钮的状态

    -(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.....
}

然后我做同样的检查,看看它是否已经发布,并做出相应的回应。我这样做是因为我想能够对多个按键组合做出反应,而且我想在按下按钮时做一些事情,然后在释放按钮时做一些其他事情。我使用CCtouchBegind和ccTouchEnded来更改纹理(精灵图像)并相应地更改状态变量。

只是添加到此线程。Mark还提供了如何实例化SPU按钮的示例,这很有帮助:

您还可以在“修改此示例”中输入普通按钮图像和亮起按钮图像,如下所示:

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture lit:(CCTexture2D *)litTexture
然后对以下对象执行相同的操作:

- (id)initWithTexture:(CCTexture2D *)normalTexture lit:(CCTexture2D *)litTexture
在此方法中,可以设置两种纹理:

[self setNormalTexture:normalTexture];
[self setLitTexture:litTexture];

这是我的解决方案,基于CCSprite,希望对某些人有用

这是控制对象(如播放器或其他东西)的协议:

@class AGSensitiveButton;
@协议AgSensitiveButton控制对象协议
@必需的
-(无效)SensitiveButton按下:(AGSensitiveButton*)S按钮;
-(void)sensitiveButton上键:(AGSensitiveButton*)S按钮;
@可选的
-(void)SensitiveTouchButton按下:(AGSensitiveButton*)按钮时间:(ccTime)按下时间;
@结束
.h文件:

#import "CCSprite.h"
#import "cocos2d.h"
#import "AGSensitiveButtonControlledObjectProtocol.h"

typedef enum {
    AGSensitiveButtonStateNormal = 0,
    AGSensitiveButtonStateHighlighted,
    AGSensitiveButtonStateDisabled
} AGSensitiveButtonState;

@interface AGSensitiveButton : CCSprite <CCTargetedTouchDelegate>

@property (nonatomic, assign, getter = isEnabled) BOOL enabled;
@property (nonatomic, assign) ccTime maximumTouchDuration;
@property (nonatomic, weak) id <AGSensitiveButtonControlledObjectProtocol> controlledObject;
@property (nonatomic, copy) void (^touchDownHandler)();
@property (nonatomic, copy) void (^touchUpHandler)();

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
             controllerObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject
             touchDownHandler:(void(^)(void))touchDownHandler
               touchUpHandler:(void(^)(void))touchUpHandler;

- (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state;

- (BOOL)isHighlighted;

@end
#导入“CCSprite.h”
#导入“cocos2d.h”
#导入“AGSensitiveButtonControlledObjectProtocol.h”
类型定义枚举{
AGSENSTIVEBUTTONSTATENORMAL=0,
AGSENSTIVEBUTTINSTATE突出显示,
AGSENSTIVEBUTTONSTATEDISABLED
}AGSensitiveButtonState;
@界面AGSENSTIVEBUTTON:CCSprite
@属性(非原子,赋值,getter=isEnabled)BOOL已启用;
@属性(非原子,赋值)ccTime maximumTouchDuration;
@属性(非原子,弱)id受控对象;
@属性(非原子,副本)无效(^touchDownHandler)();
@属性(非原子,副本)无效(^touchUpHandler)();
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture;
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture
禁用织物:(CCTexture2D*)禁用织物;
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture
controllerObject:(id)ControlleObject;
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture
disabledtexture:(CCTexture2D*)disabledtexture
受控对象:(id)受控对象;
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture
disabledtexture:(CCTexture2D*)disabledtexture
受控对象:(id)受控对象
触地处理程序:(void(^)(void))触地处理程序
touchUpHandler:(void(^)(void))touchUpHandler;
-(void)setTexture:(CCTexture2D*)纹理用于状态:(AGSensitiveButtonState)状态;
-(BOOL)isHighlighted;
@结束
implementation.m文件:

#import "AGSensitiveButton.h"

@interface AGSensitiveButton ()
@property (nonatomic, assign) AGSensitiveButtonState state;
@property (nonatomic, strong) NSDictionary *stateTextures;
@property (nonatomic, assign) ccTime currentTouchTime;
@end

@implementation AGSensitiveButton

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                        controllerObject:nil];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:disabledTexture
                        controlledObject:nil];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
             controllerObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:nil
                        controlledObject:controlledObject];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:disabledTexture
                        controlledObject:controlledObject
                        touchDownHandler:NULL
                          touchUpHandler:NULL];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject
             touchDownHandler:(void(^)(void))touchDownHandler
               touchUpHandler:(void(^)(void))touchUpHandler {
    AGSensitiveButton *button = [[self alloc] initWithTexture:normalTexture
                                                         rect:CGRectMake(0.0, 0.0, normalTexture.contentSize.width, normalTexture.contentSize.height)];
    [button setTexture:normalTexture forState:AGSensitiveButtonStateNormal];
    [button setTexture:highTexture forState:AGSensitiveButtonStateHighlighted];
    [button setTexture:disabledTexture forState:AGSensitiveButtonStateDisabled];
    button.controlledObject = controlledObject;
    button.touchDownHandler = touchDownHandler;
    button.touchUpHandler = touchUpHandler;
    return button;
}

- (void)setEnabled:(BOOL)enabled {
    [self setupNewState:enabled ? AGSensitiveButtonStateNormal : AGSensitiveButtonStateDisabled];
}

- (BOOL)isEnabled {
    return (self.state != AGSensitiveButtonStateDisabled);
}

- (BOOL)isHighlighted {
    return (self.state == AGSensitiveButtonStateHighlighted);
}

- (void)toggleTextureForCurrentState {
    CCTexture2D *textureToSet = [self.stateTextures objectForKey:[NSNumber numberWithInteger:self.state]];
    if (textureToSet) {
        self.texture = textureToSet;
        self.textureRect = CGRectMake(0.0, 0.0, textureToSet.contentSize.width, textureToSet.contentSize.height);
    }
}

- (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state {
    NSMutableDictionary *newStates = self.stateTextures.mutableCopy;
    if (texture) {
        [newStates setObject:texture forKey:[NSNumber numberWithInteger:state]];
    } else {
        [newStates removeObjectForKey:[NSNumber numberWithInteger:state]];
    }
    self.stateTextures = newStates.copy;
}

- (NSDictionary *)stateTextures {
    if (!_stateTextures) {
        _stateTextures = [[NSDictionary alloc] init];
    }
    return _stateTextures;
}

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

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

- (void)update:(ccTime)dt {
    if ((self.state == AGSensitiveButtonStateHighlighted) && (self.maximumTouchDuration)) {
        self.currentTouchTime+=dt;
        if (self.currentTouchTime >= self.maximumTouchDuration) {
            [self ccTouchEnded:nil withEvent:nil];
    } else {
        if ([self.controlledObject respondsToSelector:@selector(sensitiveTouchButtonKeepPressed:forTime:)]) {
            [self.controlledObject sensitiveTouchButtonKeepPressed:self forTime:self.currentTouchTime];
        }
    }
    }
}

- (CGRect)rectForTouches {
    return CGRectMake(-self.contentSize.width/2, -self.contentSize.height/2,
                      self.contentSize.width, self.contentSize.height);
}

- (void)forwardTouchDownEventIntoHandlers {
    if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchDown:)]) {
        [self.controlledObject sensitiveButtonTouchDown:self];
    }
    if (self.touchDownHandler) {
        self.touchDownHandler();
    }
}

- (void)forwardTouchUpEventIntoHandlers {
    if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchUp:)]) {
        [self.controlledObject sensitiveButtonTouchUp:self];
    }
    if (self.touchUpHandler) {
        self.touchUpHandler();
    }
}

- (void)setupNewState:(AGSensitiveButtonState)state {
    if (self.state != state) {
        switch (state) {
            case AGSensitiveButtonStateHighlighted: {
                [self forwardTouchDownEventIntoHandlers];
                break;
            }
            default: {
                if (self.state == AGSensitiveButtonStateHighlighted) {
                    [self forwardTouchUpEventIntoHandlers];
                }
                break;
            }
        }
        self.state = state;
        [self toggleTextureForCurrentState];
    }
}

#pragma mark - CCTargetedTouchDelegate

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    if ((self.state != AGSensitiveButtonStateNormal) || (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch]))) {
        return NO;
    }
    self.currentTouchTime = 0.0;
    [self setupNewState:AGSensitiveButtonStateHighlighted];
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.state == AGSensitiveButtonStateHighlighted) {
        if (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch])) {
            [self ccTouchEnded:touch withEvent:event];
        }
    }
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.state == AGSensitiveButtonStateHighlighted) {
        [self setupNewState:AGSensitiveButtonStateNormal];
    }
}

@end
#导入“AGSensitiveButton.h”
@界面AGSENSTIVEBUTTON()
@属性(非原子,赋值)AgSensiveButtonState状态;
@属性(非原子,强)NSDictionary*状态纹理;
@属性(非原子,赋值)ccTime currentTouchTime;
@结束
@实现AgSensiveButton
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture{
返回[self buttonWithNormalTexture:normalTexture
highlightedTexture:highTexture
控制器对象:无];
}
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
highlightedTexture:(CCTexture2D*)highTexture
disabledtexture:(CCTexture2D*)disabledtexture{
返回[self buttonWithNormalTexture:normalTexture
highlightedTexture:highTexture
disabledtexture:disabledtexture
受控对象:无];
}
+(id)带normalTexture的按钮:(CCTexture2D*)normalTexture
@class AGSensitiveButton;

@protocol AGSensitiveButtonControlledObjectProtocol <NSObject>
@required
- (void)sensitiveButtonTouchDown:(AGSensitiveButton *)sButton;
- (void)sensitiveButtonTouchUp:(AGSensitiveButton *)sButton;
@optional
- (void)sensitiveTouchButtonKeepPressed:(AGSensitiveButton *)sButton forTime:(ccTime)pressTime;
@end
#import "CCSprite.h"
#import "cocos2d.h"
#import "AGSensitiveButtonControlledObjectProtocol.h"

typedef enum {
    AGSensitiveButtonStateNormal = 0,
    AGSensitiveButtonStateHighlighted,
    AGSensitiveButtonStateDisabled
} AGSensitiveButtonState;

@interface AGSensitiveButton : CCSprite <CCTargetedTouchDelegate>

@property (nonatomic, assign, getter = isEnabled) BOOL enabled;
@property (nonatomic, assign) ccTime maximumTouchDuration;
@property (nonatomic, weak) id <AGSensitiveButtonControlledObjectProtocol> controlledObject;
@property (nonatomic, copy) void (^touchDownHandler)();
@property (nonatomic, copy) void (^touchUpHandler)();

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
             controllerObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject;

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject
             touchDownHandler:(void(^)(void))touchDownHandler
               touchUpHandler:(void(^)(void))touchUpHandler;

- (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state;

- (BOOL)isHighlighted;

@end
#import "AGSensitiveButton.h"

@interface AGSensitiveButton ()
@property (nonatomic, assign) AGSensitiveButtonState state;
@property (nonatomic, strong) NSDictionary *stateTextures;
@property (nonatomic, assign) ccTime currentTouchTime;
@end

@implementation AGSensitiveButton

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                        controllerObject:nil];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:disabledTexture
                        controlledObject:nil];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
             controllerObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:nil
                        controlledObject:controlledObject];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject {
    return [self buttonWithNormalTexture:normalTexture
                      highlightedTexture:highTexture
                         disabledtexture:disabledTexture
                        controlledObject:controlledObject
                        touchDownHandler:NULL
                          touchUpHandler:NULL];
}

+ (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture
           highlightedTexture:(CCTexture2D *)highTexture
              disabledtexture:(CCTexture2D *)disabledTexture
             controlledObject:(id <AGSensitiveButtonControlledObjectProtocol>)controlledObject
             touchDownHandler:(void(^)(void))touchDownHandler
               touchUpHandler:(void(^)(void))touchUpHandler {
    AGSensitiveButton *button = [[self alloc] initWithTexture:normalTexture
                                                         rect:CGRectMake(0.0, 0.0, normalTexture.contentSize.width, normalTexture.contentSize.height)];
    [button setTexture:normalTexture forState:AGSensitiveButtonStateNormal];
    [button setTexture:highTexture forState:AGSensitiveButtonStateHighlighted];
    [button setTexture:disabledTexture forState:AGSensitiveButtonStateDisabled];
    button.controlledObject = controlledObject;
    button.touchDownHandler = touchDownHandler;
    button.touchUpHandler = touchUpHandler;
    return button;
}

- (void)setEnabled:(BOOL)enabled {
    [self setupNewState:enabled ? AGSensitiveButtonStateNormal : AGSensitiveButtonStateDisabled];
}

- (BOOL)isEnabled {
    return (self.state != AGSensitiveButtonStateDisabled);
}

- (BOOL)isHighlighted {
    return (self.state == AGSensitiveButtonStateHighlighted);
}

- (void)toggleTextureForCurrentState {
    CCTexture2D *textureToSet = [self.stateTextures objectForKey:[NSNumber numberWithInteger:self.state]];
    if (textureToSet) {
        self.texture = textureToSet;
        self.textureRect = CGRectMake(0.0, 0.0, textureToSet.contentSize.width, textureToSet.contentSize.height);
    }
}

- (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state {
    NSMutableDictionary *newStates = self.stateTextures.mutableCopy;
    if (texture) {
        [newStates setObject:texture forKey:[NSNumber numberWithInteger:state]];
    } else {
        [newStates removeObjectForKey:[NSNumber numberWithInteger:state]];
    }
    self.stateTextures = newStates.copy;
}

- (NSDictionary *)stateTextures {
    if (!_stateTextures) {
        _stateTextures = [[NSDictionary alloc] init];
    }
    return _stateTextures;
}

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

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

- (void)update:(ccTime)dt {
    if ((self.state == AGSensitiveButtonStateHighlighted) && (self.maximumTouchDuration)) {
        self.currentTouchTime+=dt;
        if (self.currentTouchTime >= self.maximumTouchDuration) {
            [self ccTouchEnded:nil withEvent:nil];
    } else {
        if ([self.controlledObject respondsToSelector:@selector(sensitiveTouchButtonKeepPressed:forTime:)]) {
            [self.controlledObject sensitiveTouchButtonKeepPressed:self forTime:self.currentTouchTime];
        }
    }
    }
}

- (CGRect)rectForTouches {
    return CGRectMake(-self.contentSize.width/2, -self.contentSize.height/2,
                      self.contentSize.width, self.contentSize.height);
}

- (void)forwardTouchDownEventIntoHandlers {
    if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchDown:)]) {
        [self.controlledObject sensitiveButtonTouchDown:self];
    }
    if (self.touchDownHandler) {
        self.touchDownHandler();
    }
}

- (void)forwardTouchUpEventIntoHandlers {
    if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchUp:)]) {
        [self.controlledObject sensitiveButtonTouchUp:self];
    }
    if (self.touchUpHandler) {
        self.touchUpHandler();
    }
}

- (void)setupNewState:(AGSensitiveButtonState)state {
    if (self.state != state) {
        switch (state) {
            case AGSensitiveButtonStateHighlighted: {
                [self forwardTouchDownEventIntoHandlers];
                break;
            }
            default: {
                if (self.state == AGSensitiveButtonStateHighlighted) {
                    [self forwardTouchUpEventIntoHandlers];
                }
                break;
            }
        }
        self.state = state;
        [self toggleTextureForCurrentState];
    }
}

#pragma mark - CCTargetedTouchDelegate

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    if ((self.state != AGSensitiveButtonStateNormal) || (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch]))) {
        return NO;
    }
    self.currentTouchTime = 0.0;
    [self setupNewState:AGSensitiveButtonStateHighlighted];
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.state == AGSensitiveButtonStateHighlighted) {
        if (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch])) {
            [self ccTouchEnded:touch withEvent:event];
        }
    }
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.state == AGSensitiveButtonStateHighlighted) {
        [self setupNewState:AGSensitiveButtonStateNormal];
    }
}

@end