"COCOS2DX" Use volume spiral control to control the blood bar

Source: Internet
Author: User
Tags addchild

In fact, the main use of a small example to illustrate how two controls are used, one is the cccontrolpotentiometer volume spiral control, and the other is how to use the Cccontrolslider control to complete the game in the common Blood bar.

, with the twisting of the volume spiral control, the blood bar is changing.


The production process is as follows:


First, basic preparation

1, first also use (cocos2d-x-2.2.6 installation directory) \tools\project-creator under the create_project.py with the Python command to create a project called Potentiometerslider. Here in the "COCOS2DX" Windows platform cocos2dx 2.x download, installation, configuration, create their own HelloWorld "(Click to open the link) has said, no longer repeat.

2, after the first open (cocos2d-x-2.2.6 installation directory) \samples\cpp\testcpp\resources\images, copy two pictures to Potentiometerslider Resource folder resource, R1.png and R2.png, respectively, will be used as the background of this volume spiral control.


3, at the same time, as shown, the beginning and accessories, drawing, their own hand-drawn 3 pictures, one is the size of 1x1, Nothing at all. Picture 1.png, one is 100x100 background for pure yellow yellow.png, a 100x100 background approximately black gray black.png, this two pictures directly with the Fill tool, as long as the ctrl+e adjust the size can be. Saved directly to the project's Resource folder.

COCOS2DX Resources folder in the "Cocos2dx" Resource folder, play background music, import external library (click Open link) has been introduced, here no longer repeat.


II. Preparation of procedures

1. Open the HelloCpp.sln in the project \potentiometerslider\proj.win32, first modify the declaration of the callback function (the execution method of the component event) in Helloworld.h, and pay attention to introducing the corresponding class and namespace.

#include "cocos2d.h" #include "cocos-ext.h"//Use this component, you must require a header file using_ns_cc_ext;//Use this component, you must need the namespace  class Helloworld:public cocos2d::cclayer{public://here ' s a difference. Method ' init ' in cocos2d-x returns BOOL, instead of the returning ' ID ' in cocos2d-iphonevirtual bool Init ();  There ' s no ' id ' in CPP, so we recommend returning the class instance pointerstatic cocos2d::ccscene* scene ();//A Selec Tor callbackvoid ValueChange (ccobject* psender,cccontrolevent event);//Declare the callback function of the Cccontrolpotentiometer volume spiral control at the time of the twist Implement the "Static node ()" Method Manuallycreate_func (HelloWorld);

2, after the HelloWorldScene.cpp modification, in fact, this part and "COCOS2DX" Use Cccontrolbutton Create button, button click event, click on the event of the component to get, setposition coordinate problem " (Click to open link) is also the core issue of component operations. This part is primarily a callback function that initializes the two components with the Cccontrolpotentiometer volume spiral control being twisted.

#include "HelloWorldScene.h" #include "cocos-ext.h"//Use this component, you must need the header file using_ns_cc; using_ns_cc_ext;//Use this component, you must have the required namespace//define two macro variables, mainly about the maximum and minimum value of "Blood Bar" # define Control_slider_min 0#define Control_slider_        MAX 10000ccscene* Helloworld::scene () {//' scene ' is an Autorelease object ccscene *scene = Ccscene::create ();    ' Layer ' is an Autorelease object HelloWorld *layer = Helloworld::create ();    Add layer as a child to scene scene->addchild (layer); Return the scene return scene;} On "Init" need to initialize your Instancebool helloworld::init () {//Get screen size, location information, etc. ccsize visiblesize = Ccdir  Ector::shareddirector ()->getvisiblesize (); Create a new Volume spiral control cccontrolpotentiometercccontrolpotentiometer *potentiometer=cccontrolpotentiometer::create ("R1.png" , "R2.png", "closeselected.png");//1th parameter is the button does not twist part of the background picture, the 2nd is the button twist part of the background image, 3rd is the button theme picture Potentiometer->setposition ( CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2));//This component is centered potentiometer->addtargetwithactionforcontrolevents (This, Cccontrol_selector (Helloworld::valuechange), cccontroleventvaluechanged);// The 3rd parameter declares that the callback function when this component is twisted is the function this->addchild (potentiometer) indicated by the 2nd parameter;//Add this control to the scene, and the default does not automatically add// Create a new blood bar cccontrolslidercccontrolslider *controlslider = cccontrolslider::create ("Black.png", "Yellow.png", "1.png"); /The 1th parameter is the background image of the section where the blood bar is not occupied, and the 2nd is the background image of the part that the blood bar occupies, and the 3rd parameter is the condition button. Controlslider->setposition (CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT-VISIBLESIZE.HEIGHT/6));// This component is arranged at a distance above the window height of 1/6, the horizontal center//Set the button maximum, minimum value of the Datum controlslider->setminimumvalue (control_slider_min); Controlslider->setmaximumvalue (Control_slider_max);//------Controlslider->setvalue (CONTROL_SLIDER_MIN); /Set Button current value//originally Cccontrolslider is for the user to adjust, the adjustment button is 1.png, but 1.png is a 1x1 near invisible picture, while using settouchenabled (false) to lock this button, Disable user throttling controlslider->settouchenabled (false); This->addchild (controlslider,0,1);//Add this control to the scene, Also set tag to 1return true;} void Helloworld::valuechange (ccobject* psender,cccontrolevent event) {//This HelloWorld:: The namespace cannot be saved, although in this scenario, but it is missing, Unable to compile Cccontrolpotentiometer *potentIometer= (Cccontrolpotentiometer *) psender;//corresponds to this function, which refers to the volume spiral control that is being monitored on behalf of Cccontrolpotentiometercccontrolslider * Controlslider = (Cccontrolslider *) This->getchildbytag (1);//Get Blood strips//potentiometer->getvalue (), Gets the value of the cccontrolpotentiometer of the volume spiral control, which is a value of 0-1, which is the percentage of the total portion of the helix that occupies the//controlslider->setvalue, and sets the values of the blood bars. Although controlslider->settouchenabled (false), it is still possible to control the Controlslider->setvalue with code (Potentiometer->getvalue () *control_slider_max);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"COCOS2DX" Use volume spiral control to control the blood bar

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.