cocos2dx常用功能-Cocos2dx开发常用控件详解(共11部分)
来源:互联网 发布:永磁同步电机控制算法 编辑:程序博客网 时间:2024/05/21 08:40
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(Point(100,200));
s->setAnchorPoint(Point(0,0));
s->setPosition(Point(100,200));
addChild(s);
3.Cocos2d-x之Director、Scene、Layer
Scene *scene = Scene::create();
Layer *layer = Layer::create();
scene->addChild(layer);
Sprite *s = Sprite::create("HelloWorld.png");
s->setAnchorPoint(Point(0,0));
Layer *layer = Layer::create();
scene->addChild(layer);
Sprite *s = Sprite::create("HelloWorld.png");
s->setAnchorPoint(Point(0,0));
layer->addChild(s);
4.Cocos2d-x之Log
跨平台日志输出API
log("Hello Cocos2dx %d",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);
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
SizevisibleSize = Director::getInstance()->getVisibleSize();
TextFieldTTF *tf = TextFieldTTF::textFieldWithPlaceHolder("在这里输入","宋体",20);
tf->setPosition(visibleSize.width/2, visibleSize.height/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;
};
TextFieldTTF *tf = TextFieldTTF::textFieldWithPlaceHolder("在这里输入","宋体",20);
tf->setPosition(visibleSize.width/2, visibleSize.height/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"
usingnamespacecocos2d;
classBall:publicSprite {
public:
virtual bool init();
CREATE_FUNC(Ball);
// static Ball* create(){
//
// Ball * b = new Ball();
// b->init();
// b->autorelease();
//
// return b;
// }
#include "cocos2d.h"
usingnamespacecocos2d;
classBall:publicSprite {
public:
virtual bool init();
CREATE_FUNC(Ball);
// static Ball* create(){
//
// Ball * b = new Ball();
// b->init();
// b->autorelease();
//
// return b;
// }
};
Ball.cpp中
#include"Ball.h"
boolBall::init(){
initWithFile("ball.png");
return true;
}
HelloWorldScene.cpp中:
autob = 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","selected.png", [](Object* obj){
log("menu item touched”);//松开手后执行
}), NULL);
addChild(menu);
11.Cocos2d-x之TableView
有一个列表项它曾经呈现出来了,在往上面拖动的时候 处于不呈现的状态(拖出去界面)这个列表项会被回收
放到TableView的一个队列里面,如果有新的列表项需要呈现出来,会先从回收的队列里面去查找有没有已经回收过的,有回收过得会直接使用,没有回收过得需要重新创建
HelloWorldScene.h:
#include"cocos2d.h"
#include<cocos-ext.h>
USING_NS_CC_EXT;
USING_NS_CC;
classHelloWorld : 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();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
public:
/**
* 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
*
* @param idx index to search for a cell
* @return cell found at idx
*/
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx);
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual ssize_t numberOfCellsInTableView(TableView *table);
public:
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
* @js NA
* @lua NA
*/
virtual void tableCellTouched(TableView* table, TableViewCell* cell);
/**
* @js NA
* @lua NA
*/
virtual void scrollViewDidScroll(ScrollView* view){};
/**
* @js NA
* @lua NA
*/
virtual void scrollViewDidZoom(ScrollView* view){};
};
USING_NS_CC_EXT;
USING_NS_CC;
classHelloWorld : 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();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
public:
/**
* 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
*
* @param idx index to search for a cell
* @return cell found at idx
*/
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx);
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual ssize_t numberOfCellsInTableView(TableView *table);
public:
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
* @js NA
* @lua NA
*/
virtual void tableCellTouched(TableView* table, TableViewCell* cell);
/**
* @js NA
* @lua NA
*/
virtual void scrollViewDidScroll(ScrollView* view){};
/**
* @js NA
* @lua NA
*/
virtual void scrollViewDidZoom(ScrollView* view){};
};
#endif// __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp:
#include"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
boolHelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
TableView * tv = TableView::create(this, Size(300,300));
tv->setAnchorPoint(Point(0,0));
tv->setPosition(100,0);
tv->setDelegate(this);
addChild(tv);
return true;
}
Size HelloWorld::cellSizeForTable(cocos2d::extension::TableView *table){
return Size(300,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,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;
}
voidHelloWorld::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell){
LabelTTF *label = (LabelTTF*)cell->getChildByTag(2);
log("%s",label->getString().c_str());
}
voidHelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
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
boolHelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
TableView * tv = TableView::create(this, Size(300,300));
tv->setAnchorPoint(Point(0,0));
tv->setPosition(100,0);
tv->setDelegate(this);
addChild(tv);
return true;
}
Size HelloWorld::cellSizeForTable(cocos2d::extension::TableView *table){
return Size(300,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,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;
}
voidHelloWorld::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell){
LabelTTF *label = (LabelTTF*)cell->getChildByTag(2);
log("%s",label->getString().c_str());
}
voidHelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
0 0
- cocos2dx常用功能-Cocos2dx开发常用控件详解(共11部分)
- cocos2dx 常用动作详解
- cocos2dx学习之常用控件的使用
- cocos2dx 3.1.1 Lua常用控件
- cocos2dx用到的常用开发工具
- cocos2dx常用方法
- cocos2dx常用宏命令
- cocos2dx 常用的基本功能
- cocos2dx常用基本语句
- cocos2dx常用宏
- cocos2dx 常用动作使用指南
- Cocos2dx中常用的C++11特性
- cocos2dx 常见的49中动作详解及常用函数
- cocos2dx 常用的构建工具
- Cocos2dx 常用的常量、工具
- cocos2dx 几种常用action
- Cocos2dx常用宏必须知道
- cocos2dx 常用宏和函数
- 关于协方差矩阵的理解
- 脚本入门(继承 多态 索引器)
- 计算机组装与维护试题及答案
- 设计模式:Registry of Singleton模式
- kde 桌面 eclipse崩溃
- cocos2dx常用功能-Cocos2dx开发常用控件详解(共11部分)
- LayoutInflater和inflate()方法的用法
- poj2485 prime算法+距离表
- 《数据结构》 第四章 字符串和多维数组 知识总结导图
- NSThread 使用
- JavaFX高级教程:JavaFX2.0的FXML语言
- jQuery Mobile学习笔记(二)——框架起步
- C语言画出菱形
- HDU 2066 一个人的旅行 【Dijkstra】