实现一个demo,具备以下功能:

1.让几个字分别位于中间和四个角落。

2.中间的字体改变,并且带有闪烁功能。

3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触摸设置为true。

4.下面两个按钮能够实现添加节点和移除节点的作用。

效果图:


实现代码:

HelloWorldScene.h:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld); void menuRemoveCallback(CCObject* pSender); //启动触屏事件
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //触摸注册事件
virtual void registerWithTouchDispatcher(); //单点触摸事件
virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); //移动事件
virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
}; #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" using namespace cocos2d;
using namespace CocosDenshion; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} bool HelloWorld::init()
{ if ( !CCLayer::init() )
{
return false;
}
//设置当前允许触摸
this->setTouchEnabled(true); CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2- 30, 20) ); CCMenuItemImage *pCloseItem1 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuRemoveCallback) );
pCloseItem1->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2 + 30, 20) ); CCMenu* pMenu = CCMenu::create(pCloseItem1,pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("江苏理工", "Thonburi", 34);
CCSize size = CCDirector::sharedDirector()->getWinSize();
//一开始设置为绿色
pLabel->setColor(ccGREEN);
pLabel->setPosition( ccp(size.width / 2, size.height / 2) );
this->addChild(pLabel,1); //让节点闪烁的方法
CCAction *action = CCBlink::create(5, 20);
pLabel->runAction(action);
//变色的方法
CCAction *action1 = CCTintTo::create(5, 255, 0, 0);
pLabel->runAction(action1); //左上角显示姓名
CCLabelTTF* pLabel1 = CCLabelTTF::create("丁小未", "Thonburi", 34);
CCSize size1 = CCDirector::sharedDirector()->getWinSize();
pLabel1->setAnchorPoint(ccp(0, 1));
pLabel1->setPosition( ccp(0, size1.height) );
this->addChild(pLabel1,1); //右上角显示性别
CCLabelTTF* pLabel2 = CCLabelTTF::create("男", "Thonburi", 34);
CCSize size2 = CCDirector::sharedDirector()->getWinSize();
pLabel2->setAnchorPoint(ccp(1, 1));
pLabel2->setPosition( ccp(size2.width, size2.height) );
this->addChild(pLabel2,1); //右下角显示年龄
CCLabelTTF* pLabel3 = CCLabelTTF::create("23", "Thonburi", 34);
CCSize size3 = CCDirector::sharedDirector()->getWinSize();
pLabel3->setAnchorPoint(ccp(1, 0));
pLabel3->setPosition( ccp(size3.width, 0) );
this->addChild(pLabel3,1);
return true; } void HelloWorld::menuCloseCallback(CCObject* pSender)
{
//结束关闭事件
// CCDirector::sharedDirector()->end();
//
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// exit(0);
//#endif
CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *pLabel = CCLabelTTF::create("我是添加的", "Thonburi", 24);
pLabel->setPosition(ccp(size.width/2+30,size.height/2+30));
pLabel->setTag(10);
this->addChild(pLabel,1);
} void HelloWorld::menuRemoveCallback(CCObject *pSender)
{
CCNode *pLabel = this->getChildByTag(10);
this->removeChild(pLabel);
} //多点触摸方法
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
//添加子视图
//随机数是CCRANDOM_0_1,是产生0-1之间的随机数
// CCSize size = CCDirector::sharedDirector()->getWinSize();
// CCLabelTTF *pLabel = CCLabelTTF::create("触屏添加", "Thonburi", 24);
// pLabel->setPosition(ccp(100, 100));
// pLabel->setTag(10);
// this->addChild(pLabel,1);
CCLog("多点触摸Began");
} bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCLog("单点触摸");
return true;//如果这个不返回true的话,则move方法没用
} void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCLog("单点moved");
} //触摸注册事件
//如果没有这个,默认的是多点触摸,Targeted是单点,Standed是多点触摸
void HelloWorld::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

最新文章

  1. logstash读取redis数据
  2. wireshark常用过滤规则
  3. CentOS6编译装载nbd模块
  4. Macbook之设置Finder显示文件完整路径
  5. 尝试Hexo
  6. OSPF路由汇总和默认路由设置
  7. 关于H-Fox 函数
  8. C#代码设置窗体和Panel的位置大小
  9. Tsinghua dsa mooc pa1
  10. perl 当前包会覆盖父类的同名方法
  11. 《DSP using MATLAB》示例 Example 6.5
  12. Memcached缓存
  13. 输入url会发什什么
  14. Centos7 通配符HTTPS证书申请 实测 笔记
  15. Java 位运算符和 int 类型的实现
  16. 虚拟现实外包—动点飞扬软件专门承接VR/AR场景、游戏、项目外包
  17. day02 格式化字符串
  18. Java中数组、List、Set互相转换
  19. js之获取元素最终css属性
  20. OAuth2.0的refresh token

热门文章

  1. Android开发之TextView排版问题
  2. float 覆盖元素的问题
  3. C#自学笔记总结
  4. BZOJ 2016: [Usaco2010]Chocolate Eating( 二分答案 )
  5. OpenSSL命令---rand
  6. 432B - Football Kit
  7. iOS使用自定义字体
  8. Chrome设计文档-多进程资源加载
  9. 在VC6.0中能不能使用Duilib界面库呢?
  10. Android Support Library更新到v22.1之AppCompat新特性