Thursday, August 19, 2010

Detecting orientation change of mobile devices

Couple of weeks ago I was asked by one of the biggest online games company to adapt several of my games for their new portal designed for mobile devices.
One of the most important thing in this venture was to disable orientation changing of the phone. Simplest way to check if it is in landscape or in portrait mode (in every device, because I know there are functions dedicated for Iphone and maybe some for Android also) is to check if height of the screen is bigger or lower than it's width.

function checkOrientation() {
if (window.innerHeight > window.innerWidth) {
//portrait mode
} else if(window.innerHeight< window.innerWidth) { //landscape mode
}
}
It's not possible to guess in which mode user enters the page (game in that case) so it's important to check it in the begging, and each time the browser's window resize.

checkOrientation();
window.onresize = function() { //your code
checkOrientation();
}
Try this link from your phone to test it, or just open it in desktop browser and change window size: LANDSCAPE VS PORTRAIT
I know it won't works in IE, but almost all of the mobile web browsers are based on WebKit.

Wednesday, August 18, 2010

Recursion of anonymous functions

When I declare anonymous function, like

(function(){
//do something...
})();
name of the function is not added to any scope, either local or global, because there is no name at all. When I want to call the function again, for example in timer functions like setTimeout(), I simply call arguments.callee inside it

(function(){
//do something...
setTimeout(arguments.callee, 1000);
})();
According to Mozilla Developers Center:
arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.

Tuesday, August 3, 2010

"Stairs to heaven in 6,22KB" competition!

Inspired by few videos posted in comments I would like to announce a little competition. Rules are as simple as possible: play my "StHi6,22KB" game, make a video from it (using your mobile, camera or just record part of your desktop using programs like CamStudio) with your result visible, upload it on any video-hosting site like Youtube or Vimeo and put the link in the comments on 10k Apart site (here) before 25 of August. I will prepare two truly unique "Stairs to heaven in 6,22KB" T-shirts - one for the owner of the best result and one for the randomly chosen participant (e.g. by 'true random numbers generator - random.org). So, good luck and I'm waiting for your videos!

My best try (of course it doesn't count):

Saturday, July 31, 2010

10K Apart competition

In the early '90, when I had my first contact with computers, I don't even heard about Cd-roms, or other freaky inventions like hard discs. I had 2 floppy stations, 3.5' and 5.25', with that 'bigger' (by size, no capacity) all the time busy with DOS disk, so everything I had was only 1.44MB. My favorite games then were Prince of Persia (yes, that one from top of this blog) and King's Quest (I finish it first time in my life after almost 18 years of playing, couple months ago on Sarien, online canvas versions of Sierra games).
Today I felt a gust of those days. 'An Event Apart' organized crazy web-app competition called 10K Apart with two main rules: total file size, including images, scripts and markup, must be under 10K and app must work in IE9, Firefox and WebKit browsers.
After about 8 hours of developing two games and bugfixing one annoying webportal simultaneously, let me introduce Stairs to heaven in 6,22KB.

Enjoy and don't forget to click '5 stars' on 10kA site:).