Cocos2d-x Series 4 common controls

Source: Internet
Author: User

1. Simple code analysis
Create a project and analyze the code

Main. cpp

int main(int argc, char **argv){    // create the application instance    AppDelegate app;    return Application::getInstance()->run();}

AppDelegate execution

Bool AppDelegate: applicationDidFinishLaunching () {// initialize ctor create ''auto ctor = director: getInstance (); auto glview = director-> getOpenGLView (); if (! Glview) {glview = GLView: create ("CGame"); ctor-> setOpenGLView (glview);} // turn on display FPS director-> setDisplayStats (true ); // set FPS. the default value is 1.0/60 if you don't call this ctor-> setAnimationInterval (1.0/60); // create a scene. it's an autorelease object Scene * scene = Scene: create (); // create a scenario Layer * layer = Layer: create (); // create a layer scene-> addChild (layer); // Add the layer to the scenario
Sprite * sprite = Sprite: create ("HelloWorld.jpg"); // create a sprite-> setAnchorPoint (Point (); // set the anchorpoint ), the default value is (0.5, 0.5); layer-> addChild (sprite); // Add sprite (sprite) to layer // run // director-> runWithScene (scene ); use runWithScene director-> runWithScene (scene) for the first time; // director-> replaceScene (); then use replaceScene return true ;}

2. text display and Input
> 1. LabelTTF text display label

Size size = Director: getInstance ()-> getVisibleSize (); // get the current screen Size LabelTTF * label = LabelTTF: create (); label-> setString ("Hello cocos2d-x"); label-> setFontSize (36); label-> setPosition (size. width/2, size. height/2); addChild (label); // Add a genie

> 2. TextFieldTTF text input label

Bool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size size Size = Director: getInstance ()-> getVisibleSize (); TextFieldTTF * tf = TextFieldTTF :: textfieldwit2d-aceholder ("input here", "", 20); tf-> setPosition (size. width/2, size. height/2); addChild (tf); auto listener = EventListenerTouchOneByOne: create (); // [] the function closure is actually a code block and the tf variable cannot be accessed, to access tf, pass tf to the closure code block []; listener-> onTouchBegan = [tf] (Touch * t, Event * e) {if (tf-> getBoundingBox (). containsPoint (t-> getLocation () {tf-> attachWithIME ();} else {tf-> detachWithIME ();} return false ;}; // Add listener ctor:: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, tf); return true ;}

> 3. Message prompt

  • Log: log ("hello cocos2d-x % d", 200); // log can be output on each platform, usage is similar to printf ();
  • Message pop-up box: MessageBox ("Message Title", "message content ");

3. Custom class
Since cocos2d is extended from objective-c, the encoding style of objective-c is extended. Let's look at a custom Sprite.
Ball. h

# Ifndef BALL_H # define BALL_H # include "cocos2d. h "using namespace cocos2d; class Ball: public cocos2d: Sprite {public: CREATE_FUNC (Ball); // macro constructor, Ball: create ();

Virtual bool init () {initWithFile ("Ball.jpg"); // in objective-c, an initialization method usually starts with init and returns true ;};}; # endif/* BALL_H */

 

    // create a scene. it's an autorelease object    auto scene = Scene::create();    auto b = Ball::create();    b->setPosition(200,200);    scene->addChild(b);    director->runWithScene(scene);

 

4. Use of TableView
TableView is similar to ListView in Android. It also includes the Adapter (TableViewDataSource) and the item Click Event (TableViewDelegate). Here is a simple example;
HelloWorldScene. h

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC_EXT;USING_NS_CC;class HelloWorld : public cocos2d::Layer,TableViewDataSource,TableViewDelegate{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();          // implement the "static create()" method manually    CREATE_FUNC(HelloWorld);    public:        virtual Size cellSizeForTable(TableView *table);        virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx);        virtual ssize_t numberOfCellsInTableView(TableView *table);    public:    virtual void scrollViewDidScroll(ScrollView* view){};       virtual void scrollViewDidZoom(ScrollView* view){};        virtual void tableCellTouched(TableView* table, TableViewCell* cell);};#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene. cpp

# Include "HelloWorldScene. h "USING_NS_CC; Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize your instancebool HelloWorld: init () {/////////////////////// //// // 1. super init first if (! Layer: init () {return false;} TableView * TV = TableView: create (this, Size (300,300); TV-> setAnchorPoint (Point (0, 0 )); // set the TV-> setPosition (); TV-> setDelegate (this); // set the row Click Event addChild (TV); return true;} Size HelloWorld :: cellSizeForTable (TableView * table) {// return the Size of each TableViewCell. return Size (300, 50);} TableViewCell * HelloWorld: tableCellAtIndex (TableView * table, ssize_t idx) {// return a TableViewCell, which is equivalent to the getView method TableViewCell * cell = table-> dequeueCell (); LabelTTF * label; if (cell = NULL) of the BaseAdapter in android) {cell = TableViewCell: create (); label = LabelTTF: create (); label-> setTag (2); label-> setFontSize (20 ); label-> setAnchorPoint (Point (0, 0); cell-> addChild (label);} else {label = (LabelTTF *) cell-> getChildByTag (2 );} label-> setString (StringUtils: format ("label % d", (int) idx); return cell;} ssize_t HelloWorld: numberOfCellsInTableView (TableView * table) {// return the number of rows in TableView return 100;} void HelloWorld: tableCellTouched (TableView * table, TableViewCell * cell) {// LabelTTF * label = (LabelTTF *) cell-> getChildByTag (2); log ("click % s", label-> getString (). c_str ());}

5. menu display

MenuItem * n1 = MenuItemImage: create ("normal.png", "press.png", [] (Ref * r) {log ("menu1 click ");}); menuItem * n2 = MenuItemImage: create ("press.png", "normal.png", [] (Ref * r) {log ("menu2 click ");}); // generate a menu and add the Click Event n1-> setPosition (Point (s. width/2-60, s. height/2-40); // The position of menu 1 n2-> setPosition (Point (s. width/2-60, s. height/2-100); // The position of menu 2: auto Menu = menu: create (n1, n2, NULL); // generate menu addChild (Menu );

 

 

>>. Continue to supplement later...

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.