Better than nothing

Category: Game Development Page 1 of 5

Fix for Cordova error: Android target: not installed cmd: Command failed with exit code 1

I was trying to set up Cordova on my windows laptop this weekend. I followed all of the setup steps and ran

cordova requirements

but got this error:

Android target: not installed
avdmanager: Command failed with exit code 1 Error output: Exception in thread “main” java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema at com.android.repository.api.SchemaModule$SchemaModuleVersion.(SchemaModule.java:156) at com.android.repository.api.SchemaModule.(SchemaModule.java:75) at com.android.sdklib.repository.AndroidSdkHandler.(AndroidSdkHandler.java:81) at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213) at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200) Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)

A quick google search turned up lots of ideas for fixing the issue, including:

  • Downgrading to JDK 8 (Don’t do this – it’s outdated advice)
  • setting the environment and PATH variables (I did that already)
  • reinstall the JDK (I tried this – no luck)
  • create an AVD (This needs to be done, but still didn’t solve my issue)

Here’s a link to a helpful Stack Overflow thread that goes through some of these options.

I work on a Macbook for my job and I’m not too familiar with Windows dev tools setup. I spent hours trying to sort this out. Then I realized that Windows has TWO places to set environment variables: One for USER variables and one for SYSTEM variables:

So, I started going through the system variables. When I opened up the system Path variable, I saw my problem:

The JDK path was declared multiple times in the system variables. The image above is just an example – I actually had FOUR different JDK paths declared in my system path! Most of these paths pointed to non-existent JDKs. I removed all of the bad JDK paths, leaving only the path to the JDK I had just installed. Presto! My error went away!

Cordova requirements now threw a new error about not being able find ANDROID_SDK_ROOT, but that’s an easy fix. Just add a new User Environment variable called ANDROID_SDK_ROOT and give it the same value that you should already have for ANDROID_HOME.

As always, I hope that this saves you a little time and you won’t spend hours tracking down this issue like I did.

iOS SpriteKit Memory Leaks

I was building a SpriteKit game at work the other day and ran into a couple of annoying issues that were causing memory leaks in my app. The first one was created when I set up the GameScene (an SKScene subclass) in my view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    SKView * skView = (SKView *)self.view;
    
    // Create and present the scene.
    GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}

There’s one big problem with the code above: the view bounds haven’t been set in viewDidLoad. So, the GameScene size will probably be wrong. No problem, we’ll just move it to where we know the view bounds have been set:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    SKView * skView = (SKView *)self.view;
    
    // Create and present the scene.
    GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}

…except for one thing. Now, the GameScene gets recreated every time you come to the ViewController. If you are doing everything inside SpriteKit, then this isn’t an issue, but my app had several ViewControllers. Each time I left the GameViewController and returned, it would run this code again, creating a new GameScene each time. The old scenes were not being destroyed, so this created a large memory leak on an iPad Mini with iOS7 (about 20% CPU increase each time the GameScene started) that would crash the app after a few games.

The solution is simple:

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    SKView * skView = (SKView *)self.view;
    [skView presentScene:nil]; //remove everything from memory
}

This ensures that the GameScene is destroyed when you leave the GameViewController.

This solved most of my problems, but I still had a small memory leak. It wasn’t a big deal and I was tempted not to worry about it, but this game was going to be used in a tradeshow booth, which meant that it would be running for hours at a time. Under those conditions, even a small memory leak could be a big problem.

I was able to isolate the remaining issue to the fact that I set a protocol on my GameScene:

@protocol GameSceneDelegate 

@required
- (void) triggerGameOver;
@end

The parent GameViewController implemented the delegate to display a “Game Over” UIView. Even though I was destroying the GameScene, the delegate reference was still not being destroyed. It seems that SpriteKit classes are not reference counted in the same way as standard iOS classes (?). So, I replaced the protocol/delegate with an NSNotification that was sent by my SKScene and picked up by the parent ViewController. Decoupling the 2 classes fixed the memory leak and everything worked as expected.

Oddly, the memory leak only seemed to be an issue in iOS7. iOS8 worked fine. Maybe it was a bug that’s been fixed? As always, I hope maybe this helps prevent a bit of frustration for someone else.

I Love/Hate Game Engines

I haven’t posted anything in a while. I’m not being lazy – I’ve been focused on a game prototype that I have been fooling with for over a year. I picked it up again last month after a long break and I made a few breakthroughs – fixing several major issues by hacking the game engine I am using to build it.

I have a love/hate relationship with game engines (and frameworks in general). I’ve used a lot of code frameworks in my day job: Drupal, WordPress, Magento, BigCommerce, Gaia, Flixel, Flashpunk, Backbone, and a handful of others I can’t think of right now.

