基于WiEngine游戏引擎的Sample

来源:互联网 发布:linux下安装nginx 编辑:程序博客网 时间:2024/06/06 04:25

转发,请保持地址: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,会再弹出一个对话框,也就是截图的对话框,这个时候,你可以刷新,旋转,保存或者复制手机的截图了