cocos2d-x 常用代码、高频使用代码【CCLayer、CCEditBox编辑框、CCControlButton按钮、CCLabelTTF标签、CCSpriteBatchNode渲染、CCMen】

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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值