Cocos2d-x beginner guide, cocos2d-x Guide

Source: Internet
Author: User

Cocos2d-x beginner guide, cocos2d-x Guide

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);    addChild(normalS,1);    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 + getPositionX(),r.origin.y + 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 + getPositionX(),r.origin.y + 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); addChild (a, 1); a-> autorelease (); B _Button * B = new B _Button; B-> initWithImageAndMethodAndPriority ("CloseNormal.png", "CloseSelected.png ", HELLO_LAYER_BUTTON_ B, shutdownGame, this, 1); B-> setPosition (CCPoint (win. width/2, win. height/2); 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, win. height/2); 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 ));
addChild(bb, 2);    bb->autorelease();        return true;}#ifdef __cplusplusextern "C" {#endif    void shutdownGame(int a)    {        switch (a) {            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(" A !\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.


NBA2KOL is a beginner's Guide. There is a special training session for gamers. Two Special Training cards must be used. There is only one special training card. How can this problem be solved?

This beginner Guide is based on the main task.

During the guidance of special trainings for gamers, the system sends a special training card to the players, and the other has a corresponding growth task, the target of the task is to play 10 matching matches. You only need to finish 10 matching matches to receive a special training card for the task reward, for details, click the taskbar to view details.

Game Development novice teach Cocos2D-X how to learn, only C ++ development experience

Congratulations, cocos2d-x is written in c ++, theory can refer to the relevant cocos2d. There are many cocos2d communities on the Internet. Let's take a look! Visit more forums. We need to understand scenarios, layers, Genie, actions, and other related concepts.

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.