开发者

Cocos2d iPhone Game touch screen to make a character move

开发者 https://www.devze.com 2023-03-10 11:57 出处:网络
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\'

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 {
    SealPositionBasedOnTouc开发者_如何转开发hInt = 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.


Make sure the layer has touch events enabled isTouchEnabled


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消