
We recently came across a problem that stumped us for quite a while. We needed to have a timer run some code every second the app was open. This is fine until you lock your iDevice.
We noticed that when we manually locked the device and come back that the timer had stopped. It turns out that you can apply a fairly simple solution to this problem.
By playing a sound continuously, you force the device to keep working even after it is locked. We added a tiny sound file to our project and set it on loop with the volume turned off. In Titanium the code looks like this:
var silentSound = Ti.Media.createSound({url: 'Sounds/silent.wav'
});
silentSound.volume = 0;
silentSound.setLooping(true);silentSound.play();
And viola. All timers continue running even when you manually lock. Just be
sure to stop and release the sound when you don’t need it to conserve battery.
It is common in physics based games to need to be able to drag bodies around. However, setting the position of a body instantaneously based on the current touch position can introduce excessive forces to the physics simulation, and break it. So we need another way to solve this problem…
Enter Box2D’s mouse joint. In our modifications to the ti.box2d module, we have added the mouse joint for use in Titanium games using Box2D. Creating a mouse joint is simple, as shown in the code below.
var mouseJoint = world.createMouseJoint ( groundBody, draggableBody, {
maxForce : 1000,
dampingRatio : 0.6,
frequencyHz : 1.0,
collideConnected : true
});
In the above code sample , world is the result of a call to ti.box2d’s createWorld function. All joints require 2 bodies, even though we generally think of the mouse joint as really only effecting a single body. When creating mouse joints, that second body (the first parameter in this function) is generally a static ground body. The second parameter in our createMouseJoint function is the body that we want to drag around.
We’ll come back to the other options in a moment. First, let’s actually drag the body around. In order to have the body dragged towards a point, we must call the setTarget function on the mouse joint with the x and y coordinates that we want to drag the body to. You’ll generally want to do this in an update loop or anytime the mouse (touch point) moves.
view.addEventListener('touchmove', function(e)
{
mouseJoint.setTarget(e.x, e.y);
});
That’s all you need to start dragging a body around. Now lets circle back to those other options in the creation of the mouse joint. The maxForce property is the maximum amount of force that will be applied to the body in order to get it to its target point. collideConnected indicates whether the draggableBody should be allowed to collide with the groundBody.
The last two properties, dampingRatio and frequencyHz, are the parameters you will need to tweak the most to get the kind of dragging feel that you want. If you want very tight dragging, you will need a higher frequency and damping ratio. If you want some oscillation after the body reaches it’s target point, you will want a damping ratio less than 1.0. Check out the Box2D Manual to learn more about these options.
Have you ever had multiple emulators or physical devices or a combination of the two attached at once? It’s frustrating to shut off or unplug your devices in order to single out the one that needs to be targeted. Look no further, adb includes a command line switch to choose the specific device you want to target.
Let’s begin by listing our currently attached devices.
$ adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
HT09PR217646 device
As you can see, we have three devices attached to our system. Two emulators and one physical phone. Now let’s logcat our physical device because it’s running our application that we want to debug. Just pass the -d switch to adb to target our device.
$ adb -d logcat
If we had a single emulator instance and one or more physical devices attached we could pass in the -e switch to adb which targets emulator.
$ adb -e logcat
The final switch we can use for device targeting is the -s switch, which stands for serial number.
$ adb -s emulator-5554 logcat
$ adb -s emulator-5556 logcat
$ adb -s HT09PR217646 logcat
Stashboard is an ope
n source status page developed by Twilio and graciously released to the general public. It allows you to give your users an information resource when a server or service goes down. (See: Comcast Network Health, OpenDNS System Monitor, Urban Airship Status, Rackspace System Status)
One of the main benefits that Stashboard provides is that it can be deployed on Google App Engine — for free. This means it will not be directly tied to your infrastructure; which means if your entire system goes down, your users can remain joyfully informed.
Our only gripe with Stashboard is that it’s designed to be updated manually. We wanted to use it internally so we could quickly check on the servers powering Tulip or delivering content to Suite Tips or iYummi. Fortunately, it only takes minor amounts of hacking around in Python to make this possible. We present to you: hacks.
First, we have the workhorse of this particular hack. This is the script that will query our servers. You will need to modify the ‘servers’ array starting on line 14; just add your servers URL and service name (this is the ID you give to your server when you create it in the Stashboard admin.) Place this file in the project’s root directory.
After that, you just need to create a file called cron.yaml and put it in the project’s root directory as well.
And copy/paste the following code into your app.yaml file to create a pointer to ‘/check’.
Now you should have a functioning server monitor running on Google’s servers that will check your server statuses periodically. In the next episode, we’ll show you how to add notifications. Stay tuned.
CEO and Co-founder of Shockoe.com, LLC
Entrepreneur and experienced software engineer, interested in everything that involves mobile technology.