大佬教程收集整理的这篇文章主要介绍了SneakyJoyStick 用法( 兼容universal),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1. import JoystickClasses folder into project
2. 在game操作的layer.h import class
#import "SneakyButton.h"
#import "SneakyJoystickSkinnedBase.h"
@interface GameplayLayer : CCLayer {
CCSprite *vikingSprite;
SneakyJoystick *leftJoystick;
SneakyButton *jumpButton;
SneakyButton *attackButton;
}
@end
-(void)initJoystickAndButtons { CGSize screenSize = [CCDirector sharedDirector].winSize; // 1 CGRect joystickBaseDimensions = CGRectMake(0,128.0f,128.0f); // 2 CGRect jumpButtonDimensions = CGRectMake(0,64.0f,64.0f); CGRect attackButtonDimensions = CGRectMake(0,64.0f); CGPoint joystickBasePosition; // 3 CGPoint jumpButtonPosition; CGPoint attackButtonPosition; if (UI_user_iNTERFACE_I@L_601_0@m() == UIUserInterfaceI@L_601_0@mPad) { // 4 // The device is an iPad running iPhone 3.2 or later. CCLOG(@"Positioning Joystick and Buttons for iPad"); joystickBasePosition = ccp(screenSize.width*0.0625f,screenSize.height*0.052f); jumpButtonPosition = ccp(screenSize.width*0.946f,screenSize.height*0.052f); attackButtonPosition = ccp(screenSize.width*0.947f,screenSize.height*0.169f); } else { // The device is an iPhone or iPod touch. CCLOG(@"Positioning Joystick and Buttons for iPhone"); joystickBasePosition = ccp(screenSize.width*0.07f,screenSize.height*0.11f); jumpButtonPosition = ccp(screenSize.width*0.93f,screenSize.height*0.11f); attackButtonPosition = ccp(screenSize.width*0.93f,screenSize.height*0.35f); } SneakyJoystickSkinnedBase *joystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease]; // 5 joystickBase.position = joystickBasePosition; // 6 joystickBase.BACkgroundSprite = [CCSprite spriteWithFile:@"dpadDown.png"]; // 7 joystickBase.thumbSprite = [CCSprite spriteWithFile:@"joystickDown.png"]; // 8 joystickBase.joystick = [[SneakyJoystick alloc] initWithRect:joystickBaseDimensions]; // 9 leftJoystick = [joystickBase.joystick retain]; // 10 [self addChild:joystickBase]; // 11 SneakyButtonSkinnedBase *jumpButtonBase = [[[SneakyButtonSkinnedBase alloc] init] autorelease]; // 12 jumpButtonBase.position = jumpButtonPosition; // 13 jumpButtonBase.defaultSprite = [CCSprite spriteWithFile:@"jumpUp.png"]; // 14 jumpButtonBase.activatedSprite = [CCSprite spriteWithFile:@"jumpDown.png"]; // 15 jumpButtonBase.pressSprite = [CCSprite spriteWithFile:@"jumpDown.png"]; // 16 jumpButtonBase.button = [[SneakyButton alloc] initWithRect:jumpButtonDimensions]; // 17 jumpButton = [jumpButtonBase.button retain]; // 18 jumpButton.isToggleable = NO; // 19 [self addChild:jumpButtonBase]; // 20 SneakyButtonSkinnedBase *attackButtonBase = [[[SneakyButtonSkinnedBase alloc] init] autorelease]; // 21 attackButtonBase.position = attackButtonPosition; // 22 attackButtonBase.defaultSprite = [CCSprite spriteWithFile:@"handUp.png"]; // 23 attackButtonBase.activatedSprite = [CCSprite spriteWithFile:@"handDown.png"]; // 24 attackButtonBase.pressSprite = [CCSprite spriteWithFile:@"handDown.png"]; // 25 attackButtonBase.button = [[SneakyButton alloc] initWithRect:attackButtonDimensions]; // 26 attackButton = [attackButtonBase.button retain]; // 27 attackButton.isToggleable = NO; // 28 [self addChild:attackButtonBase]; // 29 }
在每一帧中检测joystick ,应用joystick的响应:
#pragma mark - #pragma mark update Method -(void) update:(cctimE)deltaTime { [self applyJoystick:leftJoystick toNode:vikingSprite fortimedelta:deltaTime]; }
-(void)applyJoystick:(SneakyJoystick *)aJoystick toNode:(CCNode *)tempnode fortimedelta:(float)deltaTime { CGPoint scaledVeLocity = ccpMult(aJoystick.veLocity,1024.0f); // 1 CGPoint newPosition = ccp(tempnode.position.x + scaledVeLocity.x * deltaTime,tempnode.position.y + scaledVeLocity.y * deltaTimE); // 2 [tempnode setPosition:newPosition]; // 3 if (jumpButton.active == YES) { CCLOG(@"Jump button is pressed."); // 4 } if (attackButton.active == YES) { CCLOG(@"Attack button is pressed."); // 5 } }
以上是大佬教程为你收集整理的SneakyJoyStick 用法( 兼容universal)全部内容,希望文章能够帮你解决SneakyJoyStick 用法( 兼容universal)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。