The simple fact is that any code framework is a philosophical statement. It’s a developer’s way of saying “this is the way that I think this (whatever) should be built.” Sometimes, they’re great. I’m personally a big fan of WordPress as a CMS and website framework. Other frameworks seem like they’re well thought out, but don’t align with my workflow, like Backbone or the Flash Gaia framework. Still other frameworks seem to have fundamental issues that makes them cumbersome to deal with. *cough*Magento*cough*.

I’ve been building my game prototype in Flixel, which is an Actionscript3 framework for building retro-style 2D games. It’s a really great framework, especially if you are building something with fairly typical game mechanics, like a 2D platformer or a top-down shooter. Unfortunately, I’m working on a concept that uses non-standard game mechanics, so I’m building a lot of pieces from scratch. Even worse, I spend a lot of time chasing down gremlins in the Flixel engine and modifying the core code.

This is where my love/hate comes in. On one hand, it’s a hassle to deconstruct someone else’s code and modify it for your needs. I spent nearly 8 hours tracking down a bug (or “unimplemented feature”, depending on your point of view) in the Flixel core that was caused by the fact that width and height measurements are stored as integers instead of floating point decimals. There’s actually no reason why they’re stored that way, except that the Adam Atomic (the creator of Flixel) never needed the decimal part of the width and height, so storing integer values made sense. I needed the decimal parts and the rounding errors were causing all sorts of ridiculous problems. I’m currently dealing with another issue that Flixel doesn’t handle – how to collision test for a “squish.” That is, how do you know if a player has been smashed between two objects?

Despite these hassles, using a game engine is a better way to go than building everything from scratch. First, you get a lot of features pre-built for you. Flixel handles all of my rendering code, object placement, and most of my basic collision testing. That’s a lot of code that I didn’t have to create from scratch, which saves me a lot of time. Additionally, there is a community of developers using Flixel – so, I can post questions to the community forums when I get stuck.

Developers tend to have strong opinions. So, it’s rare to find a framework that perfectly suits your coding style and workflow. This actually leads a lot of developers to create their own frameworks. A few years ago, when I was doing a LOT of Flash work, I developed an Actionscript3 framework that enabled our team to knock out flash websites and apps very quickly. It suited our workflow, which was very animation-heavy. I have seen similar approaches in other Flash frameworks, but ours was tailored to our specific needs.

I know some developers who will create an entire framework for a single project, but this seems like a lot of work to me if you’re not sure that you’ll ever reuse the code. I have a personal “rule of 3” when it comes to building reusable components:

    • The first time I need something, I create the code and drop it in where I need it.
    • The second time I need this same functionality, I cut-and-paste it from the previous project and probably clean it up a bit.
    • The third time I find myself using the same code, I package it into a reusable component so it’s easier to implement in the future.

My Flash framework grew out of a collection of these components that I organized and refined over a few years.

If you build similar types of things over and over, you can create your own framework organically this way. Otherwise, I suggest using existing engines and frameworks. There is no need to reinvent the wheel for a one-off project. It’s also useful to see how other people handle various problems. I often find useful bits of code that I can swipe for my own projects. As much as using someone else’s game engine isn’t ideal, it still saves me a lot of time. I really appreciate the folks who open-source their frameworks and spend time maintaining them for the rest of us.

If you care to try Flixel, the most recent version is currently being maintained by the community on Github.

Here’s a link to my hacked version of Flixel. Use at your own risk!

Using Intellij IDEA for Actionscript Development

I’ve been a die-hard Windows guy for a long time, but this year I switched to using a Mac so I could learn some iOS development. Since I made the switch, there are a few PC-only programs I miss. One of them is FlashDevelop – which I use(d) for all of my Flash/Actionscript work.

Flash is dying – that’s a fact. Not many new projects are being built in Actionscript these days, but I still get some Flash work trickling in now and then. I’ll also have a need to maintain or update older Flash projects for a few more years, so I need a good Actionscript IDE on my Mac. I tried running FlashDevelop on Parallels, but there were a few nagging problems with it that made it less than optimal. After doing some research on Actionscript IDEs for the Mac, I settled on Intellij IDEA. I was already using PHPStorm and I liked it a lot, so I thought I would give Intellij a try.

It’s been a great IDE for AS3 work. I like it almost as much as FlashDevelop, but the documentation is lacking a bit. Figuring out how to tweak the project settings was a hassle, so I made a video tutorial showing how to set up an Actionscript project in Intellij IDEA:

BTW, Intellij IDEA is an awesome all-around IDE that is definitely worth the $200 license. I use it for basically everything. Check it out for all of your HTML, CSS, javascript, PHP, Ruby, Rails, and JAVA needs.

Page 1 of 5

Powered by WordPress & Theme by Anders Norén