Renoki: Making An Android Game (part 13)

I discovered a very annoying thing recently that made me have to reconsider how I handled touch input.

Originally, my game consisted of a game loop that updates the state of all the sprites and then draws them onto the screen. Touch input was handled asynchronously, meaning I need a lock around my update section. The reason is that I don’t want touch input to affect the state of anything while I’m in the middle of updating, otherwise I could have a race condition where the update section is about to jump to state X, the user presses a button to go to state Y, and then the update section resumes and pulls it back to state X.

Now I understood the problems that could arise from this. Touch input needs to be processed relatively quickly or you get Android Not Responding errors. I felt that even if my touch input had to wait for 1 or 2 update loops to finish, that is still only fractions of a second. Unfortunately for me, sometimes the Android kernel will decide not to give the touch input priority to run. Instead, it will allow the update loop to keep acquiring the lock over and over, resulting in the dreaded ANR. And there wasn’t any sort of fair lock I could use, because I was just using Java’s “synchronized” command, which is supposed to handle locking for you.

I realized that I would have to make a queue of all touch events. When a user touches, it enqueues into a touchQueue. In each loop iteration, I dequeue 1 touch and process it. This way, you only have to lock around the touchQueue, which greatly reduces the chance of a touch event getting blocked.

It’s at this point that I realized something else stupid. When a touch occurs, it calls some onTouchEvent(MotionEvent event) and passes you a MotionEvent object which describes properties like the x, y coordinates, and whether it was a down or up press. Unfortunately, you need to make a copy of the MotionEvent it passes you, because after the onTouchEvent() is done, it will reuse that object. I sat around for a while, wondering why the MotionEvents in my queue were getting corrupted and it took me forever to realize this.

Now you need to consider having a pool of Events. It’s inefficient to recrete a new Event object every time a new touch is made. It’s better to have a pool of Event objects. When a new touch is made, you request an Event object from the pool. If the pool is empty, it creates a new one for you, otherwise it hands you a pre-existing one. When you are done with the Event object, you add it back to the pool for future touches to use. This saves on the overhead of dynamically allocating a new Event object every time there is touch input.

The Witcher 2

There are so many good things about The Witcher 2 that end up getting brushed aside due to stupid quirks with the interface or controls. I would say story wise and entertainment wise, it was as good as Dragon Age 1. That is saying a lot, because Dragon Age was the kind of game where I stayed up past 3-4am playing sometimes.

Quests are fun. Story is solid and engaging. There are 2 unique storyline paths and lots of twists and turns. The world is pretty, graphics are OK, and dialogue is fine. It’s an “Adult game” so apparently it needs to have an excess of sex scenes. Everything about this game is top notch, except for some glaringly bad interface problems.

Looting Items
When you want to loot the corpse of the monster you just killed, you have to go up to the remains and left click. Unfortunately, left click is also the button that swings your sword and if you are not exactly over the body, instead of looting you make a lunging sword slash, moving you out of range to loot. If you play this game, I can guarantee this will frustrate you throughout the entire game.

When you loot, it pops up a window of all the items. Unfortunately, it pops up the window in a static location, not under your mouse. You then have to move your mouse to the window and click to collect all the items. Back in the old days of WoW, you had this stupid looting system as well where you had to make additional clicks to loot specific items from the corpse. People got so frustrated by it that they invented mods that would automatically position the loot window to appear under your mouse cursor. Then Blizzard finally invented an option for auto-loot, where you automatically collect everything assuming your inventory isn’t full. This is a change that is absolutely necessary in any game where you loot the corpses of enemies. At the very least, you need to allow a 1 click thing to loot all and not require the user to move your mouse to the small “Loot All” button every time.

Menus and Inventory
This game has possibly the worst inventory screen of all time. When buying new equipment, it’s impossible to compare it to what you are already wearing, so you don’t know whether what you’re buying is better. You can’t sort items, so by the end of the game you have hundreds of crafting items in a random list and you have to manually scroll through it to find the 1 leather piece to finish your armor.

Equipment and crafting diagrams often have long lists of stats or information, which does not fit on the screen. You can’t manually scroll through and read this though. The information slowly scrolls and you have to wait for to get to the part you want to read, like the old scrolling TV Guide channel.

Meditation
The game is strongly based around taking potions before combat to temporarily increase your stats. In order to do this, you have to “meditate” and then drink potions, which involves two painfully long animations of Geralt first kneeling on the floor, slowly drinking a potion, then cracking his neck due to the toxicity. It’s probably 30-45 seconds of wasted time. All of this can only happen out of combat, so there is no reason to require this mechanic. You should be able to set some hotkeys or at least just click on things to instantly drink potions. It’s just plain stupid.

Combat System
I played on Hard (they had easy, normal, hard, and some super hard adjective I forget). I did that because I expect that most games nowadays have dumbed down normal modes to accommodate baddies, and I expect their super hard is probably geared towards Witcher enthusiasts that probably have already played through it once. Hard seemed like it would give a good challenge while not being stupidly unfair.

Initially, the game has some really hard sections. The Kayran, the game’s first boss fight, is incredibly difficult. You can only take 2 hits before dying, and there is no healing. It probably took me 20 tries just to get into phase 2 and then another 10 tries to figure out what the heck you’re supposed to do in phase 2. Unfortunately, after that fight, the game starts to become kind of easy.

There are a bunch of fights where you are with allies. You can always trick the game AI into only attacking your allies (who are invincible by the way) and you just come behind and slash at them, with no risk. After a while, you just start spamming left click to whack down enemies as fast as possible. There’s some magic spells which are marginally useful, but since I did sword specialization I stopped using them except for the one that gave me a shield.

