ccTouchesEnded结束所有操作

时间:2013-12-13 21:08:38

标签: cocos2d-iphone

所以我在cctouchesEnded遇到了一些问题,当一次触摸结束时触发了我所有按钮的结束动作。我理解为什么会这样,但为了解决这个问题我不知道该怎么做。如果我要为按钮创建一个类并为每个新按钮创建一个类的新实例,那么每个cctoucheEnded会分开吗?如果是这样,有一个很好的例子,说明如何使用类在cocos2d中创建简单的按钮?任何帮助,将不胜感激。这就是我目前使用按钮的方式。

  -(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {


    for(UITouch *touch in [event allTouches]) {
        CGPoint loc = [touch locationInView: [touch view]];
        loc = [[CCDirector sharedDirector] convertToGL:loc];

        if (CGRectContainsPoint(button2.boundingBox, loc)) {                //Left button pressed
            [self.character stopAction:self.walkAction];
            [self.character setScaleX:-2];
            isLeft = YES;
            isRight = NO;
            right = NO;
            left = YES; 
            if(!isRunning && !isJumping && !isSpin)
            {
               // [self.character stopAction:self.jumpAction];
                [self.character runAction:self.runAction];
                isRunning = YES;
            }
            else if(isSpin)
            {
                [self.character stopAction:self.runAction];
                [self.character stopAction:self.walkAction];
                [self.character stopAction:self.jumpAction];
                isRunning = NO;
                isStanding = NO;
            }

        }
        if (CGRectContainsPoint(button.boundingBox, loc)) {             //Right button pressed
            [self.character stopAction:self.walkAction];
            [self.character setScaleX:2];
            isLeft = NO;
            isRight = YES;
            right = YES;
            left = NO;
            if(!isRunning && !isJumping && !isSpin)
            {
                [self.character stopAction:self.jumpAction];
                [self.character runAction:self.runAction];
                isRunning = YES;
                isStanding = NO;
            }
            else if(isSpin)
            {
                [self.character stopAction:self.runAction];
                [self.character stopAction:self.walkAction];
                [self.character stopAction:self.jumpAction];
                isRunning = NO;
                isStanding = NO;
            }

        }

    }
}

-(void)ccTouchesMoved:(NSSet *)touchesT withEvent:(UIEvent *)event
{

    NSMutableArray *touches = [NSMutableArray arrayWithCapacity:100];
    for (UITouch *tmpTouch in [event allTouches]) {
        [touches addObject:tmpTouch];
    }

    CGPoint locOne = [[touches objectAtIndex:0] locationInView: [[touches objectAtIndex:0] view]];
    locOne = [[CCDirector sharedDirector] convertToGL:locOne];

    CGPoint locTwo = ccp(-100, -100);
    if ([touches count] > 1) {
        locTwo = [[touches objectAtIndex:1] locationInView: [[touches objectAtIndex:1] view]];
        locTwo = [[CCDirector sharedDirector] convertToGL:locTwo];
    }

    CGPoint locThree = ccp(-100, -100);
    if ([touches count] > 2) {
        locThree = [[touches objectAtIndex:2] locationInView: [[touches objectAtIndex:2] view]];
        locThree = [[CCDirector sharedDirector] convertToGL:locThree];
    }

    if(!(((CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree)))|| ((CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree)))))
    {
        if(!isJumping && !isSpin && !isStanding){
        [self.character stopAction:self.jumpAction];            //No buttons
        [self.character stopAction:self.runAction];
        [self.character runAction:self.walkAction];
        isRunning = NO;
        isStanding = YES;
        }
    }

    if (!(CGRectContainsPoint(button2.boundingBox, locOne) || CGRectContainsPoint(button2.boundingBox, locTwo) || CGRectContainsPoint(button2.boundingBox, locThree))) {
        left = NO; 
    } else {                                                                    //Left button
        [self.character setScaleX:-2];
        left = YES;
        isLeft = YES;
        isRight = NO;
        if(!isRunning && !isJumping && !isSpin && !isShooting)
        {
            [self.character stopAction:self.jumpAction];
            [self.character stopAction:self.walkAction];
            [self.character runAction:self.runAction];
            isRunning = YES;
            isStanding = NO;
        }
        else if(isSpin)
        {
            [self.character stopAction:self.runAction];
            [self.character stopAction:self.walkAction];
            [self.character stopAction:self.jumpAction];
            isRunning = NO;
            isStanding = NO;
        }

    }

    if (!(CGRectContainsPoint(button.boundingBox, locOne) || CGRectContainsPoint(button.boundingBox, locTwo) || CGRectContainsPoint(button.boundingBox, locThree))) {
        right = NO; 
    } else {
        [self.character setScaleX:2];                                       //Right button
        right = YES;
        isRight = YES;
        isLeft = NO;
        if(!isRunning && !isJumping && !isSpin)
        {
            [self.character stopAction:self.jumpAction];
            [self.character stopAction:self.walkAction];
            [self.character runAction:self.runAction];
            isRunning = YES;
            isStanding = NO;
        }
        else if(isSpin)
        {
            [self.character stopAction:self.runAction];
            [self.character stopAction:self.walkAction];
            [self.character stopAction:self.jumpAction];
            isRunning = NO;
            isStanding = NO;
        }


    }

}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    right = NO;
    left = NO;                                                                          //Touch ended
    isRunning = NO;
    if(!isJumping){
        [self.character stopAction:self.runAction];
        [self.character stopAction:self.jumpAction];
        if(isStanding && !isSpin){
            [self.character stopAction:self.walkAction];
            isStanding = NO; 
        }
        if(!isStanding && !isSpin){
            [self.character stopAction:self.runAction];
            [self.character runAction:self.walkAction];
            isStanding = YES;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我将发布今年早些时候在项目中使用的“双触摸按钮”的示例(cocos2d,objective-c)。您在创建它时将其传递给CCNode(例如CCSprite),当您按下它并释放它时它将执行一系列操作(请参阅.mm文件)。它将执行向上和向下的选择器(我使用它作为一个消防按钮......需要支持按住按钮)。如果您需要,您应该能够修改它以执行单个操作。

<强> DoubleActionButton.h

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

        // This button executes an action on the NODE, when the button is pressed down and when the
        // button is released.

    @interface DoubleActionButton : CCNode <CCTargetedTouchDelegate>

    +(DoubleActionButton*)buttonWithTarget:(id)target andDownSelector:(SEL)selD andUpSelector:(SEL)selU andNode:(CCNode*)node;

    @end

<强> DoubleActionButton.mm

    #import "DoubleActionButton.h"
    #import "SimpleAudioEngine.h"

    #import "CommonSTL.h"
    #import "UIConstants.h"
    #import "UIUtilities.h"

    @interface DoubleActionButton()
    {
        id _target;
        SEL _selUp;
       SEL _selDown;
        CCAction * _actUp;
       CCAction * _actDown;
       BOOL _down;
       BOOL _animating;
    }

    @property (nonatomic, assign) id _target;
    @property (nonatomic, assign) BOOL _down;
    @property (nonatomic, assign) SEL _selUp;
    @property (nonatomic, assign) SEL _selDown;
    @property (nonatomic, retain) CCAction * _actUp;
    @property (nonatomic, retain) CCAction * _actDown;
    @property (nonatomic, retain) CCNode * _node;
    @property (nonatomic, assign) BOOL _animating;

    @end

    @implementation DoubleActionButton

    @synthesize _target;
    @synthesize _actDown;
    @synthesize _actUp;
    @synthesize _selDown;
    @synthesize _selUp;
    @synthesize _animating;
    @synthesize _down;
    @synthesize _node;

    #pragma mark Setters


    #pragma mark Life Cycle

    -(CCAction*)createDownAction
    {
       // specify the visual actions that should occur when a user interacts with the button
       CCFiniteTimeAction* action0 = [CCCallFunc actionWithTarget:self selector:@selector(buttonDownStart)];
       CCFiniteTimeAction* action1 = [CCScaleTo actionWithDuration:0.1 scale:1.1f];
       CCFiniteTimeAction* action2 = [CCCallFunc actionWithTarget:self selector:@selector(buttonDownEnd)];
       CCSequence* seq = [CCSequence actions:action0,action1,action2,nil];
       return seq;
    }

    -(CCAction*)createUpAction
    {
       // specify the visual actions that should occur when a user interacts with the button
       CCFiniteTimeAction* action0 = [CCCallFunc actionWithTarget:self selector:@selector(buttonUpStart)];
       CCFiniteTimeAction* action1 = [CCScaleTo actionWithDuration:0.1 scale:1.0f];
       CCFiniteTimeAction* action2 = [CCCallFunc actionWithTarget:self selector:@selector(buttonUpEnd)];
       CCSequence* seq = [CCSequence actions:action0,action1,action2,nil];
       return seq;
    }


    -(DoubleActionButton*)init
    {
       self = [super init];
       if(self != nil)
       {
          _animating = NO;
          _down = NO;
          self._actUp = [self createUpAction];
          self._actDown = [self createDownAction];
          self._selUp = nil;
          self._selDown = nil;
          self._node = nil;
        }
        return self;
    }

    -(DoubleActionButton*)initButtonWithTarget:(id)target
                           andDownSelector:(SEL)selD
                             andUpSelector:(SEL)selU
                                 andNode:(CCNode*)node
    {
       self = [super init];
       if(self != nil)
       {
          self._actUp = [self createUpAction];
          self._actDown = [self createDownAction];
          self._target = target;
          self._selDown = selD;
          self._selUp = selU;
          self.anchorPoint = ccp(0.5,0.5);
          self._node = node;
          [self addChild:node z:100];
       }
       return self;
    }



    +(DoubleActionButton*)buttonWithTarget:(id)target
                           andDownSelector:(SEL)selD
                             andUpSelector:(SEL)selU
                                 andNode:(CCNode*)node
    {
       DoubleActionButton* button = [[DoubleActionButton alloc] initButtonWithTarget:target andDownSelector:selD andUpSelector:selU andNode:node];
       [button autorelease];
       return button;
    }

    -(void)dealloc
    {
       self._actDown = nil;
       self._actUp = nil;
       self._node = nil;
       [super dealloc];
    }

    #pragma mark Cocos2D Life Cycle

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

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

    #pragma mark General Stuff
    -(void)buttonDownStart
    {
       _animating = YES;
       _down = YES;
       if(_selDown && _target)
       {
          [_target performSelector:_selDown];
       }

       [[SimpleAudioEngine sharedEngine] playEffect:[UIConstants FILE_AUDIO_BUTTON_CLICK]];
    }

    -(void)buttonDownEnd
    {
       _animating = NO;
    }

    -(void)buttonUpStart
    {
       if(_selUp && _target)
       {
          [_target performSelector:_selUp];
       }
       _animating = YES;
    }

    -(void)buttonUpEnd
    {
       _down = NO;
       _animating = NO;
    }

    #pragma mark Touch events

    - (BOOL) touchInButton:(UITouch*)touch
    {
       CCNode* node = self._node;
       if(node)
       {

          // Is the touch inside node0's AABB?
          CGPoint touchPt = [UIUtilities TouchToScreen:touch];
          CGPoint local = [node convertToNodeSpace:touchPt];
          CGRect rect = CGRectMake(touchPt.x, touchPt.y, node.contentSize.width, node.contentSize.height);
          rect.origin = CGPointZero;

          if(CGRectContainsPoint( rect, local ) )
          {  // Not in the AABB of node0.
             return YES;
          }
       }
        return NO;
    }

    - (BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
    {
        if (!_animating && !_down && [self touchInButton:touch] )
        {
            if ( _actDown )
          {
                [self stopAllActions];
                [self runAction:_actDown];
            }
            return YES;
        }

        return NO;
    }

    - (void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event
    {
       // Finger moved off the button?
        if (_down && ![self touchInButton:touch] )
        {  // Looks like it.
            if ( _actUp )
          {
                [self stopAllActions];
                [self runAction:_actUp];
            }
        }
    }


    - (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    {
       if ( _actUp && _down)
       {
          [self stopAllActions];
          [self runAction:_actUp];
       }
    }

    -(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
    {
       if ( _actUp && _down)
       {
          [self stopAllActions];
          [self runAction:_actUp];
       }
    }


    @end

以下是使用它的示例:

  // Button 1
   DoubleActionButton* dblBtn;
   node0 = [CCSprite spriteWithSpriteFrameName:[UIConstants FILE_IMAGE_FIRE_BUTTON_UP]];
   [UIUtilities SetSpriteSize:(CCSprite*)node0 toSize:btnMaxSize];
   dblBtn = [DoubleActionButton buttonWithTarget:self andDownSelector:@selector(button1Down) andUpSelector:@selector(button1Up) andNode:node0];
   dblBtn.position = ccp(leftEdge,botEdge);
   [self addChild:dblBtn];

随意使用尽可能多的代码。

这有用吗?