目标:想要精灵进行随机的移动。

过程:需要一个白色的背景,所以先来修改 HelloWorld 的继承,改成继承 CCLayerColor,然后修改一下 init 方法。如下:

CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) 

添加完图片资源之后就可以开始敲代码了,需要注释的地方都写在代码里了,,就直接贴代码:

HelloWorld.h 声明的方法:

void addTarget();
void spriteMoveFinished(CCNode *sender);
void gameLogic(cocos2d::CCTime dt);

HelloWorld.m文件重写 init( ) 方法:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"using namespace cocos2d;
using namespace CocosDenshion;CCScene* HelloWorld::scene()
{// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectHelloWorld *layer = HelloWorld::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;
}// on "init" you need to initialize your instance
bool HelloWorld::init()
{//// 1. super init firstif ( CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ){CCSize winSize = CCDirector::sharedDirector()->getWinSize();//获取屏幕大小float sprite_scale = 2.0;CCSprite *Player =  CCSprite::create("Player.png");Player->setScale(sprite_scale);Player->setPosition(ccp(Player->getContentSize().width*sprite_scale/2.0, winSize.height/2.0));this->addChild(Player);this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);return true;}else{return false;}}void HelloWorld::gameLogic(cocos2d::CCTime dt){this->addTarget();
}void HelloWorld::addTarget(){CCSize winSize = CCDirector::sharedDirector()->getWinSize();CCSprite *target = CCSprite::create("Target.png");//随机位置int minY = target->getContentSize().height/2.0;int maxY = winSize.height - target->getContentSize().height/2.0;int rangeY = maxY - minY;int actualY = rand()%rangeY + minY;target->setPosition(ccp(winSize.width - target->getContentSize().width/2.0, actualY));this->addChild(target);//随机速度float minDuration = 2.0;float maxDuration = 4.0;int rangeDuration = maxDuration - minDuration;float actualDuration = rand()%rangeDuration + minDuration;CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0 - target->getContentSize().width/2.0, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外//增加一个回调函数,回收移动到屏幕外的精灵CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));}void HelloWorld::spriteMoveFinished(cocos2d::CCNode *sender){CCSprite *sprite = (CCSprite *)sender;this->removeChild(sprite, true);
}void HelloWorld::menuCloseCallback(CCObject* pSender)
{CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);
#endif
}

运行结果如下:

cocos2dx 精灵的移动(2)相关推荐

  1. 【Cocos2d-x】Cocos2d-x精灵的性能优化

    Cocos2d-x精灵的性能优化-使用纹理图集和精灵帧缓存 使用纹理图集 纹理图集(Texture)也称为精灵表(Sprite Sheet) 使用纹理图集的优点: 1.减少文件读取次数,读取一张图片比 ...

  2. Cocos2d-x 精灵移动

     红哥里面使用的ccTouchesMoved,回忆下我之前的教程,使用的是ccTouchesBegan,大家可以体验一下这两种方式的不同. 当我们的手指触碰到屏幕的一瞬间,调用的是ccTouche ...

  3. cocos2dx 精灵点击事件

    1首先要实现纯虚类 class Plant :public PlantInterface,public CCTouchDelegate 2把当前的精灵放入一个代理 CCDirector::shared ...

  4. cocos2dx精灵裁剪显示ClippingNode

    ClippingNode(裁剪节点)可以用来对节点进行裁剪,可以根据一个模板切割图片的节点,生成任何形状的节点显示. 使用示例 auto bg = LayerColor::create(Color4B ...

  5. cocos2d-x精灵动画帧

    原文地址:http://blog.csdn.net/musicvs/article/details/8105611 1.    会动的精灵 第一篇教程教我们的是创建一个精灵,但是它不会动,现在终于可以 ...

  6. quick cocos2d-x 精灵大小(宽高)获取

    quick下sprite的大小获取,记录一下: local w = sprite:getContentSize().width local h = sprite:getContentSize().he ...

  7. cocos2d-x精灵的放大和缩小

    过程描述 声明菜单函数: virtual void menuScaleFish(CCObject *pSender); 定义菜单函数: void fish::menuScaleFish(CCObjec ...

  8. 【玩转cocos2d-x之三十五】Earth Warrior 3D大揭秘

    3D游戏现在玩起来门槛还是挺高的.不过如果在Cocos2d-x引擎加入3D扩展,实现2.5D游戏效果又会怎么样? 1.概述 先上大会现场演示图: Apk下载: http://pan.baidu.com ...

  9. Cocos2d-x视频教程

    目录 1. 我的技术专栏 2. 相关推荐 3. 下载链接 4. cocos2d-xx Lua+JS+C++教学视频 5. 杨丰盛Cocos2D-X游戏课程 6. [Cocos2d-x]塔防游戏开发实战 ...

最新文章

  1. Bochs调试Linux内核6 - 启动过程调试 - 跳到bootsect引导程序执行
  2. openMVS-编译
  3. XMPP扩展协议详解
  4. Java面向对象之抽象方法抽象类、接口的使用
  5. c语言printf、sprintf、vsprintf用法和区别
  6. 拍照比剪刀手泄露指纹信息;国内绿 iPhone11 抢断货;PostgreSQL 12 Beta 4 发布​ | 极客头条...
  7. 图像处理自学(六):图像增强算法总结
  8. 高频交易揭:美国五大高频易访录
  9. python文本文件合并_使用python将2个文本文件合并为一个文件 - python
  10. win7忘记密码不用工具进入
  11. 3个国内最大的黑客学习网站
  12. 软件研发成本构成中的间接成本包括哪些?
  13. tensorflow中FLAGS全局变量用法
  14. c++静态成员变量和静态成员方法
  15. 1153: 简易版最长序列 C语言
  16. 百度经纬度和google经纬度转换测试
  17. VBoxGuestAdditions下载地址
  18. Real-Time Rendering——Chapter 7 Advanced Shading
  19. 学计算机不会重装系统正常吗,电脑那点事 篇一:不会重装windows操作系统?6 分钟就能学会!...
  20. java 定时器(整点报时)

热门文章

  1. 免费主机 虚拟主机 香港虚拟主机
  2. border-image
  3. jquery easyui 全部图标
  4. 2008.12.7微明(杭州)信息科技有限公司
  5. linux下hwclock及clock命令详解
  6. Truffle Unbox failed问题解决方法
  7. 网络营销试卷(参考答案及评分标准)
  8. nbiot信号 服务器,物联网协议之NBIOT
  9. C3P0Util 的制作 及DButils 中的 QueryRunner 类的使用
  10. excel 制作好看的图表