加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x 实现游戏中的系统公告

发布时间:2020-12-14 19:54:54 所属栏目:百科 来源:网络整理
导读:CTestLayer.h #ifndef __TEST_LAYER__#define __TEST_LAYER__#include "cocos2d.h"USING_NS_CC;class CTestLayer : public cocos2d::CCLayer{public:CTestLayer(void);~CTestLayer(void);virtual bool init();CREATE_FUNC(CTestLayer);virtual void update(f


CTestLayer.h

#ifndef __TEST_LAYER__
#define __TEST_LAYER__

#include "cocos2d.h"
USING_NS_CC;

class CTestLayer : public cocos2d::CCLayer
{
public:
	CTestLayer(void);
	~CTestLayer(void);

	virtual bool init();
	CREATE_FUNC(CTestLayer);

	virtual void update(float dt);

	CCLabelTTF* text1;
	CCLabelTTF* text2;
};

#endif //__TEST_LAYER__



CTestLayer.cpp

#include "TestLayer.h"

CTestLayer::CTestLayer(void)
{
}

CTestLayer::~CTestLayer(void)
{
}

bool CTestLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!CCLayer::init());

		CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();
		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui_serverlist.plist");

		//垂直滚动字幕
		CCSprite* listbase=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("login_listbase.png"));
		listbase->setPosition(ccp(visibleSize.width/2,visibleSize.height/2+10));
		this->addChild(listbase);
		text1=CCLabelTTF::create("1.Hi! Welcome to JackyStudio,nMy Blog isnblog.csdn.net/jackystudio!n2.Hi! Welcome to JackyStudio,nMy Blog isnblog.csdn.net/jackystudio!n3.Hi! Welcome to JackyStudio,nMy Blog isnblog.csdn.net/jackystudio!n4.Hi! Welcome to JackyStudio,nMy Blog isnblog.csdn.net/jackystudio!","Arial",12);
		text1->setHorizontalAlignment(kCCTextAlignmentLeft);
		text1->setAnchorPoint(ccp(0.5,1));
		text1->setPosition(ccp(visibleSize.width/2,visibleSize.height/2-55));
		this->addChild(text1);
		CCSprite* fg=CCSprite::create("fg.png");
		fg->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
		this->addChild(fg);

		//水平滚动公告
		CCSprite* textbase=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("login_textbase.png"));
		textbase->setPosition(ccp(visibleSize.width/2,50));
		textbase->setScaleX(2.5f);
		this->addChild(textbase);
		text2=CCLabelTTF::create("Hi! Welcome to JackyStudio,My Blog is blog.csdn.net/jackystudio!",12);
		text2->setPosition(ccp(visibleSize.width+text2->getContentSize().width/2,50));
		text2->setColor(ccc3(255,0));
		this->addChild(text2);

		this->scheduleUpdate();

		bRet = true;
	} while (0);

	return bRet;
}

void CTestLayer::update( float dt )
{
	CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();

	int newY = text1->getPositionY()+1;
	if (newY == 500)
	{
		newY = visibleSize.height/2-55;
	}
	text1->setPositionY(newY);

	int newX = text2->getPositionX()-1;
	if (newX <= -text2->getContentSize().width/2)
	{
		newX = visibleSize.width+text2->getContentSize().width/2;
	}
	text2->setPositionX(newX);
}


使用:

CTestLayer* testLabel = CTestLayer::create();
		CC_BREAK_IF(!testLabel);
		this->addChild(testLabel);

运行效果:



??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读