最近用cocos2dx 3.5仿着开发一款类似竖屏小游戏,发现如果一个精灵(图片大小42*125px)从上往下 匀速下降(测试时每次下降5px左右)的时候,竟然会有顿的感觉,不管是安卓手机或PC上都会,帧率在57-60之间波动,苹果手机上会好很多,本来以为我代码逻辑有问题,后来尝试只是加载一张图片而已,精灵下降时也会有卡顿的感觉,然后就这么个简单的处理都有问题吗?那如何开发精灵会快速跑动的游戏呢? 后面尝试cocos2dx 2.2.6版的, 在安卓手机上也是会卡顿测试手机有 魅族MX3 三星I9268 联想A808T ,但是PC上基本是不卡顿。
下面贴出主要代码,基本只是简单改下hellocpp 而已。
#include “HelloWorldScene.h”
#include “AppMacros.h”
USING_NS_CC;
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;
}
// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
wall = CCSprite::create(“btn-about-normal-vertical.png”);
wall->setAnchorPoint(ccp(0.0, 0.0));
wall->setPositionX(visibleSize.width / 2 - wall->getContentSize().width / 2);
wall->setPositionY(0);
this->addChild(wall);
this->scheduleUpdate();
return true;
}
void HelloWorld::update(float delta)
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
wall->setPositionY(wall->getPositionY() - 5);
if (wall->getPositionY() < -visibleSize.height)
{
wall->setPositionY(visibleSize.height);
}
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,“Alert”);
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}