Anyway, the game is solid and definitely worth playing. If they fixed up the interface problems, it would be one of the best games I’ve ever played.

Lytro camera

This camera is awesome.  I wish it weren’t so expensive, or I would buy it!

Lytro made a camera that takes photos you can re-focus later.  It results in these awesome interactive photos, and I feel like I discover something new when I change the focus.  The camera itself is also very small and compact… like I said, I wish it wasn’t so expensive!

I was reading the description of how it works, and it sounds really neat (and really complicated).  They emphasize the fact that the camera gets “lightfield data”, but I think the code to utilize that information and create the new re-focused photo is more impressive.  They call it their “Light Field Engine” and even lets you play with it right on the camera.

I saw the photo below and immediately thought of a horror-themed series, maybe where the reflections in the glass are less clear when you focus behind them and are actually really freaky ghosts or monsters.  I hate scary stuff, but that would be awesome – you’d re-focus and BAM a ghost!

Inspiration

Just saw this on Reddit, and I’ve decided what my next crafts project will be to get me back into crafts. I’m going to cross-stitch a Monokuro Boo iPhone case! The Reddit post linked to this website, where you can buy iPhone cases made for cross-stitching. I’m excited! I already have a lot of leftover needles and embroidery floss from the crafts class I taught, so I don’t even think I need to buy anything other than the case. Next step is just designing the Piggy and Emo Piggy design!

Also, I’ve decided my next technical project is going to be forcing myself to REALLY make the new layout for this site.  I’ve said it for over a year and haven’t done it, but this time I REALLY am going to do it!  Really!

In addition I really need to make the author of each post more obvious… although I giggle every time I see the smiley face next to my name and the frowny face next to Jack’s :D

EDIT: Added Piggy and Emo Piggy tags because Emo Piggy was mad I didn’t tag her

Moneyball

I cannot fathom why it was I decided to watch this movie. My guess is that I hate baseball so much that watching someone break the system would make me feel better. Or it’s because I secretly have a crush on Brad Pitt and his name is so large on the movie poster that you need to look twice to find the actual title.

Anyway, some guy invents some system on how you should draft players solely based on their statistics, instead of basing it on irrelevant factors like age, appearance, personality, etc. For example, they decide to draft this short guy because his height causes him to get a lot of walks. Brad Pitt decides to follow this strategy and after a rocky start, his team starts winning and makes it to the playoffs. This is despite having around 1/4 the overall budget of bigger teams like the Yankees. Then he gets offered some job by the Red Sox for a shit ton of money because apparently he revolutionized baseball.

This movie isn’t bad, by any stretch of the imagination. There’s nothing wrong with the acting or story. Everything flows reasonably well and there are a few funny moments. However, at the end of the movie, I just thought “OK” and left. I didn’t really think about the movie while driving home and promptly forgot about it as soon as I started doing something else. The movie just barely held my attention for it’s duration and as soon as it finished, there was no reason anymore to give it another thought.

Maybe it’s because I’m not an Oakland A’s fan or a baseball fan. Or maybe it’s because the entire movie was based on the mundane task of using math and spreadsheets to excel at a game. Or maybe it’s because they shot too many closeups of Brad Pitt’s frustrated face when they lost games, which I swear happened at least half a dozen times. Whatever the case is, the movie is OK to watch but in my opinion doesn’t have any lasting impact.

Edit: Now I remember why I wanted to see this movie. It’s because Activision-Blizzard CEO Bobby Kotick is in it. I think his appearance is the only reason I even knew the movie existed.

Renoki: Making An Android Game (part 12)

FrameLayout with a ScrollView

My game consists of a single FrameLayout with a child SurfaceView. This essentially gives me a blank canvas to draw anything I want to, which works fine because I have a static background. What I wanted was when the user presses the “About” button, I would display the credits and the user could vertical swipe to scroll through them. At the bottom there would be an exit button. A bonus would be I could interweave images in between the credit lines, but that wasn’t necessary.

I think the typical way to do this would be to create a new activity that you create with the credits. However, I didn’t want to stop my current activity because I use a gameloop and gamestate that is local to my main activity. What I really wanted was for when the user touches the “About” button, a new ScrollView appears on top of my current SurfaceView and I change the gameState to be the ABOUT state. This is a surprisingly annoying topic to search and I had to look at a dozen posts online until I could get something to work.

First of all, you will need to create your ScrollView programmatically instead of through XML. One of the things that originally turned me off of Android was how they used XML to define the layout of the UI. I’m sure there is a good reason why they did it, but all I’ve noticed is it pisses me off and makes it harder to find the answer because most of the time people provide XML solutions. Anyway, you need to make sure your FrameLayout has an ID field so you can get a reference to it in your code.

A FrameLayout can have multiple children views, where each view will overlap the previous one.  So what I want is to create a ScrollView and add it to the FrameLayout.  A ScrollView can only have 1 child view (you can only insert 1 view into it).  Because I want to display both images and text in the scrolling area, what I did was insert a LinearLayout into the ScrollView.  Inside that Linear Layout, you can insert as many ImageViews, TextViews, etc that you want.  It will all be scrollable.

Lastly, when you want to close the ScrollView, you merely remove it from the FrameLayout. You can remove it either by passing the same View you originally created or passing the index of the View you want to remove and using removeViewAt(int ID);

The neat thing about this is if you set the size of the ScrollView to be less than the fullsize of the original SurfaceView, you can still see whatever is underneath it. TouchEvents on the non-covered sections will still trigger on the original SurfaceView, so you can reuse whatever touch logic you have from your original SurfaceView. This is how I close the ScrollView.