(转载请注明出处:http://blog.csdn.net/buptgshengod)
#import <GameKit/GameKit.h> // When you import this file, you import all the cocos2d classes #import "cocos2d.h" #import "ZJoystick.h" // HelloWorldLayer @interface HelloWorldLayer : CCLayer<ZJoystickDelegate> { CCTexture2D *spriteTexture_; // weak ref //b2World* world; // strong ref //GLESDebugDraw *m_debugDraw; // strong ref //CCTMXTiledMap *_gameMap; CCSprite *_player;//是精灵,图中的熊猫 } // returns a CCScene that contains the HelloWorldLayer as the only child +(CCScene *) scene; @end
@interface HelloWorldLayer() //-(void) initPhysics; -(void) initJoystick; @end
-(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; //Joystick Delegate _joystick2.controlledObject = _player; _joystick2.speedRatio = 2.0f; _joystick2.joystickRadius = 50.0f; //added in v1.2 _joystick2.joystickTag = 999; [self addChild:_joystick2]; }
#pragma mark - zJoystick delegate methods -(void)joystickControlBegan { // CCLOG(@"Joystick Began Controlling"); } -(void)joystickControlMoved { // CCLOG(@"Joystick Move Controlling"); } -(void)joystickControlEnded { // CCLOG(@"Joystick End Controlling"); } -(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);//定义精灵的移动方式 } } @end最后一步就是要修改,init函数。前提是我们已经添加完成精灵(也就是图中的熊猫,前面第四章讲了)。
_player=panda;//panda为自己定义的精灵对象 [self initJoystick];//调用initJoystick函数(程序源代码资源下载地址)