转发,请保持地址:http://blog.csdn.net/stalendp/article/details/8589324

最近在开发一款游戏,做demo的时候,使用了Cocos2d和WiEngine游戏引擎。我先做了Cocos2d端的demo,然后移植到android端。在移植的过程中,我使用了WiEngine,由于WiEngine提供了和Cocos2d很相似的API,所以能够很快地从cocos2d迁移。

下面的demo的截图(申明:demo使用的图片资源来源于网络,只是用于学习,如果有版权问题,请联系我:stalendp@gmail.com)

本例子中只是简单地用到了Sprite和SpriteBatchNode等概念,使用TexturePacker进行了图片的处理。WiEngine中读取TexturePacker信息的类为wyZwoptexManager, 代码如下:

wyZwoptexManager* zm = wyZwoptexManager::getInstance();
wyTexture2D* tex = wyTexture2D::makePNG(RES("R.drawable.renzhet"));
zm->addZwoptex("renzhet", RES("R.raw.renzhet"), tex);

这样整个plist文件通过XML解析,被加载到内存(frame对象),就可以生成sprite了(关于引擎的原理性方面,以后有机会再写文章深入吧)。

如果用Cocos2d,类似的代码为:

CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"renzhet.plist"];

使用方法如下:

wyZwoptexManager* zm = wyZwoptexManager::getInstance();
wySprite* sprite = zm->makeSprite("boy1.png");

相应的Cocos2d代码:

 if(self=[super initWithSpriteFrameName:@"boy1.png"]) //别的写法可能更合适

其他请参考完整代码吧。

下面是完整的C++代码:

