开发者

How do I detect multi-touch in cocos2d?

开发者 https://www.devze.com 2023-04-12 18:12 出处:网络
Edited Post Ok I tried what rptwsthi said in another project just to test it...... -(id) init { if( (self=[super init])) {

Edited Post

Ok I tried what rptwsthi said in another project just to test it......

-(id) init
{
if( (self=[super init])) {

    self.isTouchEnabled = YES;
}
return self;
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSArray *touchArray=[touches allObjects];

    if ([touchArray count] == 2) {
        NSLog(@"touch 2");
    }
    else if([touchArray count]==1) {
        NSLog(@"touch 1");
    }
}

But only the "touch 1" NSLog pops ups when I press the screen with two fingers. Do I need to put 开发者_如何学Pythonwhat LearnCocos2D said in there somewhere too.

Old Post

I have a test app I'm making and in it I have 3 buttons in the hud, 2 are for moving left and right and the other is for shooting. This is what I currently have.....

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInView:[touch view]];
loc = [[CCDirector sharedDirector] convertToGL:loc];

//Move Left
CGRect left = CGRectMake(leftButton.position.x-50/2, leftButton.position.y-50/2, 50, 50);
if (CGRectContainsPoint(left, loc)) {
    [self schedule:@selector(moveLeft)]; 
}

//Move Right
CGRect right = CGRectMake(rightButton.position.x-50/2, rightButton.position.y-50/2, 50, 50);
if (CGRectContainsPoint(right, loc)) {
    [self schedule:@selector(moveRight)];
}

//Shoot
CGRect shoot = CGRectMake(shootButton.position.x-50/2, shootButton.position.y-50/2, 50, 50);
    if (CGRectContainsPoint(shoot, loc)) {
    bullet = [CCSprite spriteWithFile:@"bullet.png"];
    bullet.position = ccp(plane.position.x, plane.position.y+20);
    [self addChild:bullet];
    }
}

-(void) ccTouchesEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    [self unschedule:@selector(moveLeft)];
    [self unschedule:@selector(moveRight)];
}

But I can only press one button at a time. I want to be able to hold the right or left button and also shoot with the shoot button. Can anyone fix my code or show me a basic example of multi-touch?

Also I'm new to iOS development and any help will be much appreciated. Thanks.


Have you enabled multiple touches on the cocos2d view?

[[CCDirector sharedDirector].openGLView setMultipleTouchEnabled:YES];


You Just use allObject instead of anyObject, and check it like:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSArray *touchArray=[touches allObjects];

    if ([touchArray count] == 2)
        //DO ONE THING 
    else if([touchArray count]==1)
        //DO ANOTHER THING
}
0

精彩评论

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