23
Feb
21

Updating the Android version

Now when the majority of all changes are implemented in the coming iOS version, the work has begun implementing them in the Android version too.

This is unfortunately not a matter of cut and paste since the two projects are two completely different code bases. The apps just look the same. Behind the interface the iOS version is written in Objective-C using Cocos2d as the main graphics engine. The Android version is written in Java and uses AndEngine GLES-2 as the main graphics engine.

Sure, the ideas and code approaches are the same but I need to rewrite the code. Let’s take an easy example. We want to create a string with the text “The player Richard is excluded from the fight” where “Richard” is a variable and “The player … is excluded from the fight” is a string that is in different languages depending on where the player is from.

So in iOS:
NSString *strText = [NSString stringWithFormat:AMLocalizedString(@”str_playerexcluded”, nil), strPlayerName];

In Android:
String strText = String.format(this.getResources().getString(R.string.str_playerexcluded), strPlayerName);


So yes, they are similar but still I can’t cut and paste anything.

And regarding 2D-graphics. Let’s assume we have a bitmap (Say… a ball) that we want to rotate a full 360′ in 5 seconds.

In Cocos2d (for iOS):
id rot = [CCActionRotate actionWithDuration:5 angle:360];
[pBall runAction:rot];


In AndEngine (for Android)
RotationModifier rot = new RotationModifier(5, 0, 360);
pBall.registerEntityModifier(rot);

So it’s a lot of work and that’s why the Android version will take some time more to be finished.

Right now I’m working hard on the new Dialogs for the Android version. Up until now the dialogs (pop up windows) have looked rubbish on the Android version and that’s because I’ve used the built in Android views which are difficult to make beautiful since there is no pixel positioning. You always have to place an object “next to” or “below” or “above” something else. Not “At position 100,50”. So I have instead created my own dialog system mimicing the dialog system on iOS so that it will be easy for me in the future to create new dialogs.

While doing that I stumbled upon a problem: Word wrap of text.
This is neatly built in in the iOS system but not so well in AndEngine. Sure, there is a word wrap function that automatically wraps a string by word or letter if it doesn’t fit but unfortunately it doesn’t take newlines into account. If I have a string looking like this “I want to have a dog\nAnd a cat\nAnd a small bird that says beeep”, it would look like this on iOS:

I want to have a dog
And a cat
And a small bird that
says beeep


Unfortunately AndEngine gives me this:
I want to have a dog And
a cat And a small bird that
says beeep


So for the first time since I began using AndEngine I had to jump into it’s code and make some changes in the following files:
AutoWrap.java (at line 16)
NEWLINESANDWORDS,

FontUtils.java (at line 155)
case NEWLINESANDWORDS:
return FontUtils.splitLinesByNewlinesAndWords(pFont, pText, pResult, pAutoWrapWidth);

And then add this function in the same file:
private static > L splitLinesByNewlinesAndWords(final IFont pFont, final CharSequence pText, final L pResult, final float pAutoWrapWidth) {
ArrayList newArr = new ArrayList(1);
splitLines(pText, pResult);
for(CharSequence strLine : pResult) {
if (strLine.equals(“”)) newArr.add(” “);
else splitLinesByWords(pFont, strLine, newArr, pAutoWrapWidth);
}
return (L) newArr;
}


The code is a little ugly but it actually works so I will leave it as this and maybe return another day to make it better.


0 Responses to “Updating the Android version”



  1. Leave a Comment

Leave a comment


Categories