user597136
user597136

Reputation: 21

Cocos2d iPhone Game touch screen to make a character move

I have written a few games using cocos2d but they were all controlled by the accelerometer and used only simple touch events. All I need to do is register when the screen is touched, anywhere. I don't need any information about the position or velocity. The character currently moves across the screen and the users should be able to touch to make the character move up the screen. the current code does not work as intended. The character is not effected by the touch, it just continues to move down the screen. Please advise. Below is the code I am trying to use now.

In the game update method:

if (IsTouched == TRUE) {
    SealPositionBasedOnTouchInt = SealPositionBasedOnTouchInt - (100*dt);
}

else {
    SealPositionBasedOnTouchInt = SealPositionBasedOnTouchInt + (100*dt);
}

SealSwimming.position = ccp(SealPositionBasedOnTouchInt, SealSwimming.position.y);

The touch events:

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


    for( UITouch *touch in touches ) 
    {
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    IsTouched = TRUE;
}  
}

You'll notice I do get the touch location, this is currently not used for anything but was in the sample.

Upvotes: 0

Views: 1086

Answers (4)

Jonathan
Jonathan

Reputation: 2383

I haven't tested this.

in .h

CCSprite *sprite;


in .m

-(id)init
{
    if ((self=[super init))
    {
           CGSize s = [[CCDirector sharedDirector] winSize];

           sprite = [CCSprite spriteWithFile:@"imagename.png"];
           sprite.position = ccp(s.width/2, s.height/2);
           [self addChild:sprite];
    }
    return self;
}

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

    UITouch *touch = [touches anyObject];

    CGPoint touchLocation = [touch locationInView: [touch view]];   
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];

    [sprite setPosition:touchLocation];

    //OR YOU CAN RUN AN ANIMATION TO MAKE IT LOOK LIKE IT'S WALKING
    //[sprite runAction:[CCMoveTo actionWithDuration:5 position:touchLocation]];
}

Upvotes: 0

Boatski
Boatski

Reputation: 1

Sergio has the answer to your problem with touches not registering. I had a similar problem recently. If it doesn't matter where the user touches the screen then swallowsTouches: YES is fine, but if you have a couple layers that need to register touches you might need to set it to NO.

Upvotes: 0

Simon Lee
Simon Lee

Reputation: 22334

Make sure the layer has touch events enabled isTouchEnabled

Upvotes: 1

sergio
sergio

Reputation: 69027

Have you overridden:

-(void) registerWithTouchDispatcher {
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

in your layer/node that is receiving the touch?

You might also try and call from your code addTargetedDelegate:self without overriding registerWithTouchDispatcher, but I have never tried it.

Upvotes: 0

Related Questions