@implementation InputLayer
- (id)init
{
if(self = [super init])
{
winSize = [[CCDirector sharedDirector] winSize];
[self addJoystick];
[self addFireButton];
[self scheduleUpdate];
}
return self;
}
//添加一個(gè)按鈕
- (void)addFireButton
{
fireButton = [SneakyButton button];
fireButton.isHoldable = YES; // 按住按鈕持續(xù)觸發(fā)
//按鈕添加皮膚
SneakyButtonSkinnedBase *skinFireButton = [SneakyButtonSkinnedBase skinButton];
skinFireButton.defaultSprite = [CCSprite spriteWithSpriteFrameName:@"button-default.png"];
skinFireButton.pressSprite = [CCSprite spriteWithSpriteFrameName:@"button-pressed.png"];
skinFireButton.button = fireButton;
skinFireButton.position = CGPointMake(winSize.width - skinFireButton.contentSize.width,
skinFireButton.contentSize.height);
[self addChild:skinFireButton];
}
//添加一個(gè)搖桿
- (void)addJoystick
{
joystick = [SneakyJoystick joystick:CGRectMake(0, 0, 0, 0 )];
joystick.autoCenter = YES; //是否自動(dòng)回到中心
//360度
joystick.hasDeadzone = YES; //是否支持死亡區(qū)域,該區(qū)域不會(huì)觸發(fā)
joystick.deadRadius = 20;//死亡區(qū)域的半徑
//限制可移動(dòng)的方向數(shù)量
// joystick.isDPad = YES;
// joystick.numberOfDirections = 8; //方向數(shù)量
//給搖桿添加皮膚
SneakyJoystickSkinnedBase *skinJoystick = [SneakyJoystickSkinnedBase skinJoystick];
skinJoystick.backgroundSprite = [CCSprite spriteWithSpriteFrameName:@"button-disabled.png"];
skinJoystick.thumbSprite = [CCSprite spriteWithSpriteFrameName:@"button-disabled.png"];
skinJoystick.thumbSprite.scale = 0.5f;
skinJoystick.joystick = joystick;
skinJoystick.position = CGPointMake(skinJoystick.contentSize.width ,
skinJoystick.contentSize.height);
[self addChild:skinJoystick];
}
- (void)update:(ccTime)delta
{
GameScene *scene = [GameScene sharedGameScene];
Ship *ship = (Ship *)[scene ship];
totalTime += delta;
//點(diǎn)擊按鈕觸發(fā)
if(fireButton.active && totalTime > nextShootTime)
{
nextShootTime = totalTime + 0.5f;
[scene shootBullet:ship];
}
if (fireButton.active == NO)
{
nextShootTime = 0;
}
//joystick.velocity 這個(gè)值非常小需要將其放大(根據(jù)實(shí)際情況調(diào)值)
CGPoint velocity = ccpMult(joystick.velocity, 200);
if(velocity.x != 0 && velocity.y != 0)
{
ship.position = CGPointMake(ship.position.x + velocity.x * delta,
ship.position.y + velocity.y *delta);
}
}
運(yùn)行圖如下:
二、CCJoystick類(lèi)(最新版的已經(jīng)支持搓招了哦,下載鏈接http://code.google.com/p/ccjoystick/downloads/list)
CCJoyStick 是一個(gè)基于 Cocos2d 的搖桿類(lèi),簡(jiǎn)單幾行代碼即可為您的游戲增加一個(gè)強(qiáng)大的模擬搖桿。而且最新版本已經(jīng)支持搖桿搓招兒,滿足格斗類(lèi)游戲開(kāi)發(fā)者的需求。
基于該類(lèi)可自主擴(kuò)展諸多搖桿效果,比如 360 度模式、8 向模式。使用方法如下:
// 創(chuàng)建搖桿
myjoystick=[CCJoyStick initWithBallRadius:25 MoveAreaRadius:65 isFollowTouch:NO isCanVisible:YES isAutoHide:NO hasAnimation:YES];//BallRadius即模擬搖桿球的半徑,MoveAreaRadius即搖桿球可移動(dòng)的范圍半徑,isFollowTouch即是否將搖桿基準(zhǔn)位置 跟隨touch坐標(biāo),isCanVisible即是否可見(jiàn),isAutoHide即是否自動(dòng)隱藏(touchend即隱藏),hasAnimation即 是否顯示搖桿復(fù)位動(dòng)畫(huà)
//添加皮膚
[myjoystick setBallTexture:@"Ball.png"];//可選,不設(shè)置即看不見(jiàn)搖桿球
[myjoystick setDockTexture:@"Dock.png"];//可選,不設(shè)置即看不見(jiàn)底座
[myjoystick setStickTexture:@"Stick.jpg"];//可選,不設(shè)置即看不見(jiàn)連動(dòng)桿
[myjoystick setHitAreaWithRadius:100];//搖桿激活區(qū)域?yàn)榛鶞?zhǔn)坐標(biāo)半徑,默認(rèn)為另一個(gè)方法,設(shè)置屏幕矩形區(qū)域?yàn)榧せ顓^(qū)域setHitAreaWithRect
myjoystick.position=ccp(100,100);
myjoystick.delegate=self;
[self addChild:myjoystick];
該搖桿類(lèi)包含3個(gè)事件:
1、- (void) onCCJoyStickUpdate:(CCNode*)sender Angle:(float)angle Direction:(CGPoint)direction Power:(float)power;//angle用來(lái)控制角色朝向,direction用來(lái)設(shè)置移動(dòng)坐標(biāo),power為力度用于控制速度快慢
2 、- (void) onCCJoyStickActivated:(CCNode*)sender;
3、- (void) onCCJoyStickDeactivated:(CCNode*)sender;
實(shí)現(xiàn)代碼如下:
1 @implementation OperateLayer
2
3 - (id)init
4 {
5 if(self = [super init])
6 {
7 winSize = [[CCDirector sharedDirector] winSize];
8 joystick = [CCJoyStick initWithBallRadius:25
9 MoveAreaRadius:65
10 isFollowTouch:NO
11 isCanVisible:YES
12 isAutoHide:NO
13 hasAnimation:YES];
14 [joystick setBallTexture:@"Ball.png"];
15 [joystick setDockTexture:@"Dock.png"];
16 [joystick setStickTexture:@"Stick.jpg"];
17 [joystick setHitAreaWithRadius:100];
18
19 joystick.position = CGPointMake(100, 100);
20 [joystick setDelegate:self];
21 joystick.opacity = 150;
22 [self addChild:joystick];
23
24 CCLabelTTF *label= [CCLabelTTF labelWithString:@"shoot" fontName:@"Arial" fontSize:30];
25 CCMenuItemLabel *shoot = [CCMenuItemLabel itemWithLabel:label
26 target:self
27 selector:@selector(shoot:)];
28 CCMenu *shootMenu = [CCMenu menuWithItems:shoot, nil];
29 shootMenu.position =CGPointMake( 380, 80);
30 [self addChild:shootMenu];
31 }
32 return self;
33 }
34
35 - (void)shoot:(CCMenuItem *) menuItem{
36 GameScene *scene = [GameScene sharedGameScene];
37
38 [scene shootBullet:scene.ship];
39 }
40 - (void) onCCJoyStickUpdate:(CCNode*)sender Angle:(float)angle Direction:(CGPoint)direction Power:(float)power
41 {
42 if (sender==joystick) {
43 NSLog(@"angle:%f power:%f direction:%f,%f",angle,power,direction.x,direction.y);
44
45 GameScene *scene = [GameScene sharedGameScene];
46
47 float nextx=scene.ship.position.x;
48 float nexty=scene.ship.position.y;
49
50 nextx+=direction.x * (power*8);
51 nexty+=direction.y * (power*8);
52
53 scene.ship.position=ccp(nextx,nexty);
54 }
55 }
56
57 - (void) onCCJoyStickActivated:(CCNode*)sender
58 {
59 if (sender==joystick) {
60 [joystick setBallTexture:@"Ball_hl.png"];
61 [joystick setDockTexture:@"Dock_hl.png"];
62 joystick.opacity = 255;
63 }
64 }
65 - (void) onCCJoyStickDeactivated:(CCNode*)sender
66 {
67 if (sender==joystick) {
68 [joystick setBallTexture:@"Ball.png"];
69 [joystick setDockTexture:@"Dock.png"];
70 joystick.opacity = 150;
71 }
72 }
73 @end
運(yùn)行效果圖:
以下是兩個(gè)類(lèi)庫(kù)的下載鏈接,有需要的可以下載看看哦 ~
/Files/xuling/CCJoystick.rar
/Files/xuling/SneakyInput.rar
ps:咱新手們注意了哈,用最新的cocos2d時(shí),看看AppDelegate.m 中的
[glView setMultipleTouchEnabled:YES];設(shè)置為YES了沒(méi)有。 我剛開(kāi)始做的時(shí)候就沒(méi)設(shè)置還查了好久,嘿嘿,有點(diǎn)菜 ...
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。