Cocos2d-x beginner Guide

Source: Internet
Author: User

Cocos2d-x novice guide game development game UI

Novice guidance is a required module in game development. The following code enables only one button to be clicked on the entire game interface, which is just a required function for novice guidance.

First, define a button. The parameters of this button have priority, proxy for method implementation, priority, and so on:

////  B_Button.h//  HelloCpp////  Created by blary on 14-8-16.////#ifndef __HelloCpp__B_Button__#define __HelloCpp__B_Button__#include <iostream>#include "cocos2d.h"using namespace cocos2d;class B_Button : public CCLayer, public CCTargetedTouchDelegate{public:    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);    bool initWithImageAndMethodAndPriority(const char* normal, const char* selected, int n, void(*f)(int), CCObject* tar, int priority = kCCMenuHandlerPriority);private:    CCSprite* normalS;    CCSprite* selectedS;    CCObject* target;    int pid;    void (*handler)(int);};#endif /* defined(__HelloCpp__B_Button__) */
The implementation section is as follows:

////  B_Button.cpp//  HelloCpp////  Created by blary on 14-8-16.////#include "B_Button.h"bool B_Button::initWithImageAndMethodAndPriority(const char* normal, const char* selected, int n, void(*f)(int), CCObject* tar, int priority){    if (!CCLayer::init()) {        return false;    }    normalS = CCSprite::create(normal);    selectedS = CCSprite::create(selected);    target = tar;    pid = n;        normalS->setVisible(true);    selectedS->setVisible(false);    this->addChild(normalS,1);    this->addChild(selectedS,1);    handler=f;    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate((CCTargetedTouchDelegate*)this, priority, true);    return true;}bool B_Button::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){    CCPoint p = pTouch->getLocation();    p = CCDirector::sharedDirector()->convertToUI(p);    CCRect  r = selectedS->boundingBox();    CCRect rect(r.origin.x+this->getPositionX(),r.origin.y+this->getPositionY(),r.size.width,r.size.height);    if (rect.containsPoint(p))    {        normalS->setVisible(false);        selectedS->setVisible(true);    }    return true;}void B_Button::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){    if (normalS->isVisible()==false&&selectedS->isVisible()==true)    {        normalS->setVisible(false);        selectedS->setVisible(true);    }    CCPoint p = pTouch->getLocation();    p = CCDirector::sharedDirector()->convertToUI(p);    CCRect  r = selectedS->boundingBox();    CCRect rect(r.origin.x+this->getPositionX(),r.origin.y+this->getPositionY(),r.size.width,r.size.height);    if (rect.containsPoint(p)) {        (handler)(pid);        normalS->setVisible(true);        selectedS->setVisible(false);    }}
The following section describes how to use it:

#pragma once#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayer{public:virtual bool init();      static cocos2d::CCScene* scene();        CREATE_FUNC(HelloWorld);};#ifdef __cplusplusextern "C" {#endif        void shutdownGame(int a);#ifdef __cplusplus}#endif

# Include "helloworldscene. H "# include" B _button.h "using namespace cocos2d; // The enumerated values are defined by layer name + function Enum {role, hello_layer_button_aa, role, hello_layer_button_bb,}; ccscene * helloworld :: scene () {ccscene * scene = NULL; do {scene = ccscene: Create (); helloworld * layer = helloworld: Create (); scene-> addchild (layer);} while (0); Return scene;} bool helloworld: Init () {If (! Cclayer: Init () {return false;} ccsize win = ccdirector: shareddire()-> getwinsize (); // you can use the default priority button B _button * A = new B _button; A-> priority ("closenormal.png", "closeselected.png", hello_layer_botton_a, shutdowngame, this, 0); A-> setposition (ccpoint (win. width/2, win. height/2); this-> addchild (A, 1); A-> autorelease (); B _button * B = new B _button; B-> initwithim Ageandmethodandpriority ("closenormal.png", "closeselected.png", hello_layer_button_ B, shutdowngame, this, 1); B-> setposition (ccpoint (win. width/2, win. height/2); this-> addchild (B, 1); B-> autorelease (); B _button * AA = new B _button; AA-> initwithimageandmethodandpriority ("closenormal.png ", "closeselected.png", hello_layer_button_aa, shutdowngame, this, 0); AA-> setposition (ccpoint (win. width/2 + 100, W In. height/2); this-> addchild (AA, 2); AA-> autorelease (); B _button * BB = new B _button; bb-> initwithimageandmethodandpriority ("closenormal.png ", "closeselected.png", hello_layer_button_bb, shutdowngame, this, 1); bb-> setposition (ccpoint (win. width/2 + 100, win. height/2); this-> addchild (BB, 2); bb-> autorelease (); Return true ;} # ifdef _ cplusplusextern "C" {# endif void shutdowngame (int A) {Switch () {Case hello_layer_button_bb: {printf ("BB! \ N ") ;}break; Case hello_layer_button_ B: {printf (" B! \ N ") ;}break; Case hello_layer_button_aa: {printf (" Aa! \ N ") ;}break; Case hello_layer_botton_a: {printf ("! \ N ") ;}break; default: Break ;}# ifdef _ cplusplus} # endif

In this way, only one button event is guaranteed, even if they have the same priority. Based on the above implementation, we can easily implement the development of the novice guide module.

Cocos2d-x beginner Guide

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.