PriorJS: Lightweight JavaScript Preloader & Asset Manager

So when I was developing my first game Stellar Alien, I realized I’ll need to preload images and sound before they can be used. There’s some libraries already that do this but a lot seem pretty overkill to me, I just want to preload my assets, and maybe be able to manage them.

Maybe some others are feeling the same way, so I took the asset manager I wrote for Stellar Alien, polished it up a bit, gave it a catchy name, and released it under MIT License.

Check out PriorJS on GitHub: do whatever the heck you want with it.

 

A bright future for mobile HTML5: Firefox OS

A few blog posts back I talked about my experience as a teenage developer writing a HTML5 game from scratch and how I thought it was ready for some pretty sweet games. My opinion hasn’t changed: HTML5 is still ready for great applications. It may not be the best for all your applications, but it is should be an option to consider. HTML5 allows for rapid development, cross-platform, and it’s open. However, there are always the critical performance projects that must be native.

The Consumer says: HTML5 is too slow, I’ve heard about it!

Even if you aren’t a developer, you probably still have heard the buzzword “HTML5″ and how Mark Zuckerberg has said it is one of “Facebook’s biggest mistakes” to be relying on it. Let me tell you something, some of the media (un-shockingly) took this quote out of context. And I’m sure some of them know this, but they keep on using it.

Here’s a quote attributed to Zuckerberg from the W3C Mailing List:

“When I’m introspective about the last few years I think the biggest mistake that we made, as a company, is betting too much on HTML5 asopposed to native… because it just wasn’t there. And it’s not that HTML5 is bad. I’m actually, on long-term, really excited about it. One of the things that’s interesting is we actually have more people on a daily basis
using mobile Web Facebook than we have using our iOS or Android apps
combined. So mobile Web is a big thing for us.”

Per the quote above, he is not saying “HTML5 sucks”, “HTML5 is bad”, or “HTML5 is slow”. Not that at all. Comparing HTML5 to Native, what Facebook’s mobile app really needed was native. That’s all. No big deal.

I’d like to point out something because I found this the most interesting part of this quote:

“One of the things that’s interesting is we actually have more people on a daily basis
using mobile Web Facebook than we have using our iOS or Android apps
combined. So mobile Web is a big thing for us.”

Again, per the quote above, they have a large amount of users on their mobile website instead of their mobile applications. This is quite funny, as a lot of the media talks about how slow HTML5 is. Yet the majority of Facebook’s users are using their mobile website which is ran completely on HTML5.

So the question is: “what was wrong with Facebook’s native application if HTML5 works fine on a mobile browser?” I’d say the WebView was the issue. Using a WebView was my first attempt at making my HTML5 game a native application. The WebView is insanely slow. It is so slow. Your brain will begin to process thoughts in an incredibly slower rate if you think about how slow that WebView is. Seriously, it’s slow!!!

Developers may be able to optimize the WebView by configuring certain settings (high rendering priority, enabling hardware acceleration, caching behavior). But there have been some great developments that offer fast alternatives to the WebView.

A blazing fox decides to make his own open-source mobile operating system

firefox-os

For the past couple of years, Mozilla has been working on a huge project: FIrefox OS. Firefox OS is an open source Linux-based mobile operating system written in HTML5 & C++.

I’ve recently been playing with the operating system using the simulator and I can say it is going to change the perspective of HTML5 applications for the good. Firefox OS will allow HTML5/JavaScript to communicate to the mobile device’s hardware directly.

HTML5 Applications on Firefox OS will work as if it was written in native code. I believe Firefox OS will burn Chrome OS into ashes (cause you know, the fox is on fire). Most Firefox OS applications do not require internet connection which is great.

When I talk about HTML5 Applications on Firefox OS, it is not the same as if they were running on a browser and dealing with all that extra stuff to get it working. HTML5 Apps on Firefox OS will run on an native runtime. We’re talking about some deep stuff here guys.

Firefox OS is currently pumping out:

  • Web Telephony API – Phone call state & control
  • Web SMS API – Text Control & Interact
  • Screen orientation – Control & Detect
  • Network Info, Power Management, Battery Status

This is all the sweet stuff you’d expect in an native Android application. I think Mozilla is going to hit the mark with this one and I’m honestly very excited.

Firefox OS is already hitting it big with device makers, LG Electronics, ZTE, and TCL Corporation reported plans to create devices shipped with Firefox OS on them. LG is already thinking about making a smartwatch with Firefox OS. Wouldn’t that be pretty cool, for users to use your HTML5 App on their smartwatch?

hacking-firefox-os-slidesFirefox OS is a fully open source ecosystem which is really great. You’re not closed in, my friend. I can download the Firefox OS source code and start hacking away & start adding new features, fixing bugs, and more.

