cocos2D 虚拟摇杆Joystick功能实现


@implementationInputLayer

-(id)init
{
if(self=[superinit])
{
winSize=[[CCDirectorsharedDirector]winSize];
[selfaddJoystick];
[selfaddFireButton];
[selfscheduleUpdate];
}
returnself;
}

//添加一个按钮
-(void)addFireButton
{
fireButton=[SneakyButtonbutton];
fireButton.isHoldable=YES;//按住按钮持续触发

//按钮添加皮肤
SneakyButtonSkinnedBase*skinFireButton=[SneakyButtonSkinnedBaseskinButton];
skinFireButton.defaultSprite=[CCSpritespriteWithSpriteFrameName:@”button-default.png];
skinFireButton.pressSprite=[CCSpritespriteWithSpriteFrameName:@”button-pressed.png];
skinFireButton.button=fireButton;
skinFireButton.position=CGPointMake(winSize.width-skinFireButton.contentSize.width,
skinFireButton.contentSize.height);

[selfaddChild:skinFireButton];

}

//添加一个摇杆
-(void)addJoystick
{
joystick=[SneakyJoystickjoystick:CGRectMake(0,0,0,0)];

joystick.autoCenter=YES;//是否自动回到中心

//360度
joystick.hasDeadzone=YES;//是否支持死亡区域,该区域不会触发
joystick.deadRadius=20;//死亡区域的半径

//限制可移动的方向数量
//joystick.isDPad=YES;
//joystick.numberOfDirections=8;//方向数量

//给摇杆添加皮肤
SneakyJoystickSkinnedBase*skinJoystick=[SneakyJoystickSkinnedBaseskinJoystick];
skinJoystick.backgroundSprite=[CCSpritespriteWithSpriteFrameName:@”button-disabled.png];
skinJoystick.thumbSprite=[CCSpritespriteWithSpriteFrameName:@”button-disabled.png];
skinJoystick.thumbSprite.scale=0.5f;
skinJoystick.joystick=joystick;
skinJoystick.position=CGPointMake(skinJoystick.contentSize.width,
skinJoystick.contentSize.height);

[selfaddChild:skinJoystick];
}

