Thursday, December 11, 2014

C++ Necessary to Call Base Destructor?

The answer is no, it is automatically called, but it is also somehow important that the destructor is virtual if the class will be inherited.

Reference: http://stackoverflow.com/questions/677620/do-i-need-to-explicitly-call-the-base-virtual-destructor

Cocos2D-X: Convert String to Integer and Testing For Numeric Value

These can be done quite simply by converting the String to a Cocos2D-X String object and then calling the "intValue()" function on the String. If it is a numeric value, the function will return the value which you can use, or if it is not a numeric value, it will return 0 so it also serves for a test for numeric value, unless of course the value is "0" and you should test for that first.

int val;
String* str = String::create(“123”);
// test for 0
if(str->compare(“0”) == 0){
   val = 0;
}
else {
   val = str->intValue();
   if(val == 0){
       // String is not numeric, handle the error here
   }
   else{
       // String is numeric and its value is saved in the “val” variable
   }
}

Wednesday, December 10, 2014

Cocos2D-X: EventListenerTouchOneByOne no viable overloaded '='

In Cocos2D-X version 3.0 and newer, there is a new way of handling touches (see example code below).

However the first time I used this, I encountered a compile error with message "EventListenerTouchOneByOne no viable overloaded '='" in lines 4-6 below. When this compile error happens, one reason is because of a wrong return type on their callbacks.
   EventListenerTouchOneByOne* touchListener = EventListenerTouchOneByOne::create();
   touchListener->setSwallowTouches(true);
   touchListener->onTouchBegan = CC_CALLBACK_2(MyLayer::touchBegan,this);
   touchListener->onTouchEnded = CC_CALLBACK_2(MyLayer::touchEnded,this);
   touchListener->onTouchCancelled = CC_CALLBACK_2(MyLayer::touchCancelled,this);
   touchListener->onTouchMoved = CC_CALLBACK_2(MyLayer::touchMoved,this);

   _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

The "onTouchBegan" callback has return type "bool". The rest need to have return type "void". Otherwise you will get the compile error above (I had all of them with return type "bool").
bool MyLayer::touchBegan(Touch *touch, Event *event) {
return true;
}
void MyLayer::touchEnded(Touch *touch, Event *event) {
}
void MyLayer::touchCancelled(Touch *touch, Event *event) {
}
void MyLayer::touchMoved(Touch *touch, Event *event) {
}

Monday, December 8, 2014

Cocos2D-X Dynamic Label Height

Setting height to 0 seems to do the trick here.

Cocos2D-X Display Long Text

When displaying long text in Cocos2D-X, it doesn't seem possible to use a single Label when the text is quite long (e.g. 2000+ characters), as the Label goes completely blank instead of displaying text. Maybe it is because Cocos2D-X uses a texture for this and this somehow blew the texture size limit. To display long text in Cocos2D-X, use multiple labels e.g. one for each paragraph.

Note: to get the height of each label so as to know where to position the next one, call "getBoundingBox()" on the label.

Thursday, December 4, 2014

Cocos2D-X: Substring

It seems like the best way is to convert to a std::string first and use its substr function, so e.g. from a Cocos2D-X String object:
cocos2dxString->_string.substr(pos,len);

Note: for Cocos2D-X 3.0 and above

Reference: http://www.cplusplus.com/reference/string/string/substr/

Git Push Specific Branch

E.g. to push master branch only:
git push origin master


Reference: http://stackoverflow.com/questions/1947972/how-can-i-push-a-specific-branch-of-git-to-my-server

Wednesday, December 3, 2014

Cocos2D-X: Conversion Between String, std::string, and C String (i.e. char*)

"String" refers to the String class (previously CCString and analogous to the NSString in Objective-C). This is for Cocos2D-X 3.0 and above.

From C String to String, it's actually the standard constructor
String* cocos2dxString = String::create(“xxxxx”);

From String to C String
cocos2dxString->getCString();

From String to std::string
cocos2dxString->_string;

From std::string to String (this requires an indirect conversion it seems)
String::create(cocos2dxString->_string.c_str());

From std::string to C String
stdString.c_str();

Tuesday, December 2, 2014

Cocos2D-X Formatting Number to Two Decimal Places

Format with "%.2f" assuming the number is a floating point type.

Reference: http://stackoverflow.com/questions/4784336/two-decimal-places-using-printf

Cocos2D-X Wrapping Text

To do that, call the "setDimensions" function on the Label. The wrapping will be done automatically. Just ensure that there is sufficient height for the additional lines.

Gimp MacOS "Embedded Color Profile"

In summary, if you don't know otherwise, convert it.

Reference: http://docs.gimp.org/en/gimp-imaging-color-management.html

Error Unzipping File on MacOS: "Error 2 - No such file or directory"

Not sure why this happens but this means the ZIP file is corrupted.

To fix it:

zip -FF zipfile.zip --out repairedcopy.zip

Reference: http://apple.stackexchange.com/questions/135853/error-opening-a-zip-file-no-such-file-or-directory