We’ll see how it turns out, but I am quite sure that Firefox OS’s future is going to be swell.

Conclusion:

Now is a better time than ever to consider HTML5 for your mobile application. With the specification maturing, better browsers like Firefox & Chrome, Firefox OS packaging your HTML5 App into working like a native, and native wrappers dropping the Webview and becoming hardware accelerated, HTML5 is a viable option to choose for your mobile application, and it’s just getting better.

Turning a HTML5 Game into a fast, hardware accelerated native mobile app

 So I finally released my first game, Stellar Alien, in the Chrome Web Store. Development took a bit longer than I expected, but I got it done. When I was developing Stellar Alien I knew it had to work well on Mobile Devices. One of the strong points of developing in HTML5 is that you can write once, and be cross-platform with no porting at all. Stellar Alien is finally available in the Google Play store, but it was difficult to find the best way to turn it into a native application.

 

This is what I found out about making a HTML5 Game into a native mobile application:

Users are more used to installing apps from the marketplace

Not sure about you, but when somebody tells me they have a new app for Android, I’d assume it’s on Google Play. I wouldn’t launch my mobile browser and ask the person “Okay, what is the link to it?”

Stellar Alien was just a web app initially, but I knew I had to turn it into a native mobile application somehow.

Webview is horrible and it is too slow

When I researched ways I could turn my HTML5 game into a native application, PhoneGap popped up. PhoneGap looked okay to use, so I tried it out with a small & quick script. I ran a simple test with a <canvas> fading in using jQuery.  WOW, was that slow. It also took a bit of time for that small little script to load up. I loaded up my game on it, and the FPS was too low, making it unplayable.

I was curious why it was so slow. I found out it’s not PhoneGap’s fault, but it is the WebView. Great, so now what was I going to use? It seemed like similar software to PhoneGap also used a WebView.


Finally, I find Ludei’s CocoonJS

A few months ago while I was living in Seattle, WA, I visited a JavaScript Game Development meet up, which felt a little awkward being the only teen there, but everybody was friendly. Somebody from Ludei came and gave a talk on their new technology, CocoonJS. CocoonJS takes your canvas based HTML5 Games and turns them into a blazing fast, hardware accelerated native application. My first impression of it was that it was “just PhoneGap, specifically for HTML5 Games”. The demos that the speaker showcased at the meet up proved me wrong.  The speed was great, with FPS averaging around 250 on an Android Tablet. He later explained that CocoonJS binds canvas calls to OpenGL ES, making it hardware accelerated.

The fact that there isn’t a WebView & that my canvas is hardware accelerated, really made me excited about CocoonJS.  Another thing is there’s no SDK. You do not have to do any code changes or use some amazing, magical SDK. It’s awesome.

Let’s make this clear, CocoonJS is not a browser. How Ludei describes it is that it is a “JavaScript Virtual Machine”. For your game to be hardware accelerated using OpenGL ES with no code changes, your game needs to be Canvas Based. If it’s DOM-based, you’re out of luck. However, CocoonJS does also have a WebView extension that is easy to use & has some great features. I would take a look at that if your game really, really needs to be DOM-based. Note that their WebView support is still pretty new and might be buggy (my experience).

If your game is canvas based, great! You’ll have a hardware accelerated, native HTML5 game in no-time. CocoonJS also supports a lot of popular game engines.  These are the engines they currently have listed at the time of this writing:

  • CAAT
  • ImpactJS
  • Construct2
  • Cocos2DX HTML5
  • Isogenic

If the engine you use for your game isn’t listed, it may work if you try it out (but be sure everything is being rendered with the canvas API, no DOM!)

My experience using CocoonJS was insanely simple and easy. I would definitely recommend it if you need your canvas based game to become a native application, quick and easy(and fast!)

Here’s how simple it was:

  • Install the CocoonJS Launcher App to test your game to make sure it works well.
  • Launch your application (just give the launcher a link to a zip file containing the code)
  • Play through it, make sure everything is pretty & smooth.
  • Works great? Now you can use CocoonJS’s Cloud Compiler.  Their compiler will compile your game into the selected marketplaces (currently iOS, Android).
  • Get the APK, sign it like usual, & upload that baby to Google Play and/or Apple AppStore!

If you run into any problems, Ludei’s support is great and should get back to you in less than a couple of business days. The updates usually come around monthly(lately) and they fix a lot of bugs.

Conclusion

So that’s how I finally got Stellar Alien on Google Play. The link is here, perhaps it would be fun to play while you’re checking out CocoonJS, and that link is here ;)

Have fun!

Newbie’s thoughts on HTML5 Game Development

There is a lot of mixed opinions about HTML5 & Game Development. You ask one person, they will say HTML5 is not ready, or it’s too slow for real games. Another will say JavaScript isn’t a real programming language and it’s a horrible mess. Some will say HTML5 is ready to go and anyone can get started. So what’s going on? As a 14 year old who loves to program I finally decided to try out game development. HTML5/JavaScript were the technologies I chose to use for developing my first game, “Stellar Alien”. I was leaning more towards Java or Python, but I’ve always preferred the Web Platform. The Web is the most open platform of all and it’s a very large audience you can reach out to (after all, it’s the world-wide web!).

Naturally, when I heard about HTML5 and its capabilities, I decided to play around with it to see what it was about, although I was a bit skeptical. HTML is just a markup language after all, how are people making games that are comparable to the ones that were developed in “Adobe Flash” with it? I started diving in and found that the game logic is actually written in JavaScript and HTML5 provides a new <canvas> API that allows JavaScript to draw on a 2D Surface.

After a few months, I completed my first game in HTML5/JavaScript, “Stellar Alien”. It was a very interesting project and I learned a lot about game development, developing a large project & programming in general. I’ve decided to post my thoughts & opinions on HTML5 Game Development because I think it may interest some Web Developers who’ve been meaning to experiment with HTML5, but are skeptical due to all of the mixed feedback about developing games in HTML5.

HTML5 is ready

But it’s not perfect. Neither are your other popular browser-based games alternatives such as Flash, Unity3D, or Java(via applet). There are some quirks and you find yourself sometimes coming up with a little “hack” to get something to work properly, and those aren’t fun. HTML5 Game Engines can really save you the pain of getting started in HTML5 Game Development, although I wrote Stellar Alien without one and it was not too bad.

Although I do think it’s ready, I don’t think EVERYTHING should be in HTML5/JavaScript now. It depends on your game and your goal. Although I do think you should always try to prefer HTML5. It’s a very open platform and you don’t have to work and pay for Flash’s IDE, or work in Unity3D. Just fire up your favorite text editor and get going.

Browser Support is decent

It’s not 2009 anymore! I see a lot of reasons why people do not fully dive into HTML5 including old-news about it having bad browser support or it will only work with a very minimal amount of browsers. It is 2013 at the time of this writing and the support for the important HTML5 Technologies most 2D games need (<canvas> tag, <audio> tag, localStorage) is great. <canvas> is supported on all major browsers that you should care about, including Firefox, Chrome, Opera, IE9, & Safari. It is the same for the new <audio> tag. Although the HTML5 Audio has some bad issues I’ll discuss in a minute.

Canvas is fast(enough)

And if it’s not, you can make optimizations, like any other software you would write.
You can implement “dirty rectangles” so your game will only clear pixels where it’s necessary and not the whole canvas every frame(although some game engines take care of this).  If you have a large background image like my game has, you should draw that background image on another canvas, that isn’t in a loop, and then draw your foreground canvas on top of the background. This is so that your large image is only drawn once, and not every frame.

Game engines, game engines!

Not everyone wants to start coding a game from scratch like I did. I understand that. You can get started much quicker by using a game engine. There are many game engines out there for HTML5 that really make developing in it a lot more pleasant. ImpactJS is a very popular game engine, although it’s $99 dollars. If you dislike commercial game engines like me, and want something free & open source, then check out CraftyJS, it’s one of the favorite ones I’ve played around with. Also look at this table on Github that lists tons of open source game engines to use:https://github.com/bebraw/jswiki/wiki/Game-Engines

HTML5 Audio sucks

The HTML5 <audio> tag wasn’t fun to develop in. It is buggy, it is bad, it just isn’t good. And sound is an important part of a game. I’ve noticed some sound would stutter, like there was some latency issues, some wouldn’t even play at all!

But not all hope is lost! The Web Audio API is a more advanced JavaScript API for doing all that crazy audio stuff on the Web than the <audio> tag, and it’s awesome! At a glance, it looks like a complex API, but you simplify it and build your own little audio system.

So should you use Web Audio or the <audio> tag? Well, it depends on your game really. All my game had to do was play a few sounds, some background music, and it was okay, so I ended up using an open source audio library called YAAK, which uses <audio> tags, but saves me some of the pain of working with them. Other games may need more complex audio programming, and some even have gameplay that depend on sound! That is where Web Audio should definitely be the one your game should be using.

One problem with the Web Audio API is that it’s newer than the <audio> tag. Which means browser support isn’t as good. Web Audio is supported on Chrome & Safari 6.0. Firefox has planned support for Web Audio but they are seriously taking their sweet time (perhaps they’re focusing on their awesome new OS).

Conclusion

Developing my first game in HTML5/JavaScript was a blast (most of the time). I will definitely consider developing my upcoming games in HTML5 again. I hope more indie game developers, web developers, web designers, or newbies to game development to dive into HTML5 for making their games and have a great time hacking!