0、缩放结点
//缩放结点
void scale(CCNode *node, const CCSize &dstSize)
{
CCSize sz = node->getContentSize();
float scaleX = dstSize.width / sz.width;
float scaleY = dstSize.height / sz.height;
node->setScale(scaleX, scaleY);
}
一、创建一个CCLayer
#ifndef _MYLAYER_H_
#define _MYLAYER_H_
#include "cocos2d.h"
class MyLayer : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(MyLayer);
cocos2d::CCSize winSize;
};
#endif
CCScene* MyLayer::scene()
{
CCScene *scene = CCScene::create();
MyLayer *layer = MyLayer::create();
scene->addChild(layer);
return scene;
}
bool MyLayer::init()
{
if ( !CCLayer::init() )
{
return false;
}
winSize = CCDirector::sharedDirector()->getVisibleSize();
return true;
}
二、CCEditBox编辑框(文本框、密码框)
//添加用户名文本框
CCScale9Sprite* ssp1=CCScale9Sprite::create("textField.png");
ssp1->setPreferredSize(CCSizeMake(100, 50));
ssp1->setAnchorPoint(ccp(0.5, 0.5));
CCEditBox *nameBox = CCEditBox::create(CCSizeMake(100, 50), ssp1);
nameBox->setTag(1000);
nameBox->setPlaceHolder("输入用户名");
nameBox->setFontColor(ccc3(255, 0, 255));
nameBox->setPosition(ccp(50, 50));
nameBox->setMaxLength(16);
nameBox->setInputMode(kEditBoxInputModeAny);
this->addChild(nameBox);
三、CCControlButton按钮
//登录、退出回调函数
void LoginScene::call_login(CCObject* pObj, cocos2d::extension::CCControlEvent event)
{
}
//添加登录
CCScale9Sprite* ssp = CCScale9Sprite::create("button.png");
ssp->setPreferredSize(CCSizeMake(100, 50));
ssp->setAnchorPoint(ccp(0.5, 0.5));
CCControlButton* controlBtn = CCControlButton::create(ssp);
controlBtn->setPreferredSize(CCSizeMake(100, 50));
controlBtn->setPosition(ccp(100, 100));
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(LoginScene::call_login), CCControlEventTouchUpInside);
this->addChild(controlBtn);
四、CCLabelTTF标签
CCLabelTTF *m_lbl = CCLabelTTF::create("Hello,World", "Arial", 45.0);
m_lbl->setColor(ccc3(255, 0, 0));
m_lbl->setPosition(ccp(500, 300));
this->addChild(m_lbl);
五、CCSpriteBatchNode的使用
CCSpriteBatchNode *batch = CCSpriteBatchNode::create("1.png");
batch->setPosition(CCPointZero);
this->addChild(batch);
CCSprite *sprite = CCSprite::createWithTexture(batch->getTexture());
sprite->setPosition(ccp(100, 200));
this->addChild(sprite);
六、CCMenu、CCMenuItemImage菜单的使用
CCMenu *menu = CCMenu::create();
menu->setPosition(ccp(0, 0));
this->addChild(menu);
CCMenuItem *item = CCMenuItemImage::create("1.png", "1.png");
item->setPosition(ccp(200, 200));
menu->addChild(item);