添加遥控杆

1. 首先需要再HelloWorldLayer.h中包含ZJoystick.h文件 并且让其实现ZJoystickDelegate协议

2.打开HelloWorldLayer.mm文件实现ZJoystickDelegate的一些方法:

#pragma mark - ZJoystick Delegate
-(void)joystickControlBegan{ }
-(void)joystickControlMoved{ }
-(void)joystickControlEnded{ } -(void)joystickControlDidUpdate:(id)joystick toXSpeedRatio:(CGFloat)xSpeedRatio toYSpeedRatio:(CGFloat)ySpeedRatio{ ZJoystick *zJoyStick=(ZJoystick *)joystick;
if (zJoyStick.joystickTag==999) {
CGFloat xPos=_player.position.x;
CGFloat yPos=_player.position.y;
_player.position=ccp(xPos+xSpeedRatio, yPos+ySpeedRatio);//更改要操控精灵的位置 } }

3.新增-(void)initJoystick方法 在init方法的最后调用

//初始化遥感
-(void)initJoystick{
ZJoystick *_joystick2=[ZJoystick joystickNormalSpriteFile:@"JoystickContainer_norm.png" selectedSpriteFile:@"JoystickContainer_trans.png" controllerSpriteFile:@"Joystick_norm.png"];
_joystick2.position=ccp(_joystick2.contentSize.width/2+10, _joystick2.contentSize.height/2+10);
_joystick2.delegate=self;
_joystick2.controlledObject=_player;//要操控的精灵
_joystick2.speedRatio=2.0f; //精灵的速度
_joystick2.joystickRadius=50.0f; //遥感器的半径
_joystick2.joystickTag=999;
[self addChild:_joystick2];
}

cocos2d学习之路四(添加遥控杆)-LMLPHP

所需图片及Zjoystick.h文件下载http://download.csdn.net/detail/superlele123/7170959

05-11 14:47