Cocos2dx Game Development Series notes 15.1: Add a virtual joystick SneakyInput

Source: Internet
Author: User

 

(It seems that there is a problem with saving the csdn blog)


(When I have finished saving all the files, I owe my hand to click the return button of the browser, and then the automatic saving of csdn is not easy to use, and now this note is the second time T.T)
Today's Christmas
Bone is not romantic with Zhe
The cat is watching the video in bed and eating fruit snacks.
Sugar cane with bones beside it
Eat it after finishing this article.




SneakyInput:
An open source project address: https://github.com/Ntran013/SneakyInput
REFERENCE Note 8: Legend of war






= 1 =
Add the Sneaky code.




= 2 =
In a separate Layer, declare a button and a joystick:
Public:
HudLayer ();
Bool init ();


SneakyButton * mButtonA;
SneakyJoystick * mJoystick;


// Void inputUpdate (float dt );
};
Add this separate Layer in GameScene:
GameScene: GameScene ()
{
MHudLayer = new HudLayer ();
This-> addChild (mHudLayer, 1 );
MGameLayer = new GameLayer ();
This-> addChild (mGameLayer, 0 );
Init ();
This-> schedule (schedule_selector (GameScene: inputUpdate ));
}
This HudLayer control layer adds the game logic layer.
Void GameScene: inputUpdate (float dt)
{
CCPoint> // std: cout <velocity. x <std: endl;


If (velocity. x >=0.4f> velocity. y >=0.4f> mGameLayer-> mHero-> using withdirection (velocity );
}> MGameLayer-> mHero-> idle ();
}


If (attackInterval <= 0.0f ){
If (mHudLayer-> mButtonA-> getIsActive ()){
MGameLayer-> mHero-> attack ();
AttackInterval = 0.5f;
......
}
}
In this method, first obtain the speed of the joystick. If the absolute speed value exceeds 0.4, the corresponding action is used to move the protagonist.
Then, determine whether the button mButtonA is pressed. If yes, the attack will be performed.
A variable is used to control the attack interval: 0.5 seconds for attack detection.


= 4 =
The following describes how to initialize the joystick and attack buttons:
In the HudLayer:
Joystick initialization:
CCSpriteFrameCache *> CCSpriteFrameCache: sharedSpriteFrameCache ();
Cache-> addSpriteFramesWithFile (UI. plist, UI.png );


MJoystick = NULL;
MJoystick = new SneakyJoystick ();
MJoystick-> initWithRect (CCRectZero); // use the origin to initialize
MJoystick-> setAutoCenter (true); // auto center
MJoystick-> setHasDeadzone (true); // The setting is invalid.
MJoystick-> setDeadRadius (10); // set the radius of the invalid Region
SneakyJoystickSkinnedBase * jstickSkin = new SneakyJoystickSkinnedBase ();
JstickSkin-> autorelease ();
JstickSkin-> init ();
JstickSkin-> setBackgroundSprite (CCSprite: createWithSpriteFrameName(JoyStick-base.png ));
JstickSkin-> setThumbSprite (CCSprite: createWithSpriteFrameName(JoyStick-thumb.png ));
// JstickSkin-> getThumbSprite ()-> setScale (0.5f );
JstickSkin-> setPosition (ccp (50, 50 ));
JstickSkin-> setJoystick (mJoystick );
This-> addChild (jstickSkin );
Attack button Initialization
MButtonA = NULL;
MButtonA = new SneakyButton ();
MButtonA-> initWithRect (CCRectZero );
MButtonA-> setIsToggleable (false );
MButtonA-> setIsHoldable (true );
SneakyButtonSkinnedBase *> btnASkin-> autorelease ();
BtnASkin-> init ();
BtnASkin-> setPosition (ccp (430, 50 ));
BtnASkin-> setdefasprsprite (CCSprite: createWithSpriteFrameName(button-default.png ));
BtnASkin-> setPressSprite (CCSprite: createWithSpriteFrameName(button-pressed.png ));
BtnASkin-> setActivatedSprite (CCSprite: createWithSpriteFrameName(button-activated.png ));
// BtnASkin-> setDisabledSprite (CCSprite: createWithSpriteFrameName(button-disabled.png ));
BtnASkin-> setButton (mButtonA );
This-> addChild (btnASkin );
Well, it's so easy to use the joystick. Thanks to the original author.



 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.