wwboy6手記

Especially in gaming, there are lots of object that perform several actions, and it may have a chance that, in a newer version, there is a same kind of object that perform more actions.

For example, there are two similar UIs in the game, one will do the actions in sequence: actionA0, then actionA1, and another one will do: actionB0, actionA0, actionA1, then actionB1. For code reusability these two UI are implemented in two class, the first one is ClassA and another one is ClassB, and ClassB extends Class A to do the action A and B.

In this programming idea, there is a state machine function in ClassA to do the action w.r.t. the state. The state will be increased by 1 after doing an action. ClassB will override this function in order to change the action sequence.

//Consider ClassA with a state function
void ClassA::processState(int state) {
   switch(state) {
       case 0:
           //blablabla
           break;
       case 1:
           //blablabla
           break;
   }
}

//And a subclass ClassB want to extend that
void ClassB::processState(int state) {
   switch(state) {
       case 0:
           //state 0 of ClassB
       case 1:
       case 2:
           //ClassB [1,2] -> ClassA [0,1]
           ClassA::processState(state-1);
           break;
       case 3:
           //state 3 of Class B
           break;
   }
}

Of cause it’s going to define the sequence in the code, and a better practice may be reading the state information in file, such as P-List or XML, and then run the sequence w.r.t. the file, but it’s not always possible for more complex logic, and this state machine function and achieve the flexibility.

If you are working on Android NDK, and you want to know which part of your app use up the most memory, and to check if there is memory leak, so you need memory analyze. However, the memory analyze for NDK is different from SDK.

In order to do so please follow these two posts:

http://bricolsoftconsulting.com/2012/04/25/how-to-enable-native-heap-tracking-in-ddms/
http://stackoverflow.com/questions/10737722/native-heap-tracking-issue-native-heap-tab-is-empty

Note for using simulator to do memory analyze
– No need of CyanogenMod ROM, libc_malloc_debug_leak.so and libc_malloc_debug_qemu.so
– Use /data/local.prop instead of setprop to set libc.debug.malloc

Summary for using simulator to do memory analyze
1. Create an AVD and start it
2. Use “adb push” to put a file “local.prop” to “/data” with the the content “libc.debug.malloc = 1”
3. Restart the AVD
4. Add a line “native=true” in ddms.cfg file located at ~/.android/ddms.cfg, then open ddms, and choose “Navtive Heap” tag of DDMS
6. Run the testing app, choose that in ddms, and press snapshot to grab heap usage

Tips
– Make use of the first and third small icon button to organize the list

There are two post about graphics optimization which should be useful for who are working on Cocos2D game.

http://www.raywenderlich.com/2361/how-to-create-and-optimize-sprite-sheets-in-cocos2d-with-texture-packer-and-pixel-formats

http://allseeing-i.com/Performance-tips-for-Cocos2d-for-iPhone

Ref: http://www.mikeash.com/pyblog/using-filemerge-with-subversion.html

It’s so nice to use merging tool to resolve SVN conflict. Once you set it up, use ‘l’ option when conflict is detected in svn update, do the merging in File Merge tool, save the merge and quit File Merge, and then use ‘r’ option to resolve the file.

You don’t have to scare about conflict any more!

Barcamp HK 2012

一年一度ge Barcamp就咁就完左
http://www.barcamp.hk/
佢係一個一日時間好多個conference ge event
完全免費,但名額可能有限

一如以往唔多野睇XD
不過香港ge IT event冇理由唔支持

有幾個topic我覺得好好

1. Pitching
香港IT人成日比人話唔識講野,唔識溝通,唔識sell自已
其實一句講曬就係唔識pitching
pitch就係simple, 中point, 唔留手, 一兩句講完
只求一句,你以後都會記得

2. Angular JS
http://angularjs.org/
其實barcamp應該好多呢D tech sharing先0岩
都唔明點解只有幾多係咁
呢個係其中一個好好ge sharing
雖然講者準備唔多但係都show到D feature
其實就係一D descriptive language去做D visual野
TODO list, draw flow chat / graph 等等

3. Lytro
http://www.lytro.com/
其實係講者攞到部機出黎show off haha
不過真係好有趣
一個lens,影一次,可以calculate返唔同焦點ge相出黎
甚至可以做到個3d model(理論上可以)

有時間有興趣下年黎睇下啦~

Steam大減價~我買左幾隻game la~你呢?

小弟推介:

The Elder Scrolls V: Skyrim
類型:RPG
價格:原價USD59.99,減價後USD29.79

其實唔洗我推介都好多人識呢隻game啦~Meta Score: 94分!

SpaceChem
類型:Puzzle
價格:原價USD9.99,減價後USD4.99

小弟玩過最癲ge puzzle game之一,過程中好有科學家ge感覺,難度由淺入深

