Cocos2dx knowledge-virtual joystick

Source: Internet
Author: User

[Nagging]

If the mobile phone device does not have a handle or keyboard, you can only operate the game by touching the screen.

The role of the virtual joystick is to create several buttons in the game to simulate the handle, move up and down, attack, jump, and make big moves through touch.

As shown below, there is a mobile virtual joystick and a key.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/B2/wKiom1QK2S6wr9OOAAIvVx2bcvs056.jpg "Title =" 20131217223639500.png" alt = "wkiom1qk2s6wr9ooaaivvx2bcvs056.jpg"/>

Open source sneakyinput virtual joystick, Open Source Address: https://github.com/Ntran013/SneakyInput

Here, the blogger further modifies the open-source sneakyinput to facilitate the use of the blogger.


[Demo download]

Http://down.51cto.com/data/1871368



[Virtual joystick]


1. Description

Sneakyinput is an open-source project. You only need to modify the image, button image, and position used by the joystick.

Therefore, you only need to modify"// -- Need modify start --"Section, change the button image of the virtual joystick to your own image.


2. Usage

(1) include the sneaky. h header file in your game. H class.

(2) Add the virtual joystick in the initialization function Init () of game. cpp.

Sneaky * sneaky = new sneaky ();

This-> addchild (sneaky, 1,999 );

(3) moves and buttons are generally carried out in game Update.

(4) Mobile:

// Obtain the direction vector. DV is the standardization vector, that is, the length is 1.

Ccpoint DV = sneaky-> joystick-> getvelocity ();

// Move characters

SP-> setposition (SP-> getposition () + DV );

(5) button:

// Determine whether button a is pressed

Sneaky-> buttona-> getisactive ()


3. code example

// Bool flag = false; // whether to execute the key action void game: Update (float DT) {ccnode * sp = This-> getchildbytag (1 ); // move int movespeed = 2; ccpoint DV = sneaky-> joystick-> getvelocity (); SP-> setposition (SP-> getposition () + DV * movespeed ); // press the IF (sneaky-> buttona-> getisactive ()&&! Flag) {ccjumpby * Jump = ccjumpby: Create (0.5f, CCP (),); // jump cccallfunc * func = cccallfunc: Create (this, callfunc_selector (game: setflag); SP-> runaction (ccsequence: Create (jump, func, null); flag = true ;}} void game: setflag () {flag = false ;}//



This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1549666

Cocos2dx knowledge-virtual joystick

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.