Ios 目标c:ccTouchesBegin End和Moved在使用自定义精灵时不开火

Ios 目标c:ccTouchesBegin End和Moved在使用自定义精灵时不开火,ios,objective-c,cocos2d-iphone,Ios,Objective C,Cocos2d Iphone,我有一个Ship类,在这个Ship类中,我有ccTouchesBegin、ccTouchesEnd和ccTouchesMoved方法。但这些方法永远不会开火: 这是船的级别: // // Ship.m // Asteroids // // Created by trikam patel on 06/08/2014. // Copyright 2014 trikam patel. All rights reserved. // #i

我有一个Ship类,在这个Ship类中,我有ccTouchesBegin、ccTouchesEnd和ccTouchesMoved方法。但这些方法永远不会开火:

这是船的级别:

//
    //  Ship.m
    //  Asteroids
    //
    //  Created by trikam patel on 06/08/2014.
    //  Copyright 2014 trikam patel. All rights reserved.
    //

    #import "Ship.h"
    #import "Helper.h"

    @implementation Ship


    // on "init" you need to initialize your instance
    -(id)init:(NSString*)imageName :(NSMutableArray*)asteroids
    {
        if( (self=[super init:imageName]) ) {

        asteroids = asteroids;

        }
        return self;
    }

    -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {
        for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];

        if(allowedToMove){
            if(hasShipHit){
            if(!shiphit){
                shiphit = [[CCSprite alloc] init];
                CCSpriteBatchNode *spritesheet = [Helper setupAnimation:@"shiphit" :2 :shiphit];
                [self addChild:spritesheet];
            }

            [shiphit setPosition:ccp([self position].x, [self position].y)];
            [shiphit setPosition:ccp(point.x, point.y + 76)];
            }else{
            [self setPosition:ccp(point.x, point.y + 76)];

            }

        }


        }
    }


    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];

        NSLog(@"begin move ship");
        if(hasShipHit){
            int shipX = [shiphit position].x;
            int shipY = [shiphit position].y;
            if (CGRectContainsPoint(CGRectMake (shipX - 20.5, shipY - 96, 50, 50), point))
            {
            allowedToMove = true;
            }
        }else{
            int shipX = [self position].x;
            int shipY = [self position].y;

            if (CGRectContainsPoint(CGRectMake (shipX - 20.5, shipY - 96, 50, 50), point))
            {
            allowedToMove = true;
            }
        }

        }

    }

    -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];
        allowedToMove = false;
        }
    }
以下是Ship类在父类中的使用方式:

    self.isTouchEnabled = YES;

    Background *backgorund = [[Background alloc] init:@"bg1.png"];
    [self addChild:backgorund];

        ship = [[Ship alloc] init:@"ship.png" :asteroidArray];
    [ship setPosition:ccp(100, 100)];
    [self addChild:ship];

您的船级必须启用触摸处理,以便对触摸开始和触摸结束等触摸方法作出响应。如果我没有弄错的话,您的Ship类是一个定制的CCSprite或类似的东西,所以它可能没有任何isTouchEnabled属性

在父层中使用触摸方法,并从那里调用ship方法

类似于

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];
        [ship methodForTouch:point];
    }
}

希望有帮助:)

在你的飞船级,你启用了触摸处理吗?@YvesLeBorg,我在init的父层中添加了触摸处理,说实话,我不确定这将如何工作(至少在cocos2dv3中,从未使用过它)。您需要在启用触摸功能的对象中进行回调。。。这意味着,如果您从父级(船舶容器)检测,而不是从船舶本身检测,您的业务逻辑可能会大不相同。@YvesLeBorg,我是否应该在父级而不是船舶类别中进行所有碰撞检测并跟踪船舶、子弹和小行星?。我只是想跟上苹果的代码标准规范。阅读,学习,尝试,但做你自己的事情。。。最终,你将拥有自己的模式和标准,这在我看来只能比苹果的一些教条更好。至于这里的具体答案,我需要更好地了解你的应用程序,以便提供指导(而不是在这个论坛:)。。。但是:如果与触摸相关的事件往往涉及多个“可触摸”和“可触摸类型”、它们之间的关系以及它们可以做什么,我会在“可触摸”类之外进行。每个人都需要太多的背景,而这通常不是他们所关心的。