読者です 読者をやめる 読者になる 読者になる

IPhoneのアプリをcocos2d-xに移植するにあたってのメモ(2)


自メモ)

関数置き換え系のメモ



ググって出てくる情報)

らへん。

2.0=>3.0系へのマイグレーション



とりあえず3.0系ベースで書き換えてみる(適宜追加する予定)

  • NSString
NSString *str;
NSString *start = [NSString stringWithFormat:@"%@",str];

=>

String str; // or const char* str
String *start = String::createWithFormat("%s",str);
 
//get string from CCString
const char* tmp = start->getCString(); //or
string tmp(start->getCString());
  • NSRange, NSMakeRange and Substrings
NSString *start;
NSString *end; 
NSString *haystack;
 
NSRange startRange = [haystack rangeOfString:start];
 
NSRange endRange = [haystack rangeOfString:end];
 
if(startRange.location != NSNotFound && endRange.location != NSNotFound)
    {
           NSRange datarange = NSMakeRange(startRange.location + startRange.length, endRange.location(startRange.location + startRange.length));
 
       NSString *sub = [haystack substringWithRange:datarange];
 
       return deckdata;
    }


=>

string str1;
string str2;
string haystack;
 
//no real need for CCString here
String *start = String::createWithFormat("%s",str1);
 
String *end = String::createWithFormat("%s",str2);
 
unsigned startRange = haystack.find(start->getCString());
 
unsigned endRange = haystack.find(end->getCString());
 
if(startRange != -1 && endRange != -1)
{
    return haystack.substr( startRange + start->length(), endRange - (startRange + start->length()));
}
  • NSMutableArray
NSMutableArray *menus = [NSMutableArray arrayWithCapacity:100];
 
CCMenu *men;
[menus addObject:men];
 
for(id element in menus) //iterate menus array
{
    CCMenu *m = (CCMenu*) element;
    for (CCMenuItem* item in [m children]) //iterate children of each menu
    {
            [item setIsEnabled:NO];
    }
}


=>

Vector<Object> *menus = new Vector<Object>(100);
Object *m_item;//menu
Object *mm_item;//menu item
 
Menu *item;
menus->addObject(item);
 
//CCARRAY_FOREACH(menus, m_item)
for (Node *m_item:menus)
{
    Menu *menu = dynamic_cast(m_item); // use dynamic cast sparingly, its expensive
    //CCARRAY_FOREACH(menu->getChildren(), mm_item)
    for (Node *mm_item:menu->getChildren())
    {
       MenuItem *child = dynamic_cast(mm_item);
       child->setEnabled(false);
    }
}

備考)
上記だとObject型だ使ってるけど、cocos2d::Value 型はプリミティブ(intとか)にしか使えないので今一感満載

  • NSDictionary
	NSString* fullPathToPList = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"World.plist"];
	NSDictionary* worldPlist = [NSDictionary dictionaryWithContentsOfFile:fullPathToPList];

	// Dictionaries within dictionaries…
	NSDictionary* worldDict;
	NSDictionary* levelsDict;
	NSDictionary* levelDict;

	// Now drill down to what we want.
	worldDict = [worldPlist valueForKey:@"World"];
	levelsDict = [worldDict valueForKey:@"Levels"];
	levelDict = [levelsDict valueForKey:[NSString stringWithFormat:@"Level%d", levelNumber]];
	// Set up the terrain data so we can draw it
	[self setUpTerrainFromArray:[NSMutableArray arrayWithArray:[dict objectForKey:@"Terrain"]]];

=>

std::string path = FileUtils::getInstance()->fullPathForFilename("World.plist");
	ValueMap worldDict = FileUtils::getInstance()->getValueMapFromFile(path);

	auto worldDictionary = worldDict.at("World").asValueMap();
	auto levelsDictionary = worldDictionary.at("Levels").asValueMap();
	auto levelDictionary = levelsDictionary.at("Level1").asValueMap();
	auto terrainFloorArray = levelDictionary.at("TerrainFloor").asValueVector();

	createTerrainFromArray(terrainFloorArray);
void WorldLayer::createTerrainFromArray(cocos2d::ValueVector &terrainFloorArray)
{

	std::string subterraneanImageName = "";
	// Iterate through the terrain points detailed in the terrainFloorArray
	for (const auto &value : terrainFloorArray)
	{
		auto dict = value.asValueMap();
		auto location = PointFromString(dict.at("Location").asString());

		// If there is no subterraneanImageName, use the last one
		if (dict.find("SubterraneanImageName") != dict.end())
		{
			subterraneanImageName = dict.at("SubterraneanImageName").asString();
		}
		auto p = new TerrainPoint(location, subterraneanImageName);
		_terrainPoints.pushBack(p);
	}
}

cocos2d::Map

  • CGPoint
NSMutableArray *arrAllPoints;
NSMutableArray *points = [NSMutableArray arrayWithCapacity:nCurrNumOfPoints];
 
for(int i=0;i < nCurrNumOfPoints;i++)
{
        CGPoint pt=arrAllPoints[i];
    
        [points addObject:[NSValue valueWithCGPoint:ccp(pt.x,pt.y)]];
}
        [self setPoints:points];

=>

PointArray *arrAllPoints;
PointArray *points = PointArray::create(nCurrNumOfPoints);
 
for(int i=0;i < nCurrNumOfPoints;i++)
{
        Point pt = arrAllPoints->getControlPointAtIndex(i);
 
        points->addControlPoint(pt);
}
    
    this->setPoints(points);
  • NSTimer
[NSTimer scheduledTimerWithTimeInterval:timer 
target:self 
selector:@selector(myTimerMethod:) 
userInfo:nil 
repeats:NO];

=>

Director* pDirector = Director::getInstance();
Scheduler *scheduler = pDirector->getScheduler();
 
scheduler->scheduleSelector(schedule_selector(MyCCObject::myTimerMethod),this,timer,1,0,false);