#ifndef SAMPLE_H_
#define SAMPLE_H_#include "com_wiyun_engine_skeleton_Skeleton.h"
#include "FirstScene.h"
#include <cstdlib>#include <android/log.h>#define  LOG_TAG    "SkeletonProject"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)void runSample();class CloudCache;
class ttCloud;
class Background;
class BoyAndDog;void log(const char* msg);float genRand() {return rand() % 10000 / 10000.0f;
}wyLabel* m_hint;class Cloud: public wyLayer {
private:wySprite* cloud;float speed;public:Cloud() {buildCloud();}virtual ~Cloud() {}void buildCloud() {wyZwoptexManager* zm = wyZwoptexManager::getInstance();char buf[50];sprintf(buf, "cloud_%d.png", 1);cloud = zm->makeSprite(buf);cloud->setScale(0.5f);addChildLocked(cloud);this->setVisible(false);wyTimer* timer = wyTimer::make(wyTargetSelector::make(this, SEL(Cloud::onUpdateSprite)));scheduleLocked(timer);}void spawn() {LOGI("Cloud#spawn");this->setVisible(true);speed = genRand() * 0.3f + 0.2f;cloud->setPosition(wyDevice::winWidth + getContentSize().width / 2,genRand() * wyDevice::winHeight / 3 + wyDevice::winHeight / 2);char msg[50];sprintf(msg, "posy: %f", cloud->getPositionY());LOGI(msg);}void onUpdateSprite(wyTargetSelector* ts) {if (cloud->isVisible()) {cloud->setPosition(cloud->getPositionX() - speed,cloud->getPositionY());if (cloud->getPositionX() < 0) {this->setVisible(false);cloud->stopAllActions(true);}}}
};
class CloudCache: public wyLayer {
private:Cloud* clouds[10];
public:CloudCache() {for (int i = 0; i < 10; i++) {clouds[i] = new Cloud();clouds[i]->setVisible(false);addChildLocked(clouds[i]);}clouds[0]->spawn();wyTimer* timer = wyTimer::make(wyTargetSelector::make(this, SEL(CloudCache::onUpdateSprite)));scheduleLocked(timer);}virtual ~CloudCache() {}void spawn() {for (int i = 0; i < 10; i++) {Cloud* cloud = clouds[i];if (!cloud->isVisible()) {cloud->spawn();break;}}}void onUpdateSprite(wyTargetSelector* ts) {float rr = genRand();if (rr < 0.001) {spawn();}}
};class Background: public wyLayer {
private:wySprite* bg[4];
public:Background() {buildbg1();buildbg2();}virtual ~Background() {}void buildbg1() {wySprite* bg = wySprite::make(wyTexture2D::makeJPG(RES("R.drawable.bg1")));bg->setPosition(wyDevice::winWidth / 2, wyDevice::winHeight / 2 + 50);bg->setScale(0.5f);addChildLocked(bg);}void buildbg2() {wyZwoptexManager* zm = wyZwoptexManager::getInstance();for (int i = 0; i < 4; i++) {int id = i / 2;char sn[20];sprintf(sn, "bg%d.png", id + 2);bg[i] = zm->makeSprite(sn);bg[i]->setPosition(bg[i]->getContentSize().width * (i%2),bg[i]->getContentSize().height / 2);addChildLocked(bg[i]);}// start to updatewyTimer* timer = wyTimer::make(wyTargetSelector::make(this, SEL(Background::onUpdateSprite)));scheduleLocked(timer);}void onUpdateSprite(wyTargetSelector* ts) {for (int i = 0; i < 4; i++) {if (bg[i]->getPositionX() < -bg[i]->getContentSize().width) {bg[i]->setPosition(bg[i]->getContentSize().width-0.3,bg[i]->getPositionY());}int id = i / 2;bg[i]->setPosition(bg[i]->getPositionX() - 0.1f - 0.7f * id,bg[i]->getPositionY());}}
};class BoyAndDog: public wyLayer {
public:BoyAndDog() {wySprite* boy = buildBoy();wySprite* dog = buildDog();boy->setPosition(wyDevice::winWidth / 2,boy->getContentSize().height / 2);dog->setPosition(wyDevice::winWidth / 2 - 80,dog->getContentSize().height / 2 - 10);addChildLocked(boy);addChildLocked(dog);}virtual ~BoyAndDog() {}wySprite* buildBoy() {wyZwoptexManager* zm = wyZwoptexManager::getInstance();// add spritewySprite* sprite = zm->makeSprite("boy1.png");// create animation and add it to atlas spritechar buf[128];wyAnimation* anim = wyAnimation::make(0);for (int i = 1; i <= 4; i++) {sprintf(buf, "boy%d.png", i);wySpriteFrame* f = zm->getSpriteFrame(buf);f->setDuration(0.15f);anim->addFrame(f);}wyAnimate* a = wyAnimate::make(anim);wyRepeatForever* rp = wyRepeatForever::make(a);sprite->runAction(rp);return sprite;}wySprite* buildDog() {wyZwoptexManager* zm = wyZwoptexManager::getInstance();// add spritewySprite* sprite = zm->makeSprite("0.png");sprite->setFlipX(true);// create animation and add it to atlas spritechar buf[128];wyAnimation* anim = wyAnimation::make(0);int si = 3 * 4;for (int i = si; i < 4 + si; i++) {sprintf(buf, "%d.png", i);wySpriteFrame* f = zm->getSpriteFrame(buf);f->setDuration(0.1f);anim->addFrame(f);}wyAnimate* a = wyAnimate::make(anim);wyRepeatForever* rp = wyRepeatForever::make(a);sprite->runAction(rp);return sprite;}
};void log(const char* msg) {if (m_hint != NULL) {m_hint->setText(msg);}
}//
void runSample() {// init the texturewyZwoptexManager* zm = wyZwoptexManager::getInstance();wyTexture2D* tex = wyTexture2D::makePNG(RES("R.drawable.renzhet"));zm->addZwoptex("renzhet", RES("R.raw.renzhet"), tex);tex = wyTexture2D::makePNG(RES("R.drawable.dog"));zm->addZwoptex("dog", RES("R.raw.dog"), tex);// setup the texturewyDirector* director = wyDirector::getInstance();director->setShowFPS(true);// create scenewyScene* scene = new wyScene();m_hint = wyLabel::make("display some information here", SP(20));m_hint->setPosition(wyDevice::winWidth / 2,wyDevice::winHeight - m_hint->getContentSize().height / 2);scene->addChildLocked(new Background());scene->addChildLocked(new BoyAndDog());scene->addChildLocked(new CloudCache());scene->addChildLocked(m_hint);// run with itdirector->runWithScene(scene);// release, the scene and action will be hold by others// so this won't destory themwyObjectRelease(scene);
}

说明:本例子中没有对内存进行考虑,所以有内存溢出的bug,细心的读者如果能指出来,感激不尽

====

最后,记录一个android截图的技巧,防止忘记:

使用Android SDK文件夹下tool文件夹下的ddms,然后点击菜单Device=>Screen Capture..或者直接按组合键,ctrl+S,会再弹出一个对话框,也就是截图的对话框,这个时候,你可以刷新,旋转,保存或者复制手机的截图了

基于WiEngine游戏引擎的Sample相关推荐

  1. 基于WiEngine游戏引擎--背景移动

    转发,请保持地址:http://blog.csdn.net/stalendp/article/details/8602281 手机游戏中动作类的游戏还是很受欢迎的,这些游戏中,大部分有着移动的背景.这 ...

  2. cocos creator 方法数组_基于 Cocos 游戏引擎的音视频研发探索

    本文转载自公众号:流利说技术团队(lls_tech) 版权归原作者所有 本文主要介绍了流利说团队基于 Cocos 游戏引擎进行音视频相关需求开发过程中所遇到的问题和解决方案.文章中将依次阐述 Coco ...

  3. unity游戏开发毕设_《毕业设计(论文)-基于Unity游戏引擎的游戏设计》.doc

    学 号070125分类号本科生毕业论文(设计) 题目: 基于Unity游戏引擎的游戏设计 院(系) 电子与信息工程系专业 计算机科学与技术班级2007级学 生 姓 名指导教师(职称)提 交 时 间 二 ...

  4. 基于Laya游戏引擎实现微信小游戏排行榜

    我们都知道,微信小游戏和小程序目前风头十足,很多公司都逐渐增加了相关业务线来迅速推广自己的产品和抢占用户群.说到微信小游戏,就不得不提到排行榜这个功能,就目前游戏行业,似乎都离不开排行榜这个重要功能, ...

  5. 基于Bevy游戏引擎和FPGA的双人游戏

    资源下载地址:https://download.csdn.net/download/sheziqiong/85931377 资源下载地址:https://download.csdn.net/downl ...

  6. html5 3d游戏引擎演示,HTML5、JavaScript 3D游戏引擎和框架

    由于很多人都在用JavaScript.HTML5和WebGL技术创建基于浏览器的3D游戏,所有JavaScript 3D游戏引擎是一个人们主题.基于浏览器的游戏最棒的地方是平台独立,它们能在iOS.A ...

  7. HTML5/Javascript 2D游戏引擎列表

    2D  Javascript游戏引擎列表 Akihabara Lincense: GPL2/MIT Akihabara 是用于创建8/16位图游戏的js库和工具集合.它通过使用HTML5 Canvas ...

  8. 迪士尼收购HTML5游戏引擎公司Rocket Pack

    http://rocketpack.fi/engine/ 北京时间3月3日消息,据国外媒体报道,迪士尼公司闪电收购了一家名为Rocket Pack的基于HTML5游戏引擎公司.业界估计迪士尼准备自立门 ...

  9. HTML5游戏引擎中音频的播放策略

    [本文属原创,如有转载,请注明出处http://blog.csdn.net/yl02520/article/] 随着HTML5 API的不断丰富和浏览器对HTML5支持的不断完善,基于HTML5开发的 ...

  10. html5 3d游戏引擎演示,Top 10:HTML5、JavaScript 3D游戏引擎和框架

    由于很多人都在用JavaScript.HTML5和WebGL技术创建基于浏览器的3D游戏,所有JavaScript 3D游戏引擎是一个人们主题.基于浏览器的游戏最棒的地方是平台独立,它们能在iOS.A ...

最新文章

  1. 操作系统学习笔记 第四章:存储器管理(王道考研)
  2. Jenkins安装maven integration plugin失败解决方法
  3. python代码壁纸-爬虫 抓取王者荣耀所有英雄皮肤高清壁纸+超强注释
  4. 第四季度数据中心网络设备销量达35亿美元
  5. _vsnprintf 用法
  6. 网络知识:整理各种路由器组网方法!网跨段也能访问!
  7. MYSQL----(2) 数据库基础知识
  8. 华为鸿蒙新机价格表,华为Mate30已确认:鸿蒙系统+巴龙5000,售价感人
  9. Markdown(五)——绘图工具mermaid之流程图Flowchart
  10. implicit关键字和explicit关键字
  11. 【9933】单词的划分
  12. 用户故事与敏捷方法—发布计划
  13. android studio日记本源代码,效能日记本——随时回味过往点滴
  14. 兽药促销发展分析及新策略谈
  15. 计算机专业考研难么,计算机专业考研难吗
  16. ESP8266 下 OTA 更新 Firmware 和FileSystem
  17. jqGrid可编辑模式下 单元格内容不能复制处理
  18. V831——条形码识别
  19. composer安装依赖包
  20. linux英伟达显卡内核不匹配,解决ubuntu16.04循环登录问题,原因为linux图形化界面和英伟达显卡不兼容...

热门文章

  1. 一文读懂元宇宙,AI、灵境计算...核心技术到人文生态
  2. GitHub Top 100的Android开源库
  3. Vbs脚本编程简明教程之五
  4. iOS 11.2 - 11.3.1 越狱教程
  5. 【数学建模】预测模型之BP网络预测
  6. xshell安装激活教程
  7. winrar的破解激活
  8. WebRTC基本概念
  9. 计算机专业实习报告-5000字+,以及计算机专业实习周记-15篇
  10. 对可道云KodExplorer去掉版权简单破解方法