c++随笔【文字弹跳】(cocos2d)

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

#define MOVELABEL 101


USING_NS_CC;



using namespace cocostudio::timeline;

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() )
    {
        return false;
    }
    
    origin=Director::getInstance()->getVisibleOrigin();
    visibleSize=Director::getInstance()->getVisibleSize();
    movedirection=Vec2(2,-2);

    
    auto closemenuItem=MenuItemImage::create("closeNormal.png","closeSelect.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    closemenuItem->setPosition(Vec2(origin.x+visibleSize.width-closemenuItem->getContentSize().width/2,origin.y+closemenuItem->getContentSize().height/2));
    auto menu=Menu::create(closemenuItem,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu,1);
    
    auto sprite=Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(0,0));
    sprite->setAnchorPoint(Vec2(0.0,0.0));
    this->addChild(sprite,0);
    
    auto label1 = Label::createWithSystemFont("Hello,World", "Consolas", 36);
    label1->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height*0.8));
    label1->setColor(Color3B(255, 0, 0));      //设置字体颜色
    this->addChild(label1,1);
    label1->setTag(MOVELABEL);
 
    TTFConfig config("Fangsong.ttf", 36);
    auto label2 = Label::createWithTTF(config, "你好,世界");
    label2->setPosition(Vec2(visibleSize.width*0.5+origin.x, visibleSize.height+origin.y-label2->getContentSize().height));
    label2->enableGlow(Color4B::RED);     //设置荧光效果,仅限ttf文字
    label2->enableOutline(Color4B(0, 255, 0, 255), 5);      //设置描边,描边宽度为5,仅限ttf文字
    label2->setAnchorPoint(Vec2(0.5,0.5));
    this->addChild(label2,1);
    
    this->scheduleUpdate();
    
    return true;
}


void HelloWorld::update(float dt){
    auto label=this->getChildByTag(MOVELABEL);
    if (label->getPosition().y< origin.y + label->getContentSize().height/2 || label->getPosition().y> getContentSize().height- label->getContentSize().height/2 ){
        //遇到下方或上方
        movedirection.y=-movedirection.y;
    }
    if (label->getPosition().x< origin.x + label->getContentSize().width/2 || label->getPosition().x>getContentSize().width- label->getContentSize().width/2){
        //遇到左边或右边
        movedirection.x=-movedirection.x;
    }
    movedirection.y=movedirection.y>0?int(movedirection.y+0.1):int(movedirection.y-0.1);
    movedirection.x=movedirection.x>0?int(movedirection.x+0.1):int(movedirection.x-0.1);
    label->setPosition(label->getPosition()+movedirection);
}

void HelloWorld::menuCloseCallback(Ref *pSender){
    Director::getInstance()->end();
    #if (CC_TARGET_OS_PLATFORM==CC_PLATFORM_IOS)
        exit(0);
    #endif

}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    virtual void update(float dt);

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    void menuCloseCallback(Ref *pSender);
    
    

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
private:
    cocos2d::Vec2 origin;
    cocos2d::Size visibleSize;
    cocos2d::Vec2 movedirection;
};

#endif // __HELLOWORLD_SCENE_H__


猜你喜欢

转载自blog.csdn.net/u010255642/article/details/79108144