在Cocos2D中使用两个游戏杆

这是我想要做的:用左手操纵杆,玩家可以操纵人物,用正确的操纵手柄进行攻击。 问题是左边的游戏杆似乎得到所有的input,正确的甚至没有注册任何东西。 我在eagleView之后启用了多重触摸,并彻底地完成了代码。 但我似乎错过了一些东西。 我用两根棍子踢球,这两支球在比赛中都显示出来了,但就像我说的,只有左边的球杆才起作用。

我将它们初始化。 从h。 文件:

SneakyJoystick *joystick; SneakyJoystick *joystickRight; 

在m.file中,我综合,取消分配并初始化它们。 为了使用一个控制和另一个进攻我把这个:

 -(void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray*)listOfGameObjects { if ((self.characterState == kStateIdle) || (self.characterState == kStateWalkingBack) || (self.characterState == kStateWalkingLeft) || (self.characterState == kStateWalkingRight)|| (self.characterState == kStateWalkingFront) || (self.characterState == kStateAttackingFront) || (self.characterState == kStateAttackingBack)|| (self.characterState == kStateAttackingRight)|| (self.characterState == kStateAttackingLeft)) { if (joystick.degrees > 60 && joystick.degrees < 120) { if (self.characterState != kStateWalkingBack) [self changeState:kStateWalkingBack]; }else if (joystick.degrees > 1 && joystick.degrees < 59) { if (self.characterState != kStateWalkingRight) [self changeState:kStateWalkingRight]; } else if (joystick.degrees > 211 && joystick.degrees < 300) { if (self.characterState != kStateWalkingFront) [self changeState:kStateWalkingFront]; } else if (joystick.degrees > 301 && joystick.degrees < 360){ if (self.characterState != kStateWalkingRight) [self changeState:kStateWalkingRight]; } else if (joystick.degrees > 121 && joystick.degrees < 210) { if (self.characterState != kStateWalkingLeft) [self changeState:kStateWalkingLeft]; } if (joystickRight.degrees > 60 && joystickRight.degrees < 120) { if (self.characterState != kStateAttackingBack) [self changeState:kStateAttackingBack]; }else if (joystickRight.degrees > 1 && joystickRight.degrees < 59) { if (self.characterState != kStateAttackingRight) [self changeState:kStateAttackingRight]; } else if (joystickRight.degrees > 211 && joystickRight.degrees < 300) { if (self.characterState != kStateAttackingFront) [self changeState:kStateAttackingFront]; } else if (joystickRight.degrees > 301 && joystickRight.degrees < 360){ if (self.characterState != kStateAttackingRight) [self changeState:kStateAttackingRight]; } else if (joystickRight.degrees > 121 && joystickRight.degrees < 210) { if (self.characterState != kStateAttackingLeft) [self changeState:kStateAttackingLeft]; } [self applyJoystick:joystick forTimeDelta:deltaTime]; [self applyJoystick:joystickRight forTimeDelta:deltaTime]; } 

也许这与把时间三角洲都放在一起呢? 我试着解决它,但它没有工作。 所以我很感激你们可以给我的任何input:)

根据请求这里是一些更多的代码:这是从我的gameplaylayer的初始化。

 self.isTouchEnabled = YES; [knight setJoystick:leftJoystick]; [knight setJoystickRight:attackButton]; 

另外从gameplaylayer方法:

 -(void)initJoystickAndButtons { CGSize screenSize = [CCDirector sharedDirector].winSize; CGRect joystickBaseDimensions = CGRectMake(0, 0, 128.0f, 128.0f); CGRect attackButtonDimensions = CGRectMake(0, 0, 128.0f, 128.0f); CGPoint joystickBasePosition; CGPoint attackButtonPosition; joystickBasePosition = ccp(screenSize.width*0.10f, screenSize.height*0.15f); attackButtonPosition = ccp(screenSize.width*0.9f, screenSize.height*0.15f); SneakyJoystickSkinnedBase *joystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease]; joystickBase.position = joystickBasePosition; joystickBase.backgroundSprite = [CCSprite spriteWithFile:@"dpadDown.png"]; joystickBase.thumbSprite = [CCSprite spriteWithFile:@"joystickDown.png"]; joystickBase.joystick = [[SneakyJoystick alloc] initWithRect:joystickBaseDimensions]; leftJoystick = [joystickBase.joystick retain]; [self addChild:joystickBase]; SneakyJoystickSkinnedBase *attackButtonBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease]; attackButtonBase.position = attackButtonPosition; attackButtonBase.backgroundSprite = [CCSprite spriteWithFile: @"dpadDown.png"]; attackButtonBase.thumbSprite = [CCSprite spriteWithFile:@"joystickDown.png"]; attackButtonBase.joystick = [[SneakyJoystick alloc] initWithRect: attackButtonDimensions]; attackButton = [joystickBase.joystick retain]; [self addChild:attackButtonBase]; } 

我在appdelegate中join了这个

 glView.multipleTouchEnabled = YES; 

Solutions Collecting From Web of "在Cocos2D中使用两个游戏杆"

我今天完成了整个代码,只是(过了太久)发现了一个错误,就是这样做的。

 leftJoystick = [joystickBase.joystick retain]; attackButton = [joystickBase.joystick retain]; 

它实际上必须是

 attackButton = [attackButtonBase.joystick retain]; 

现在它工作。 总是让你疯狂的小事

把它们都放到三角洲应该不重要,正如你所说的那样,我已经采取了它,你已经在init方法中启用了触摸。

你可以发布你的触摸方式吗? 不仅你需要启用触摸,你应该确保你正在使用

 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

而不是cctouch。 我认为问题可能在于如何处理上述方法中的代码,如果您使用该代码编辑问题,那么人们可能能够帮助您多一点。

更新:

我看到你正在使用鬼祟的操纵杆。 我认为这可能已经过时了,因为它是在2010年左右上次更新的。请看一下这个存储库,它更简洁,更容易实现,不需要很长的if语句,因为您可以使用三个游戏杆来移动您的精灵代表:

 -(void)joystickControlBegan; -(void)joystickControlMoved; -(void)joystickControlEnded; 

https://github.com/zaldzbugz/zJoystick

编辑:

你有没有在EAGLView上设置多点触控?

在初始化EAGLView之后,添加:

 view.multipleTouchEnabled = YES;