Cocos2d iphone 精灵运动的多点触控-cocos2d
我已经处理这个问题很长时间了。 我想做的是通过触摸屏幕左侧或右侧来控制CCSprite的左右移动 如果每次触摸后都抬起手指,那么这样做没有问题。但我想要的最好用一个例子来描述: 玩家触摸屏幕左侧,精灵向左移动。现在玩家(仍然触摸左侧)触摸右侧…精灵现在应该向右移动。现在玩家的一个手指在左边,一个手指在右边,如果他现在从右边抬起触球,精灵应该再次向左移动 这就是我现在拥有的:Cocos2d iphone 精灵运动的多点触控-cocos2d,cocos2d-iphone,controls,multi-touch,uitouch,Cocos2d Iphone,Controls,Multi Touch,Uitouch,我已经处理这个问题很长时间了。 我想做的是通过触摸屏幕左侧或右侧来控制CCSprite的左右移动 如果每次触摸后都抬起手指,那么这样做没有问题。但我想要的最好用一个例子来描述: 玩家触摸屏幕左侧,精灵向左移动。现在玩家(仍然触摸左侧)触摸右侧…精灵现在应该向右移动。现在玩家的一个手指在左边,一个手指在右边,如果他现在从右边抬起触球,精灵应该再次向左移动 这就是我现在拥有的: -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)e
-(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];
}
-(void)cctouchsbegind:(NSSet*)接触事件:(UIEvent*)事件{
NSSet*AllTouchs=[event AllTouchs];
UITouch*touch=[[AllTouchs AllObject]对象索引:0];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
如果(位置x<240){
[玩家行走:左];
}否则,如果(位置x>240){
[玩家行走:右];
}
//刷卡检测第1部分
firstTouch=位置;
}
-(无效)ccTouchesEnded:(NSSet*)接触事件:(UIEvent*)事件{
NSSet*AllTouchs=[event AllTouchs];
UITouch*touch=[[AllTouchs AllObject]对象索引:0];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
//刷卡检测第2部分
lastTouch=位置;
浮动开关长度=CCPDEANCE(第一次触摸,最后触摸);
如果(firstTouch.y60){
[玩家跳跃:kkJumpUp];
}否则如果(firstTouch.y>lastTouch.y&&swipeLength>60){
[玩家跳跃: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];
}
}
//1。在appDelegate中启用多点触摸
[glView SETMULTIPLETOUCHEBLED:是];
//2. 创建一个数组以跟踪活动触摸
touchArray=[[NSMutableArray alloc]init];
//3. 触摸法
-(void)CCTouchesStart:(NSSet*)与事件接触:(UIEvent*)事件{
用于(UITouch*触摸屏){
如果(![touchArray containsObject:touch]){
[触摸阵列添加对象:触摸];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
CCLOG(@“开始:%f”,位置.x);
如果(位置x<240){
[玩家行走:左];
}否则,如果(位置x>240){
[玩家行走:右];
}
}
}
}
-(无效)ccTouchesEnded:(NSSet*)接触事件:(UIEvent*)事件{
用于(UITouch*触摸屏){
[触摸阵列移除对象:触摸];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
CCLOG(@“结束:%f”,位置.x);
}
用于(触摸阵列中的UITouch*触摸){
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
CCLOG(@“仍然:%f”,位置.x);
如果(位置x<240){
[玩家行走:左];
}否则,如果(位置x>240){
[玩家行走:右];
}
}
如果([touchArray count]==0){
[player endWalk];
}
}
如果我正确理解您的问题,您应该做的是:
cctouchesend
中,不要简单地调用endWalk
,而是检查是否仍存在任何触摸(为此,您可以迭代alltouchs
)startWalk
endWalk
cctouchsbegind
中的代码,只是您进行了迭代(我不确定alltouchs
在touchsended的索引0中包含什么内容)
旧答案:
你没有说你现在如何处理触球。在任何情况下,方法都是定义ccTouch*
方法(与ccTouch*
),其中*可以是:开始
,移动
,结束
-(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 {
...
}
请记住,每次检测到新触摸时都会触发ToucheSBegind。因此,如果您想知道当前活动的所有触摸的状态,必须使用alltoucks
另外,请看一下我对这些方法中触摸的语义有何见解。触摸如何工作不是我的问题(请参阅更新的问题)。我遇到的问题是,让玩家在用两个手指触摸屏幕后继续移动,然后抬起一个手指,让玩家继续移动。与此同时,我自己也找到了一个解决方案……但感谢你的努力,因为你的更新答案实际上就是我得到的答案,我接受你的答案;-)