-(void)update:(ccTime)delta
{
GameScene*scene=[GameScenesharedGameScene];
Ship*ship=(Ship*)[sceneship];
totalTime+=delta;

//点击按钮触发
if(fireButton.active&&totalTime>nextShootTime)
{
nextShootTime=totalTime+0.5f;
[sceneshootBullet:ship];
}

if(fireButton.active==NO)
{
nextShootTime=0;
}

//joystick.velocity这个值非常小需要将其放大(根据实际情况调值)
CGPointvelocity=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);
}
} 运行图如下:二、CCJoystick类(最新版的已经支持搓招了哦,下载链接http://code.google.com/p/ccjoystick/downloads/listCCJoyStick是一个基于Cocos2d的摇杆类,简单几行代码即可为您的游戏增加一个强大的模拟摇杆。而且最新版本已经支持摇杆搓招儿,满足格斗类游戏开发者的需求。基于该类可自主扩展诸多摇杆效果,比如360度模式、8向模式。使用方法如下://创建摇杆myjoystick=[CCJoyStickinitWithBallRadius:25MoveAreaRadius:65isFollowTouch:NOisCanVisible:YESisAutoHide:NOhasAnimation:YES];//BallRadius即模拟摇杆球的半径,MoveAreaRadius即摇杆球可移动的范围半径,isFollowTouch即是否将摇杆基准位置跟随touch坐标,isCanVisible即是否可见,isAutoHide即是否自动隐藏(touchend即隐藏),hasAnimation即是否显示摇杆复位动画//添加皮肤[myjoysticksetBallTexture:@”Ball.png”];//可选,不设置即看不见摇杆球
[myjoysticksetDockTexture:@”Dock.png”];//可选,不设置即看不见底座
[myjoysticksetStickTexture:@”Stick.jpg”];//可选,不设置即看不见连动杆
[myjoysticksetHitAreaWithRadius:100];//摇杆激活区域为基准坐标半径,默认为另一个方法,设置屏幕矩形区域为激活区域setHitAreaWithRect

myjoystick.position=ccp(100,100);
myjoystick.delegate=self;
[selfaddChild:myjoystick];该摇杆类包含3个事件:1-(void)onCCJoyStickUpdate:(CCNode*)senderAngle:(float)angleDirection:(CGPoint)directionPower:(float)power;//angle用来控制角色朝向direction用来设置移动坐标power为力度用于控制速度快慢
2-(void)onCCJoyStickActivated:(CCNode*)sender;
3-(void)onCCJoyStickDeactivated:(CCNode*)sender;实现代码如下:1@implementationOperateLayer
2
3-(id)init
4{
5if(self=[superinit])
6{
7winSize=[[CCDirectorsharedDirector]winSize];
8joystick=[CCJoyStickinitWithBallRadius:25
9MoveAreaRadius:65
10isFollowTouch:NO
11isCanVisible:YES
12isAutoHide:NO
13hasAnimation:YES];
14[joysticksetBallTexture:@”Ball.png];
15[joysticksetDockTexture:@”Dock.png];
16[joysticksetStickTexture:@”Stick.jpg];
17[joysticksetHitAreaWithRadius:100];
18
19joystick.position=CGPointMake(100,100);
20[joysticksetDelegate:self];
21joystick.opacity=150;
22[selfaddChild:joystick];
23
24CCLabelTTF*label=[CCLabelTTFlabelWithString:@”shootfontName:@”ArialfontSize:30];
25CCMenuItemLabel*shoot=[CCMenuItemLabelitemWithLabel:label
26target:self
27selector:@selector(shoot:)];
28CCMenu*shootMenu=[CCMenumenuWithItems:shoot,nil];
29shootMenu.position=CGPointMake(380,80);
30[selfaddChild:shootMenu];
31}
32returnself;
33}
34
35-(void)shoot:(CCMenuItem*)menuItem{
36GameScene*scene=[GameScenesharedGameScene];
37
38[sceneshootBullet:scene.ship];
39}
40-(void)onCCJoyStickUpdate:(CCNode*)senderAngle:(float)angleDirectio免费云主机域名n:(CGPoint)directionPower:(float)power
41{
42if(sender==joystick){
43NSLog(@”angle:%fpower:%fdirection:%f,%f,angle,power,direction.x,direction.y);
44
45GameScene*scene=[GameScenesharedGameScene];
46
47floatnextx=scene.ship.position.x;
48floatnexty=scene.ship.position.y;
49
50nextx+=direction.x*(power*8);
51nexty+=direction.y*(power*8);
52
53scene.ship.position=ccp(nextx,nexty);
54}
55}
56
57-(void)onCCJoyStickActivated:(CCNode*)sender
58{
59if(sender==joystick){
60[joysticksetBallTexture:@”Ball_hl.png];
61[joysticksetDockTexture:@”Dock_hl.png];
62joystick.opacity=255;
63}
64}
65-(void)onCCJoyStickDeactivated:(CCNode*)sender
66{
67if(sender==joystick){
68[joysticksetBallTexture:@”Ball.png];
69[joysticksetDockTexture:@”Dock.png];
70joystick.opacity=150;
71}
72}
73@end运行效果图:以下是两个类库的下载链接,有需要的可以下载看看哦 ~/Files/xuling/CCJoystick.rar/Files/xuling/SneakyInput.rarps:咱新手们注意了哈,用最新的cocos2d时,看看AppDelegate.m 中的[glViewsetMultipleTouchEnabled:YES];设置为YES了没有。 我刚开始做的时候就没设置还查了好久,嘿嘿,有点菜 …

相关推荐: pfsense防火墙

pfSense是FreeBSD下的免费开源的防火墙和路由器软件,源自于m0n0wall的操作系统,是一个功能强大的,灵活的防火墙和路由平台。并有中文爱好社区论坛http://www.m0n0china.org/和http://bbs.tl20.com http…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

Like (0)
Donate 微信扫一扫 微信扫一扫
Previous 01/28 17:31
Next 01/28 17:31