Search code examples
iosobjective-ccocos2d-iphone

Objective-c: ccTouchesBegin End and Moved not Firing when using Custom Sprites


I have a Ship Class, in this Ship Class i have the methods ccTouchesBegin, ccTouchesEnd and ccTouchesMoved. But these methods never fire:

Here is the Ship Class:

//
    //  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;
        }
    }

This is how the Ship Class gets used in the Parent:

    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];

Solution

  • Your ship class must enable touch processing so it respond to Touch methods such as TouchesBegan and TouchesEnded. If I got it correct, your Ship class is a custom CCSprite or something similar, so probably it won't have any isTouchEnabled property.

    Use your touches methods in your Parent Layer and call your ship methods from there.

    Something like...

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

    Hope it helps :)