HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 使用cocos2d处理触摸CCSprite的最佳做法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嘿,所有我刚刚开始研究cocos2d库.我听说过,如果你习惯于在ActionScript中进行编程,那么这是一个简单的库,我发现很多概念确实是类似的.

我开始查看示例项目(与here相关的示例游戏特别有用),我看到处理触摸通常不是在CCSprite中完成的.相反,实例化CCSprites的CCLayer对触摸事件做出反应,并遍历通过它创建的精灵来检测哪个CCSprite被触摸(如果有的话).

我想让CCSprites处理他们是否被感动了,并呼吁通知它被触动(如果需要的话).在/ tests / TouchesTest下找到的Paddle类只是这样 – 它自己处理触摸.

所以,我的问题是:这是最好的做法?在中央位置处理触摸是否更好,并通过儿童迭代看到被触动的内容?还是应该每个孩子处理自己的触摸事件?还是没关系?

我更喜欢每个孩子处理自己的触摸事件,但我想遵循最佳做法(如果存在).谢谢!

解决方法

我认为这是一个偏好的问题,但我喜欢让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

希望这有助于快乐编码!

添加滴答法和解释(对于下面的Stephan的问题):

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

-(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来更改纹理(sprite图像)并相应地更改状态变量.

大佬总结

以上是大佬教程为你收集整理的iphone – 使用cocos2d处理触摸CCSprite的最佳做法全部内容,希望文章能够帮你解决iphone – 使用cocos2d处理触摸CCSprite的最佳做法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: