Wednesday, November 27, 2013

How to handle multiple resolutions / multiple screen sizes with the Starling Framework?

One of the great reasons that i choose Adobe AIR and Starling Framework as my favorite tool to create Android Games is that, it can be easily customized to support multiple resolutions for multiple screens. Honestly,  if you are going to design and re-size every single image of your games for all the Android devices, it is going to make your life much more miserable...

So, actually the complete guide to support multiple resolutions is stated clearly here, which includes the 3 strategies:
a) Stretching the stage
b) Letterbox
c) Smart Object Placement

In this post, particularly, I will demo a quick and an easy approach - Stretching the stage. Just in case you are still getting stuck with the guides, here's a simple working solution for your reference (which i actually use it in all of my games :))

public class myGame extends Sprite
    private var mStarling:Starling;
    public function myGame()
        // support autoOrients
        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;
        var screenWidth:int  = stage.fullScreenWidth;
        var screenHeight:int = stage.fullScreenHeight;
        var viewPort:Rectangle = new Rectangle(0, 0, screenWidth, screenHeight)
        mStarling = new Starling(Game, stage, viewPort);
        mStarling.stage.stageWidth  = 640;
        mStarling.stage.stageHeight = 960;
        // set anti-aliasing (higher the better quality but slower performance)
        mStarling.antiAliasing = 1;
        // start it!


What is actually done is that, in the codes, i have defined my default device to be a screen with the width = 640px and the height = 960px (ratio w:h = 2:3). With this, all my game elements (buttons, background images, etc) would be designed based on this resolution. Then, if the game is loaded into a device with a different screen size (and pixel density), the Starling would help me re-size accordingly. It could be a compromise, but it will effectively save you a lot of time.

Isn't that easy and convenient compared to that in the Java Eclipse?

Thursday, November 21, 2013

Quoridor Board Game - Lessons and future improvement

Few months ago, I published the Quoridor Board Game, with the promise that, if the user rating exceeds the threshold of 30, I would further improve the game. Today, you guys really make it... (btw, is that a coincidence?). And I would spend more time and efforts into polishing this board game.

The truth is, before it reached 30, a number of enhancement has been made, from time to time, especially on the user interaction parts. Yet, I am aware that it still has room for improvement. Here, I would like to briefly summarize what it lacks of, and what should be possibly improved.

1. Computer AI (Artificial Intelligence) difficulty

Honestly, the current Quoridor computer is still too weak to be taken as a training. A number of players have complained for this. This is my first priority. And i see my competitor could make it as well. So, no excuse. Just a matter of time.

2. User's friendliness

In order to squeeze a 9 x 9 matrix board into mobile devices (plus walls and others), it is undoubtedly a challenging task. This is especially significant when the player is required to drag and place the walls. The existing version has been designed such that when the wall is moved around, the surrounding tiles are highlighted to make the movement clearer. Apart from that, some users request that it would be greater to add a 'confirm' button, to be able to 'revert' in case there is a typo made accidentally. Guys (and girls), i copy that!

3. Undo feature

This is another challenging feature which greatly tests the programming skills of the game creator. I am not sure if this is needed. But nothing to lose if it is getting integrated.

4. Challenge other players Online

Definitely, this is a very demanding feature. Playing with computer all the time would be too boring... However, i set no date for this feature to come true, yet. After all, without the sufficient amount of players, this feature could hardly impress anyone. Moreover, enabling the Online Challenge Mode would cost. (Maybe i should wait until the number of rating exceeds 100? :p)

Updated on 30th Nov 2013:
- A Hard Mode is added to the latest release.
- An Undo feature is added. This might help address the issue of 'wrong wall placement', while helping the players to have a better analysis of the computer AI's game play.
- You can choose which player to start first through the green Setting button.

Friday, November 15, 2013

How to add Admob ANE into the Flash Builder project?

