回到Xcode,新建ArmStatic类,继承于CCSprite.同样在初始化方法中打开用户交互.
下面添加触摸回调方法,touchBegan以及touchMoved方法和Arm基本相同,主要不同处在于其中的moveArm方法:
-(void)moveArm:(MoveDirection)direction{ float rotation = self.rotation; if (direction == armMoveDirectionUp) { rotation -= 5; }else if(direction == armMoveDirectionDown){ rotation += 5; } if (rotation > 90) { rotation = 90; }else if (rotation < -90){ rotation = -90; } Robot *robot = (Robot*)self.parent; [robot moveArmBefore]; self.rotation = rotation; }
这里判断旋转方向的逻辑和Arm基本是一样的,所不同的是:
1.这里没有物理关节帮你限制Arm旋转的范围,你必须自己写代码限制. 2.向上旋转要减小角度,反之要增加角度.因为默认角度增加的旋转为顺时针方向,在这个例子中即向下旋转. 3.通过手动调整物理对象的角度来实现旋转,这里和物理引擎没啥事了.