[cocos2d-x]HelloWorldDemo
实现一个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); }
[cocos2d-x]HelloWorldDemo相关推荐
- Improve Performance and Reduce Memory with PVRTC Textures and Cocos2d
转载自:http://www.uchidacoonga.com/2011/07/pvrtc-textures-and-cocos2d/ Hello everyone! A few months hav ...
- 【Cocos2d开发】Cocos2d下安卓环境的搭建
在进行Cocos2d游戏开发前 我们先来配置一下环境,我们先来准备一下工具,我们所需要的工具分别为: 1.Cocos2d引擎 2.JDK 3.SDK 4.NDK 5.ANT 6.ADT 1.下载Coc ...
- cocos2d 从v1.x升级到v2.x需要注意的几个地方
首先v1.x一些CCNode定位函数实现的有问题,导致返回的CCPoint的x坐标不正确(超出320后无变化),怀疑是其对屏幕旋转判断的不正确;而且这种现象在iOS 7.1之前的模拟器中运行都正常,在 ...
- cocos2d游戏jsc文件格式解密,SpideMonkey大冒险
点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 介绍cocos2d游戏中常用的jsc格式文件的解密." 01 - 在破解游戏应用中,经常会碰到后缀为jsc的文件,这是基于coco ...
- 如何优化cocos2d程序的内存使用和程序大小:第一部分
译者: 在我完成第一个游戏项目的时候,我深切地意识到"使用cocos2d来制作游戏的开发者们,他们大多会被cocos2d的内存问题所困扰".而我刚开始接触cocos2d的时候,社区 ...
- Cocos2d入门--3--向量的应用
小球向一个方向持续运动的Demo HelloWorldScene.h ...... //设置一个protected的属性 protected:cocos2d::Vec2 _vec; ...... He ...
- Cocos2d之Action类详解
一.声明 文章中使用到的cocos2d的源代码的版本是cocos2d-x-3.3rc0. 二.主要内容 [Action类简介] 在cocos2d中,Action类是所有动作的基类.Action类继承了 ...
- [cocos2d-x]cocos2d和cocos2d-x的一些通用性
不得不说要说总结,因为Cocos2d-X的代码和Cocos2d-iphone两个引擎除了语言不同外(Cocos2d-X使用C++,Cocos2d-iphone使用Object-C)可以说没有其他差异. ...
- Cocos2d中从场景切换到UIViewController视图方法总结
第一种:直接从场景切换到UIViewController视图(网上流传的版本) - (void) showUIViewController:(UIViewController *) controlle ...
- Cocos2d之Texture2D类详解之将文件加载成Texture2D对象
一.声明 笔者以cocos2d框架cocos2d-x-3.3rc0版本的源码做分析.本文为笔者原创,允许转载和分享,只要注明文章出处即可. 二.简介 Texture2D类简介 Texture2D类允许 ...
最新文章
- sqlprofiler 常用调试方法
- Helloworld【C#】
- 聚合登录平台网站源码 PHP聚合登录源码
- Vscode 如何使用内置浏览器?
- Linux误删C基本运行库libc.so.6处理方法
- mplayer+科大讯飞离线语音包
- 客户端DDK编译环境配置说明
- 输出UTC时间和北京时间
- 材料成型计算机基础,材料成型及控制工程 主干课程
- MATLAB学习笔记02-MATLAB的数据类型
- 符号-$,美元符号$在不同语言代码中的解释与应用
- 移动硬盘安装双系统windows10和ubuntu18.04
- linux 深信服ssl_mac访问不了深信服的ssl vpn
- js控制禁用退格键回到上一个页面
- PDF如何编辑?教你几个常用的PDF编辑方法
- 总结HTMLT5高级的新特性
- CPU架构之ARM优化
- JavaScript 事件和事件类型
- 面向对象基础案例(2)
- 申请并下载LIWC字典(英文版)用作科研
热门文章
- JQuery合并表格单元格
- 移动硬盘安装Windows7
- .NET的资源并不限于.resx文件,你可以采用任意存储形式[上篇] (转载)
- 如何修改Vs2008环境变量
- 通过继承来实现注解方式的属性注入
- 蛮力法在求解最优解问题中的应用(JAVA)--旅行家问题、背包问题、分配问题
- CentOS提示另一个目前运行的程序正在访问软件信息
- jquery 字符串去首尾空格_jquery $.trim()去除字符串空格详解
- ril.java_RIL.java里request流程
- 检测网络是否稳定的计算机命令,如何查看自己的网络是否稳定