If you are first time integrating Adobe Native Extension (ANE) into the Flash Builder project (here, i demo an Andoid project), I bet you would have encountered some issues during debugging.

In this post, particularly, I would like to talk about adding Admob into the Flash Builder project. Since the Admob doesn't officially provide the ANE, here I am using the third-party one in the demo.

 If you have come across the error message such as:

VerifyError: Error #1014: Class so.cuo.anes.admob::Admob could not be found.

OR the error pop up similar to:

Error: Requested extension so.cuo.ane.Admob could not be found.

 then this is the right place to look for.

To revise, there are 2 steps to properly add an Adobe ANE into the Flash Builder (here, it is Admob ANE):

Step 1:

Go to Project Tab -> Properties -> Actionscript Build Path -> Native Extensions -> Add ANE

Step 2: (Which is most of the time overlooked by the beginner)

Go to Project Tab -> Properties -> Actionscript Build Packaging -> Google Android -> Tick the Checkbox of the Package

If you miss the step 2, you will face the errors mentioned.

One more thing, unlike using the Admob jar library in the Java project, the one provided by the third-party doesn't support Admob Ads in the debugging mode. You could only 'see' the Ads coming out if you "Export Release Build".

Following the link provided by Mike Monti, I am also able to display the Admob Ads.
One thing to remind is that, remember to add the following in your xml tags:
<meta-data android:name="" android:value="4323000"/>
<activity android:name="" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
To produce the Banner ads, you can simply follow the code below:

var adMobManager:AdMobManager = AdMobManager.manager;
          adMobManager.bannersAdMobId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
          adMobManager.createBanner(AdMobSize.SMART_BANNER,    AdMobPosition.BOTTOM_CENTER,"BottomBanner",null,true);

Hope this helps.

Brave Heroes Game Tips

Just after few hours i have posted the Brave Heroes Game Walkthrough, I manage to have a breakthrough in this game - reach the Maximum level of the Hero, which is 45!

So, i believe my Game Tips of the Brave Heroes is convincing enough with my achievement :)
Particularly, I would like to solve some puzzles that some players might have during the game play. And to emphasize, i didn't pay any in this game. Feel free to drop any feedback~

What is Maximum Level of the Heroes in the Brave Heroes Game?

As you can see the picture above, the highest reachable Heroes' level is 45 (through gaining experience or buying experience with gold). However, you are able to somehow perform "Level Unlock" with 45 blue Gems as stated. Technically speaking, it is almost impossible for me to unlock the next level without paying. The cost of 45 blue Gems is 1,000,000 Golds (it would take me hours or days to obtain such a huge amount through unlocking stages). Moreover, one thing to point out is that, in total i have only unlocked 9 Heroes out of 24. I really hope that the maximum level of 45 is enough for me to go through all the stages left, otherwise i would just stop playing this game. :p

How much is the cost of the experience?

With simple calculation, you will find out that one experience value would cost 7 Golds, regardless of how much your Heroes' level is.

How to approach and kill Enemy Heroes easily?

The enemy Heroes are sneaky. They keep running away if there is no army surrounding. Once you have killed the melee army in front of them, they will escape cunningly at the back of the tower and turn back to strike again. To lure them approach you without escaping, make sure your Hero's position is in such a way that, the enemy archer army is able to shoot the arrow from the back of the enemy Hero (as in the picture). This is crucial, especially if you choose the Range Hero as the leader. Approach the enemy Hero when they come with an archer. Let the archer stand behind him so that he will foolishly think that there is some backup and won't run away. Of course, use portion to regenerate hit points whenever necessary. Also, make sure your Hero is strong enough (at least 5 levels more than the enemy hero's)

How to earn more Golds in the Brave Heroes game?

No doubt, it is to kill more enemy Heroes in a stage, while purposely losing it through time count down. After raising your Leader Hero to such an extent that the Hero is at least having 5 levels higher than the enemy Hero's, now focus to kill the Heroes in the stage. Remember, do not win the stage as the enemies (both army and Hero) will grow stronger. Patiently wait them to get spawned and kill them with the tactics above. Killing one Hero may reward you around 200-500 experience, with around 1000-2500 Golds, plus 1 possible treasure that may contain up to 2500 Golds. Once in a blue moon, the treasure may come with 1 blue Gem too (as in the picture, the crate is Gold in color)! Until now, I have broken my previous records and now obtain more than 30,000 Golds in a single stage, though 'losing' in the end. Repeat the process infinitely, you can even train more Heroes to achieve the maximum level of 45.

One thing to note is that, do not manually quit the game yourself, though the Golds are still owned in the end, your Hero will lose all the experience gained (as observed).

Apart from that, need not fear about one versus multiple Heroes in a stage. The enemy Heroes come one by one. Just make sure you kill them before they are gathering and come to you.


Hope that the Game Tips help :)

Tuesday, November 12, 2013

Brave Heroes Game Walkthrough

Recently I am addicted to an Android Game - Brave Heroes

It is a Tower Defense Game, with very cutie Heroes and the armies (and enemies :))

Many has negative comments on the Brave Heroes game. One of the facts is that, unlike other games, the stages that you have defeated would "increase the level of difficulty" as well. Most of the players find this annoying as they couldn't (or very tough to) train the Heroes strong enough to face the coming just-unlocked stages. And hence, it seems that game is focusing on the "monetization" instead of the game play. BUT, IT DOESN'T.

So, this post is a Brave Heroes walkthrough to convince all of the would-be players (or on-going players) that, this game is really an interesting and working properly game which can provide a lot of fun without any money purchase, as long as you are a bit more patient.

Upgrading Hero is Essential

 This makes sense.  At the beginning stages (<10), you only have a Melee Hero "Balzac". Use all the money to upgrade as high as possible. Higher level means higher attack, attack speed, hit point, etc. One thing to mention, you should Enchant at least once on the weapon (Legendary Great Sword for Balzac Hero) to increase the chance of critical attack. I personally think the shield enchant is optional.

Our Army is negligible

I admit that they are adorable (especially their movement). But I have to frankly say that, their existence can serve nothing more than a few seconds buffer for the enemy invasion. My advice is, do not rely on them or upgrade them. In the Shop section, the only thing to upgrade is the "Gold", such that the stage clear reward can be increased.

Melee Hero is the best Leader

In Brave Heroes, you can summon up to 6 Heroes in a stage. The Heroes are categorized as "Near"(melee), "Distance" (Range), "Magic" (Range). The "Near" Heroes are the one who have the highest hit points such that, they are the best candidates as the front end strikers (to sustain the heavy pain longer), while the "Distance" and the "Magic" Heroes are attacking from the back. Here, particularly, I would like to recommend the "Penguin" as your option. The Penguin has a critical stunning attack skill which helps him (a guy) take a lead in the attack most of the time. And seriously, the young penguins attack skill is so funny and useful, especially when targeted at the escaping enemy Heroes. Here, precisely, i only upgrade the Penguin after discovering that he himself is able to VS even 4 Heroes! (one by one of course). And to re-emphasize, i didn't pay for this!

Purposely Lose is a strategy

As mentioned, if you have passed the stages with 3 stars, the enemies' level will be raised as well (including the enemy Heroes). This would mean that, even though you continuously select the Stage 1 to play with (and win in the end), the enemy armies would get infinitely stronger! Honestly, I have 1 stage that i couldn't even beat even though after summoning all 5 Heroes (total up 6 altogether), while there is no Hero at the enemy site!

Hence, the strategy is - to lose in a stage intentionally, through time-out. Within the period, kill as many enemy Heroes as possible (and the armies. Wait patiently for them to get spawned). Even though if you lose, you would still own the money. This is by far the most effective strategy to raise the level of the Hero and to earn money!
Killing an enemy Hero could reward you with about 200-500 experience, and up to few thousands gold (with the treasure). In the end, you could be able to rake in more than 15000 Gold (in my extreme case, 30000 Gold in a stage (20th) even though i lose in a stage!

Then you might ask, is it that easy to kill an enemy Hero? To do this, you have to understand 1 thing - the enemy Hero tends to escape when there is no army around. Therefore, to ensure the Hero stand 'stupidly' in front of you, you have to make some effort (timing) to let the enemy archer standing behind the enemy Hero. With this, even though they are running out of hit points, they are not going to escape and let you kill them. And then, for the Range enemy Heroes, you got to approach them (especially when they activate their skills) to attack them (assume you use the melee Hero such as the Penguin i mentioned).


I haven't finished the game, with only around 25 stages cleared. The thing is, i have my melee Hero (the Penguin) level raised to about 42 easily, 10+ more than my up-coming enemy Heroes! And I just need the solo Penguin to defeat them! The game would go endless if i continue my game play experience as stated above.

So, any hassle of playing the Brave Heroes game again? Perhaps the only glitch is that, on and beyond the stage 10, the game starts getting slower (probably due to too much objects appearing). Hope that they can add a fast forward button to boost the speed whenever necessary.

See Here for Brave Heroes Tips

Sunday, November 10, 2013

Generate non-repeating random numbers for games in actionscript AS3

When creating games, the random number generation is one of the important elements to avoid a monotonous game play experience.

Generally, it is straightforward to create a random number in actionscript AS3 (Simply use the built-in Math.random() function). But the question is, how to generate a list of random numbers, without any repetition? I used to be a PHP programmer. With PHP, it has an elegant approach with just 2 lines:

$numbers = range(m, n); //manually generate an array storing a list of numbers ranging from m to n
shuffle($numbers); //yes, we have it shuffled already~

Unfortunately, we don't have a similar solution in AS3...
Instead, the solution is:

var arrWanted:Array = [m, m+1, m+2, ... , n]; //manually generate an array storing a list of wanted numbers
var arrRandom:Array = new Array();
while(arrWanted.length > 0)
   var intRandom:int = Math.random()*arrWanted.length;

The idea is that, from the wanted list of number, we randomly pick any of them, while reducing them at the same time. In the end, we have our arrRandom filled with a list of random numbers.

I realize that this problem has been solved by others. In this post, particularly, i would like to add a real example of using the non-repeating random numbers generation as well.

Generate random non-repeating cards in Big 2 (Big Two) Card Game

Big 2 (Big Two) is a popular Poker Card Game involving 4 players. At the beginning of each round, each player is provided 13 cards (which total up to 52). In reality, the cards are distributed one by one, clock-wise. However, when dealing with the cards distribution in coding, we could simply apply the random numbers generation stated above.

Let's rewind the codes (AS3 part). With a little bit modification, we would have our jobs done easily.

var arrWanted:Array = new Array();
for(var i:int=0;i<52;i++)

var arrRandom:Array = new Array();
while(arrWanted.length > 0)
   var intRandom:int = Math.random()*arrWanted.length;

var arrPlayer1:Array = new Array(); 
var arrPlayer2:Array = new Array();
var arrPlayer3:Array = new Array();
var arrPlayer4:Array = new Array();
  else if(i<26)
  else if(i<39)

At the end, we have our arrays ready. Each array stores a list of 13 numbers which represent the Poker Cards.

Friday, November 8, 2013

Match 3 Game Algorithm Part 5 - Miscellaneous (Special Combinations)

By following the algorithms stated here, I believe you almost get the ideas of how a Match 3 game would look like in terms of logic. A little naive (as i didn't use any built in library or framework, or referring to the popular flood fill algorithm). But since it is from scratch, it has the flexibility of any customization. Furthermore, based on those solid understanding, you would be able to add more variations in your game.

Performance-wise, as long as you have followed the statements strictly, we could ensure a decent working solution. (I will demo a working one in the future post)

In this post, particularly, i would like to recap a number of features on the Candy Crush Saga that make the Match 3 game much more interesting - Special Candy Combinations, as stated below:

a) Color Bomb - formed by a match involving 5 candies in a uni-direction

b) Striped Candy - formed by a match involving 4 candies in a uni-direction

c) Wrapped Candy - formed by a match involving 5 candies in a "T", "+" or "L" shape.

Technically speaking, to detect these special candy combinations, it isn't that hard since we could make some checking in the results of the matching detection algorithm.

For example, in the picture above, after the node of 35th is exchanged with the 43rd, we would obtain a match of 5 nodes (33rd, 34th, 35th, 36th and 37th) according to the 1st statement.

However, the problem does not just end here. Since we are going to 'generate' the special 'candy' (item) ourselves, you might further pose some questions:

Q1: How to determine at where does the special candy reside?
Q2: How to enable those special effects formed by the special candies matches?

To answer the Q1, you would need to take some efforts remembering the index of the nodes touched by the player. If it takes place through random falling, you may take the smallest index of the matches to be the index of the special candy.

In the case of Q2, on the other hand, it is likely more complicated that we have to look into the special combinations one by one.

Consider the nodes exchange of Color Bomb:
a) 1 Color Bomb with 1 normal node
    Effect: All the nodes which are identical to the normal node will explode.
b) 1 Color Bomb with 1 Color Bomb
    Effect: All the nodes in the matrix explode.
c) 1 Color Bomb with 1 Striped Candy
    Effect: All the nodes having the same color become stripped candies and explode.
d) 1 Color Bomb with 1 Wrapped Candy
    Effect: All the nodes having the same color explode. And then, all the nodes with a random color explode.

Apart from that, we have to consider the exchange of Striped candy and Wrapped candy:
a) 1 Striped Candy with 1 Striped Candy
   Effect: Both Striped Candies explode
b) 1 Striped Candy with 1 Wrapped Candy
   Effect: Both candies explode, along with 3x3 matrix crossing both horizontally and vertically
c) 1 Wrapped Candy with 1 Wrapped Candy
   Effect: Both candies explode, along with 5x5 matrix surrounding both candies.

No pseudo-code? I believe with the basic algorithms in the previous posts, you would be able to construct the logic here. In case you really want, please leave a message here and i would update this :)

In my Match 3 game, i don't care about the orientation of the striped candy. Instead, i randomly explode either a horizontal or a vertical line.

Next: A real Android Match 3 Game Demo

Tuesday, November 5, 2013

Match 3 Game algorithm Part 4 - How to enable the user interaction?

Until now, we manage to come out with a fundamental match 3 game with the matches detection and a deadlock detection. So far so good :)

But the thing is, something is still missing: user interaction. This is the crucial part that lets the players get hooked on the game. The players are required to make some moves to proceed with the games, either through forming more matches, or completing some special missions. (In the case of the candy crush, the missions are forming matches to clear the jelly, or achieving a threshold score with minimum moves, and etc)

Here, the basic user interaction tutorial would be: move the nodes/items, either horizontally or vertically.

At first sight, this is pretty straightforward. In fact, since the each node/item in my match 3 game is coupled with an integer index, it isn't hard to make a rule such that, if the first node touched is a neighbor of the second, both the nodes are exchangeable, and hence we could perform some matching detection algorithm.

For example, the picture shows a matrix of 8x8. At the nodes indexed at 10th, 17th, 18th, 19th and 26th, it is easily observed that, the node of 18th can be only exchanged with its neighbor which has the absolute index difference of 1 or 8. With this understanding, we could conveniently generalize the algorithm...

However, let's consider 1 more case:

