cocos2dx常用功能-Cocos2dx开发常用控件详解(共11部分)

前端之家收集整理的这篇文章主要介绍了cocos2dx常用功能-Cocos2dx开发常用控件详解(共11部分)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2dx游戏开发 第三阶段:常用功能1

1---Cocos2dx常用功能-Cocos2dx开发常用控件详解
//是否显示左下角的帧频数据
director->setDisplayStats( true );
// set FPS. the default value is 1.0/60 if you don't call this
//显示每一帧的时间间隔
director->setAnimationInterval(1.0 / 10);
2.Cocos2d-x 之Sprite 视频很重点
Sprite *s = Sprite :: create ( "img.jpg" );
s->
setAnchorPoint ( Point ( 0 , 0 ));
s->
setPosition ( 100 ,216)">200 ));
addChild(s);
3.Cocos2d-x之Director、Scene、Layer
Scene *scene = Scene :: create ();
Layer *layer = Layer :: create ();
scene->
addChild (layer);

"HelloWorld.png" );
s->
0 ));
layer->addChild(s);
4.Cocos2d-x之Log
跨平台日志输出API
log( "Hello Cocos2dx %d" ,216)">200 );
5.Cocos2d-x之MessageBox
MessageBox ( " 消息内容 " , 消息标题 " );
attach /ə'tætʃ/ 附加;附属 detach /dɪ'tætʃ/ vt. 分离;派遣
6.Cocos2d-x之LabelTTF:
Size size = Director :: getInstance ()-> getVisibleSize ();
LabelTTF *label = LabelTTF :: create ();
label->
setString ( "Hello jikexueyuan" );
label->
setFontSize ( 36 );
label->
setPosition (size. width / 2 ,size. height / 2 );
addChild(label);
7.Cocos2d-x之TextFieldTTF
Size visibleSize = getVisibleSize ();

TextFieldTTF *tf = TextFieldTTF :: textFieldWithPlaceHolder ( 在这里输入 宋体 20 );

tf->
setPosition (visibleSize. 2 );

addChild (tf);


auto listener = EventListenerTouchOneByOne :: create ();
listener->
onTouchBegan = [tf]( Touch *t, Event *event){

if (tf-> getBoundingBox (). containsPoint (t-> getLocation ())) {
tf->
attachWithIME ();
}
else {
tf->
detachWithIME ();
}

return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,tf);

8.Cocos2d-x之创建自定义
Ball.h中
#include <iostream>
#include
"cocos2d.h"

using namespace cocos2d;


class Ball: public Sprite {

public :
virtual bool init();

CREATE_FUNC(Ball);

// static Ball* create(){
//
// Ball * b = new Ball();
// b->init();
// b->autorelease();
// return b;
// }
};

Ball.cpp中
"Ball.h"
bool Ball::init(){
initWithFile( "ball.png" );
true ;
}


HelloWorldScene.cpp 中:
auto b = Ball::create() ;
b->setPosition(200,200);
addChild(b);

9.Cocos2d-x之内存管理机制
Object *obj = new Object();
obj->autorelease();
log("count:%d",obj->getReferenceCount());
永远不要使用delete obj

10.Cocos2d-x之Menu
闭包是C++11的特性
auto menu = Menu::create(MenuItemImage::create( "normal.png" ,27)">"selected.png" ,[](Object* obj){
log("menu item touched);//松开手后执行
}), NULL );
addChild(menu);

11.Cocos2d-x之TableView
有一个列表项它曾经呈现出来了,在往上面拖动的时候 处于不呈现的状态(拖出去界面)这个列表项会被回收
放到TableView的一个队列里面,如果有新的列表项需要呈现出来,会先从回收的队列里面去查找有没有已经回收过的,有回收过得会直接使用,没有回收过得需要重新创建

HelloWorldScene.h:
"cocos2d.h"
<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
bool init();

// a selector callback
void menuCloseCallback(Object* pSender);

// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);


/**
* cell height for a given table.
*
*
@param table table to hold the instances of Class
*
@return cell size
*/

virtual Size cellSizeForTable(TableView *table);
/**
* a cell instance at a given index
*
*
idx index to search for a cell
*
cell found at idx
*/

virtual TableViewCell* tableCellAtIndex(TableView *table,ssize_t idx);
/**
* Returns number of cells in a given table view.
*
*
number of cells
*/

virtual ssize_t numberOfCellsInTableView(TableView *table);

public :

/**
* Delegate to respond touch event
*
*
table table contains the given cell
*
cell cell that is touched
* @js NA
* @lua NA
*/

void tableCellTouched(TableView* table,TableViewCell* cell);

/**
* @js NA
* @lua NA
*/

void scrollViewDidScroll(ScrollView* view){};
void scrollViewDidZoom(ScrollView* view){};
};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp:
"HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
false ;
}

TableView * tv = TableView::create(
this ,Size( 300 ,216)">300 ));
tv->setAnchorPoint(Point(
0 ));
tv->setPosition(
0 );
tv->setDelegate(
this );
addChild(tv);


true ;
}


Size HelloWorld::cellSizeForTable(cocos2d::extension::TableView *table){
return Size( 50 );
}

TableViewCell* HelloWorld::tableCellAtIndex(cocos2d::extension::TableView *table,ssize_t idx){

TableViewCell *cell = table->dequeueCell();
LabelTTF *label;

if (cell== NULL ) {
cell = TableViewCell::create();
label = LabelTTF::create();
label->setTag(
2 );
label->setFontSize(
30 );
label->setAnchorPoint(Point(
0 ));
cell->addChild(label);
}
else {
label = (LabelTTF*)cell->getChildByTag(
2 );
}

label->setString(StringUtils::format(
"Label %ld" ,idx));

return cell;
}


ssize_t HelloWorld::numberOfCellsInTableView(cocos2d::extension::TableView *table){
return 100 ;
}


void HelloWorld::tableCellTouched(cocos2d::extension::TableView *table,cocos2d::extension::TableViewCell *cell){

LabelTTF *label = (LabelTTF*)cell->getChildByTag(
2 );
log(
"%s" ,label->getString().c_str());

}


void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit( 0 );
#endif
}

猜你在找的Cocos2d-x相关文章