Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Cocos2d iphone CCToucheSensed结束所有操作_Cocos2d Iphone - Fatal编程技术网

Cocos2d iphone CCToucheSensed结束所有操作

Cocos2d iphone CCToucheSensed结束所有操作,cocos2d-iphone,Cocos2d Iphone,因此,我一直有一些问题与cctouchesEnded,当一个触摸结束时,它触发了我所有的按钮结束行动。我理解为什么会发生这种情况,但要解决它,我不知道该怎么办。如果我为按钮创建一个类,并为每个新按钮创建一个新的类实例,那么每个按钮会是独立的吗?如果是这样的话,有没有一个很好的例子说明如何使用类在cocos2d中创建简单的按钮?任何帮助都将不胜感激。下面是我目前使用按钮的方式 -(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)e

因此,我一直有一些问题与cctouchesEnded,当一个触摸结束时,它触发了我所有的按钮结束行动。我理解为什么会发生这种情况,但要解决它,我不知道该怎么办。如果我为按钮创建一个类,并为每个新按钮创建一个新的类实例,那么每个按钮会是独立的吗?如果是这样的话,有没有一个很好的例子说明如何使用类在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;
        }
    }

}

我将发布一个今年早些时候在一个项目中使用的“双触按钮”示例(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
下面是一个使用它的示例:

  // 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];
您可以随意使用所需的代码


这有帮助吗?

一个很好的例子是项目编号?虽然我不明白为什么在这种情况下不能使用带有三个CCMenu项的CCMenu。。。你有源代码。。。应该很容易扩展一个类,或者用一个好的起点创建自己的类。谢谢,这正是我需要的!
  // 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];