Here, the node indexed 15th, despite being able to exchange with the node 7th, 14th, 23rd, it is unable to interact with the node 16th (because they are not directly connected). Therefore, this special case has to be addressed in our algorithm, which is stated in the next statement:

Statement 4:
- Two nodes are exchangeable, provided that their absolute index difference is 1 or 8, AND if one of them is in the column 0th or 7th, the other one is NOT in the column 7th or 0th.

Forgive my poor explanation in the last part. It simply means that, the node in the column 0th cannot be exchanged with the one in the column 7th.

Coming up next: Miscellaneous (Special Combinations)

Friday, November 1, 2013

Match 3 Game algorithm Part 3 - How to determine whether there is no more match?

From the previous post, we learnt that to detect if there is a match in the match 3 game matrix, we could scan the nodes 1 by 1, each with the vertical and the horizontal direction.

Then, it must be natural to think that, if to do the opposite: "How to determine whether there is no more match?", we could simply apply the same concept, couldn't we?

To some extent, it is true that we could perform the similar technique. However, we haven't really defined the problem yet. Precisely, the puzzle to be solved is: "How to check whether there is no match, EVEN THOUGH after the players have made all the possible moves?" In other word, a match 3 game deadlock, in which the game couldn't proceed due to no more possible match.

There we go, with the condition added, the problem is way more troublesome than expected, and that's the reason i separated this topic.

Before the start of the algorithm, it would be better to have a look at some scenarios:

The picture shows a matrix with an extreme case such that, if the node of 35th and 36th (horizontal exchange) is exchanged, it could lead to the most matches (Here, let's just consider a match of 3 nodes only):
a) node 19th - vertical black
b) node 20th - vertical red
c) node 27th - vertical black
d) node 28th - vertical red
e) node 33rd - horizontal black
f) node 35th - vertical black
g) node 36th - vertical red, and horizontal red

With this understanding, it enlightens us on another statement:

Statement 2:
- For a horizontal exchange node of (nth) and (n+1)th, a matching detection algorithm is necessarily to be executed on the nodes:
a)  (n-2*8)th vertical
b)  (n+1 - 2*8)th vertical
c)  (n-8)th vertical
d) (n+1 - 8)th vertical
e) (n-2)th horizontal
f) (n)th vertical
g) (n+1)th vertical, horizontal

Similarly, this applies to the vertical nodes exchange as well:

 The picture shows that, if the node 35th is exchanged with the node 43th, it will trigger the matches:
a) node 19th - vertical black
b) node 33rd - horizontal black
c) node 34th - horizontal black
d) node 35th - horizontal black
e) node 41st - horizontal red
f) node 42nd - horizontal red
g) node 43rd - horizontal red, vertical red

Hence, not surprisingly, we have our 3rd statement:

Statement 3:
- For a vertical exchange node of (nth) and (n+8)th, a matching detection algorithm is necessarily to be executed on the nodes:
a)  (n-2*8)th vertical
b)  (n-2)th horizontal
c)  (n-1)th horizontal
d) (n)th horizontal
e) (n-2 + 8)th horizontal
f) (n-1 + 8)th vertical
g) (n + 8)th vertical, horizontal

Cool. Now we can apply the algorithm based on the statements. Here's the pseudo-code:
- foreach node nth
 manually exchange the node with (n+1)th, perform matching detection algorithm, 
 manually exchange the node with (n+8)th, perform matching detection algorithm.

 The pseudo code is as brief as possible. The thing to point out is that, although each could be exchanged in 4 directions (up, down, left, right), we only consider "right", and "down" while scanning each node. Again, this systematic checking avoids redundancy effectively.

There are several situations in which you could deploy the algorithm:
a) At the initial stage - when the game starts, you could check if the matrix encounters a deadlock or not
b) After the player have made a move - to check if the interaction is effective or not
c) After the items regenerated and have fallen down - to check if the fallen items would form some matches or not.

Coming up next: How to enable the user interaction?