In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
@ implementation InputLayer
-(id) init
{
If (self = [super init])
{
WinSize = [[CCDirector sharedDirector] winSize]
[self addJoystick]
[self addFireButton]
[self scheduleUpdate]
}
Return self
}
/ / add a button
-(void) addFireButton
{
FireButton = [SneakyButton button]
FireButton.isHoldable = YES; / / Press and hold the button to trigger continuously
/ / Button to add skin
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]
}
/ / add a rocker
-(void) addJoystick
{
Joystick = [SneakyJoystick joystick:CGRectMake (0,0,0,0)]
Joystick.autoCenter = YES; / / whether to automatically return to the center
/ / 360 degrees
Joystick.hasDeadzone = YES; / / whether the dead zone is supported, which will not be triggered
Joystick.deadRadius = 20 position / radius of the dead zone
/ / limit the number of directions that can be moved
/ / joystick.isDPad = YES
/ / joystick.numberOfDirections = 8; / / number of directions
/ / add skin to the rocker
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
/ / Click the button to trigger
If (fireButton.active & & totalTime > nextShootTime)
{
NextShootTime = totalTime + 0.5f
[scene shootBullet:ship]
}
If (fireButton.active = = NO)
{
NextShootTime = 0
}
/ / the value of joystick.velocity is very small and needs to be magnified (adjusted according to the actual situation)
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)
}
}
The running diagram is as follows:
2. CCJoystick class (the latest version already supports rubbing, download link http://code.google.com/p/ccjoystick/downloads/list)
CCJoyStick is a Cocos2d-based joystick class, a few lines of code can add a powerful analog joystick to your game. And the latest version already supports rocking tricks to meet the needs of combat game developers.
Based on this class, many rocker effects can be expanded independently, such as 360-degree mode and 8-direction mode. The method of use is as follows:
/ / create a rocker
Myjoystick= [CCJoyStick initWithBallRadius:25 MoveAreaRadius:65 isFollowTouch:NO isCanVisible:YES isAutoHide:NO hasAnimation:YES]; / / BallRadius is to simulate the radius of the rocker ball, and MoveAreaRadius is the radius of the range in which the rocker ball can move. IsFollowTouch means whether to follow the reference position of the rocker to touch coordinates, whether isCanVisible is visible, whether isAutoHide is automatically hidden (touchend is hidden), and whether hasAnimation shows rocker reset animation.
/ / add skin
[myjoystick setBallTexture:@ "Ball.png"]; / / optional. If not set, the joystick ball cannot be seen.
[myjoystick setDockTexture:@ "Dock.png"]; / / optional. If not set, the base cannot be seen.
[myjoystick setStickTexture:@ "Stick.jpg"]; / / optional. If not set, the connecting rod cannot be seen.
[myjoystick setHitAreaWithRadius:100]; / / the joystick activation area is the reference coordinate radius, which defaults to another method. Set the screen rectangle area to the activation area setHitAreaWithRect.
Myjoystick.position=ccp (100100)
Myjoystick.delegate=self
[self addChild:myjoystick]
The joystick class contains three events:
1.-(void) onCCJoyStickUpdate: (CCNode*) sender Angle: (float) angle Direction: (CGPoint) direction Power: (float) power;//angle is used to control character orientation, direction is used to set moving coordinates, and power is used to control speed.
2.-(void) onCCJoyStickActivated: (CCNode*) sender
3.-(void) onCCJoyStickDeactivated: (CCNode*) sender
The implementation code is as follows:
1 @ implementation OperateLayer
two
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]
eighteen
19 joystick.position = CGPointMake (100,100)
20 [joystick setDelegate:self]
21 joystick.opacity = 150
22 [self addChild:joystick]
twenty-three
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}
thirty-four
35-(void) shoot: (CCMenuItem *) menuItem {
36 GameScene * scene = [GameScene sharedGameScene]
thirty-seven
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)
forty-four
45 GameScene * scene = [GameScene sharedGameScene]
forty-six
47 float nextx=scene.ship.position.x
48 float nexty=scene.ship.position.y
forty-nine
50 nextx+=direction.x * (power*8)
51 nexty+=direction.y * (power*8)
fifty-two
53 scene.ship.position=ccp (nextx,nexty)
54}
55}
fifty-six
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
Run the effect diagram:
The following is the download link of the two class libraries, if you need it, you can download it and have a look.
/ Files/xuling/CCJoystick.rar
/ Files/xuling/SneakyInput.rar
Ps: attention, our newcomers. When using the latest cocos2d, check out the AppDelegate.m.
[glView setMultipleTouchEnabled:YES]; whether it is set to YES. When I first started, I didn't set it up and checked it for a long time. Hey, there's a la carte.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.