Cocos2d iphone 如何在cocos2d中检测多点触摸?
编辑文章 好的,我在另一个项目中尝试了rptwsthi所说的,只是为了测试它Cocos2d iphone 如何在cocos2d中检测多点触摸?,cocos2d-iphone,multi-touch,Cocos2d Iphone,Multi Touch,编辑文章 好的,我在另一个项目中尝试了rptwsthi所说的,只是为了测试它 -(id) init { if( (self=[super init])) { self.isTouchEnabled = YES; } return self; } - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *touchArray=[touches allObjects];
-(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");
}
}
但当我用两个手指按下屏幕时,只会弹出“touch 1”NSLog。我需要把LearnCos2D说的话也放在那里吗
旧职位
我有一个我正在制作的测试应用程序,其中我在hud上有3个按钮,2个用于左右移动,另一个用于拍摄。这就是我目前拥有的
-(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)];
}
但我一次只能按一个按钮。我希望能够按住右键或左键,也可以使用“拍摄”按钮进行拍摄。有人能修改我的代码或给我看一个多点触摸的基本示例吗
另外,我是iOS开发新手,任何帮助都将不胜感激。谢谢。您只需使用
allObject
而不是anyObject
,并按如下方式进行检查:
- (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
}
只需使用
allObject
而不是anyObject
,并按如下方式进行检查:
- (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
}
您是否在cocos2d视图上启用了多次触摸
[[CCDirector sharedDirector].openGLView setMultipleTouchEnabled:YES];
您是否在cocos2d视图上启用了多次触摸
[[CCDirector sharedDirector].openGLView setMultipleTouchEnabled:YES];
对不起,我该把这个放在哪里?我不确定cocos2d文件是什么文件。你可以在任何地方调用此方法,即在场景的init方法或app delegate的applicationdFinishLaunching方法中。抱歉,但我应该将此放在哪里?我不确定cocos2d文件是什么文件。您可以在任何地方调用此方法,即在场景的init方法或应用程序代理的ApplicationdFinishLaunching方法中。请参阅此将帮助您了解更多请参阅此将帮助您了解更多