我一直在处理这个问题很长一段时间。 我想要做的是让CCSprite的左/右移动由屏幕左侧或右侧的触摸控制。
如果在每次触摸后抬起手指,这样做是没有问题的。但我想要的最好用一个例子来描述: 玩家触摸屏幕的左侧,精灵移动到左侧。现在玩家(虽然仍然触摸左侧)触及右侧......精灵现在应该向右移动。现在玩家左手上有一个手指,右手上有一个手指,如果他现在从右侧抬起触摸,精灵应再次向左移动。
这就是我现在所拥有的:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (location.x < 240) {
[player walk:kkMoveLeft];
} else if (location.x > 240) {
[player walk:kkMoveRight];
}
//Swipe Detection Part 1
firstTouch = location;
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//Swipe Detection Part 2
lastTouch = location;
float swipeLength = ccpDistance(firstTouch, lastTouch);
if (firstTouch.y < lastTouch.y && swipeLength > 60) {
[player jump:kkJumpUp];
} else if (firstTouch.y > lastTouch.y && swipeLength > 60){
[player jump:kkJumpDown];
}
[player endWalk];
}
如果有人能告诉我如何解决这个问题,我会很感激。谢谢。
更新我的解决方案:
//1. Enable multitouch in the appDelegate
[glView setMultipleTouchEnabled:YES];
//2. Create an Array to keep track of active touches
touchArray = [[NSMutableArray alloc] init];
//3. Touch Methods
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (![touchArray containsObject:touch]) {
[touchArray addObject:touch];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CCLOG(@"start: %f", location.x);
if (location.x < 240) {
[player walk:kkMoveLeft];
} else if (location.x > 240) {
[player walk:kkMoveRight];
}
}
}
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
[touchArray removeObject:touch];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CCLOG(@"end: %f", location.x);
}
for (UITouch *touch in touchArray) {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CCLOG(@"still: %f", location.x);
if (location.x < 240) {
[player walk:kkMoveLeft];
} else if (location.x > 240) {
[player walk:kkMoveRight];
}
}
if ([touchArray count] == 0) {
[player endWalk];
}
}
答案 0 :(得分:1)
如果我理解你的问题,你应该做的是:
ccTouchesEnded
中,而不是简单地调用endWalk
,检查是否仍然存在任何触摸(为此,您可以迭代allTouches
);
在适当的情况下(即,触摸仍在激活播放器),请致电startWalk
。
如果没有触摸,请拨打endWalk
。
代码与ccTouchesBegan
中的代码相似,只有你迭代(我不确定touchesEnded中索引0包含的allTouches
)。
老答案:
你现在没有说你如何处理接触。无论如何,要走的路是定义ccTouches*
方法(与ccTouch*
相比),其中*可以是:Began
,Moved
,Ended
。
-(void)tccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
...
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
...
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
...
}
请记住,touchesBegan会在检测到的每个 new 触摸时触发。因此,如果您想知道当前有效的所有触摸的状态,则必须使用allTouches
。
还要查看this post from iphonesdk,我发现这些方法中的触摸语义很有见地。