SneakyJoyStick 用法( 兼容universal)

来源:互联网 发布:php 电子商城 编辑:程序博客网 时间:2024/06/03 16:54

1. import  JoystickClasses folder into project

2. 在game操作的layer.h import class


#import "SneakyJoystick.h"

#import "SneakyButton.h"


#import "SneakyButtonSkinnedBase.h"

#import "SneakyJoystickSkinnedBase.h"


@interface GameplayLayer : CCLayer {
    CCSprite *vikingSprite;
    SneakyJoystick *leftJoystick;
    SneakyButton *jumpButton;
    SneakyButton *attackButton;
}
@end



-(void)initJoystickAndButtons {CGSize screenSize = [CCDirector sharedDirector].winSize; // 1CGRect joystickBaseDimensions = CGRectMake(0, 0, 128.0f, 128.0f);                      // 2CGRect jumpButtonDimensions   = CGRectMake(0, 0, 64.0f, 64.0f);CGRect attackButtonDimensions = CGRectMake(0, 0, 64.0f, 64.0f);CGPoint joystickBasePosition;                                  // 3CGPoint jumpButtonPosition;CGPoint attackButtonPosition;if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // 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:vikingSpriteforTimeDelta:deltaTime];}

实际通过下面方法对某node的操作


-(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   }}


原创粉丝点击