某幾關仲可以造大炮打怪獸XD
玩完每一關都有得同網上其他人compare下,睇下邊個快/同ge command少
仲有好多人share solution上youtube,睇下人地有幾癲!
警告:此遊戲puzzle難度極高非喜勿進

Splice
類型:Puzzle
價格:原價USD9.99,減價後USD4.99

介面靚爆燈!Sound track都好有誠意

睇起黎好簡單,越玩越有難度

小弟都係偏愛puzzle game不要見怪

Leap Motion

好簡單ge idea,好強ge引吸力
好想即刻用佢寫隻game

For a class with different state, instead of using switch-case loop all the time, there should be a state system to manage the behavior for each state.

For example a monster with different attacking state, there should be a class for each state, and a list of the object with those classes to represent states. In this case, each state is an enemy behavior containing a list of enemy behaviors (see also: Software Design: Feature based design on class), which is a flexible pattern to receive enemy event and share behavior between different state. (p.s.: In the other word, a normal enemy is an enemy with one state.)

Fire Dragon: attack
-> Fire Ball State (current Enemy State): receive attack event, board-cast event to its behaviors
-> Fire Ball Behavior (the only behavior in Fire Ball State): receive attack event, generate a fire ball

Sudo code to construct the Fire Dragon like this:

class EnemyBehaviorList : EnemyBehavior {
    //take enemy behaviors in constructor
}

class FireBallState : EnemyBehaviorList {
    //to do something specific
}

Enemy fireDragon = new Enemy() {
    name = "FireDragon",
    behaviours = new List () {
        new FireBallState() {            //Fire Ball State
            new FireBallBehaviour(),     //Fire Ball Behavior
            new BehaviorA()
        },
        new BehaviorB()                 //Another State, can be a behavior
    },
    currentBehaviourIndex = 0
}


and the code to construct an enemy that will only generate fire ball:

Enemy fireDragon = new Enemy() {
    name = "FireDragon",
    behaviours = new List () {
        new FireBallBehaviour()
    },
    currentBehaviourIndex = 0
}

This is about when to create a class: for a new feature instead of a new object concept
For example, an enemy for a combat game. Instead of making a class for an enemy, we should make a class for a feature/skill/behavior of enemy. Enemy should be a “data structure” instead of “controller”.

// Wrong practice:
class EnemyA : Enemy {
    ...
    public void throwFireBall() {...}  // it is hard to share this
                                       // with other enemy
}

// Correct practice:
class Enemy {
    string name; //name = "EnemyA"
    List<EnemyBehavior> behaviors;
}
class EBThrowFireBall : EnemyBehavior { //EB stands for EnemyBehavior
    public override void someEnemyMessage() {
        //throw fire ball
    }
}


Therefore different enemies can share same behavior with different settings and combination.

The key to make this decision is all about concept: enemy is a combination of behavior, and the behavior sharing between enemies are expected.

As I worked for mobile app almost 2 years, I conclude that Android is not a good platform for plugin development, comparing to iOS platform. I always feel painful to integrate any plugin for Android apps. Here are some reasons why.

Manifest.xml vs info.plist

Manifest file just does too much then a configuration file. Every activity/service and permission requirement in Android app have to register itself in it, which causes many plugins to modify this file. By the design pattern of Android SDK, many plugins cannot avoid to do so.

More than one plugins modifying the same file turns out the plugin users to do that manually, which increase the chance of careless mistake and the effort to manage them, or the users will do a template for several plugins, but it is still complicated if there are too many.

In my opinion, I don’t see any point for such registration on Manifest XML. To protect end-user? They won’t care about it. To protect app developer from third-party developer? No it can protect nothing. It would be perfect if Manifest XML does exactly what info.plist does. At least it should be application oriented, but not module oriented. There should be another automatic system to detect and conclude the combined activity/service and permission requirement.

Message in Activity

Such as onActivityResult, must be listened by an Activity, and there is only one active activity at the same time normally. It also causes different plugins trying to modify the only activity class.

Android View Parameter

All parameters of an Android View, such as position, width and height and etc, are stored in a ViewParam, which is an instance of one of the ViewParam classes, depending on the parent view of it. which causes a dependency of plugin component to external component.

Many plugins developer (mostly in iOS) implement a help method to create a view for users, so that the view could be added to the custom view made by the plugin users and it’s done. However, the view parameter cannot be pre-made as the plugin developer don’t know what is the parent view they are using. The plugin users have to hard code the parameters themselves, and have to update that if the view is updated.

May be it’s not fair to compare an old designed Cocoa Framework with a really new Java framework, and Google is so busy about catching up the visual design against iOS. For now a plugin management for Android should be a good idea to be invented.

Design a site like this with WordPress.com
Get started