cocos2d-x常用 及新版本的一些变化

本文档详细介绍了 Cocos2d-x 游戏开发框架的使用技巧,包括场景创建、字符串操作、数组管理、动作控制、触摸事件处理等核心功能。此外还涉及了内存检测、反锯齿效果实现、加速度感应器应用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cocos2d-x常用

 
1.定义一个Scene

#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"

USING_NS_CC;

class GameScene : public CCLayer
{
public:
  // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  virtual bool init();
 
  // there's no 'id' in cpp, so we recommand to return the exactly class pointer
  static cocos2d::CCScene* scene();
 
  // implement the "static node()" method manually
  LAYER_NODE_FUNC(GameScene);
 
private:
 
};

#endif // __GAME_SCENE_H__

#include "GameScene.h"

USING_NS_CC;

CCScene* GameScene::scene()
{
  // 'scene' is an autorelease object
  CCScene *scene = CCScene::node();
 
  // 'layer' is an autorelease object
  GameScene *layer = GameScene::node();
 
  // add layer as a child to scene
  scene->addChild(layer);
 
  // return the scene
  return scene;
}

// on "init" you need to initialize your instance
bool GameScene::init()
{
  //////////////////////////////
  // 1. super init first
  if ( !CCLayer::init() )
  {
    return false;
  }
 
  CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
  return true;
}


2. 字符串的使用(NSString)
(1)
const char * getString()
{
  CCString *pRet = new CCString();
  pRet->autorelease();

  pRet->m_sString = XXXXX;
 
  return pRet->m_sString.c_str();
}


(2)
std::string mapName;
CCLOG("[%s]",mapName.c_str());

.h中声明
std::string * m_pFontName;
构造
, m_pFontName(NULL)
希构
CC_SAFE_DELETE(m_pFontName);
赋值
CC_SAFE_DELETE(m_pFontName);
m_pFontName = new std::string(fontName);




3. 格式刷(stringWithFormat)
char pTempMap[32] = {0};
sprintf(pTempMap,"%s.tmx",mapName.c_str());


4. CCMutableArray
CCMutableArray<CCSpriteFrame*> *walkRightFrames = new CCMutableArray<CCSpriteFrame*>();
//注:2.11后没有CCMutableArray 了    具体见文章最后

5.CCARRAY_FOREACH使用
(1)
CCArray::arrayWithCapacity(10);

CCObject * pObject;
CCARRAY_FOREACH(_RemoteplayerArray, pObject)
{
  PtCharacter * pCharacter = (PtCharacter*)pObject;
  if(pCharacter && pCharacter->_spriteSheet->getIsVisible())
  {
    //NViewDistance * pViewDistance = (NViewDistance*)[_ViewDistanceArray objectForKey:pCharacter.ItemID];
    //pViewDistance->getPosition() = pCharacter->_characterSprite->getPosition();
  }
}

(2)
CCNode * child;
CCArray * pArray = menu->getChildren();
CCObject * pObject = NULL;
CCARRAY_FOREACH(pArray, pObject)
{
  if(pObject == NULL)
    break;

  child = (CCNode*)pObject;
}


6. win32内存检测工具Visual Leak Detector , CRT

7. 相反的动作 actionBy->reverse();

8. 相同的动作第二次使用,actionTo0->copy()->autorelease()

9. 动作Sequence : CCSequence::actions(actionBy, actionByBack, NULL)

10. 重复动作: CCRepeatForever::actionWithAction(actionUp)

11. Iterator使用
CCMutableArray<CCObject *>::CCMutableArrayIterator it;
for(it = m_paddles->begin(); it != m_paddles->end(); it++)
  {
    paddle = (Paddle*)(*it);

    if(!paddle)
      break;
  }


12. CCLayerMultiplex与switchTo使用
CCLayer* pLayer1 = new MenuLayer1();
CCLayer* pLayer2 = new MenuLayer2();
CCLayer* pLayer3 = new MenuLayer3();
CCLayer* pLayer4 = new MenuLayer4();

CCLayerMultiplex* layer = CCLayerMultiplex::layerWithLayers(pLayer1, pLayer2, pLayer3, pLayer4, NULL);
addChild(layer, 0);

((CCLayerMultiplex*)m_pParent)->switchTo(0);


13. 添加触摸
setIsTouchEnabled( true );

void Parallax2::registerWithTouchDispatcher()
{
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
}

bool Parallax2::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
  return true;
}

void Parallax2::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
}

void Parallax2::ccTouchCancelled(CCTouch* touch, CCEvent* event)
{
}

void Parallax2::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
  CCPoint touchLocation = touch->locationInView( touch->view() );  
  CCPoint prevLocation = touch->previousLocationInView( touch->view() );  

  touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
  prevLocation = CCDirector::sharedDirector()->convertToGL( prevLocation );

  CCPoint diff = ccpSub(touchLocation,prevLocation);
 
  CCNode* node = getChildByTag(kTagNode);
  CCPoint currentPos = node->getPosition();
  node->setPosition( ccpAdd(currentPos, diff) );
}

14. 反锯齿
child->getTexture()->setAntiAliasTexParameters();

15. Accelerometer
setIsAccelerometerEnabled(true);

void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)
{

}
 
cocos2d-x  2.1.1的一些变化整理
一。最为明显的变化是CCMutableArray没有了,只剩CCArray。
并且你会发现有ccArray和ccCArray这两个结构体。
这两个结构体的作用是
There are 2 kind of functions:
- ccArray functions that manipulates objective-c objects (retain and release are performed)
- ccCArray functions that manipulates values like if they were standard C structures (no retain/release is performed)

而CCArray完全取代了之前CCMutableArray的作用,并且遍历使用CCARRAY_FOREACH。
同时CCDictionary取代了CCMutableDictionary,并且遍历使用CCDICT_FOREACH,得到的遍历对象是CCDictElement,这和map的遍历得到iterator有点像,CCDictElement可以getkey或者getObject。CCDictionary的Key被限制为int或者string,而存储对象也被限制为CCObject。
个人感觉这样有点不high啊,取出东西需要先强制转换类型,并且key做了限制。

二。文件路径的搜索方式变了。
查看CCFileUtils.h文件
2.1最大的变化是查找文件路径的方式,
参考fullPathForFilename函数,查找一个文件“gamescene/uilayer/sprite.png”时,将顺序查找
Ai/gamescene/uilayer/Bj/sprite.png这样形式的一个路径,直到找到文件为止,其中Ai是基本路径,可以是包路径,或者文档路径,“mnt/sdcard”等等,而Bj也是一个可变路径,通常是分辨率。
这样可以很方便的适配不同的分辨率。
2.2辅助查找文件用的配置
loadFilenameLookupDictionaryFromFile函数,可以打开一个plist配置文件,其中包含一个映射表,将文件名A映射为A++,在调用getNewFilename函数时,输入A,将返回A++,可以说是当文件升级后,可以方便找到新的文件,也可以帮助一个很长的文件路径来配置一个shortname。

以上两点和之前我在1.0版本基础上所写的resourceManager很像,可以帮助管理不同的分辨率下的资源,还可以很方便的升级资源。英雄所见略同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值