teaching machines

Final Project: Virtual Baby – wettstaj

December 20, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

The application i build is called “Virtual Baby” and it is exactly as it sounds like it is. It is a simulation of a baby on your phone.

Although this project isnt fully complete, it is working properly and I would like to continue working on it in the future. Basically, i just need to add different features that make the actual simulation/game more realistic.

There inst much I changed from the design document besides cleaning up the code a bit for readability. To be honest I did not change the actual code from what i had had before, just moved it around a bit.

Virtual Baby tries to turn the phone into a baby that you carry with you wherever your phone goes. The idea is that the user learns what its like to take care of a baby in its early stages of life. Right now though, my baby just blinks and stares at you and then cries until you rock it for a minute or two. At least thats what it does on the outside, but lets look inside the code.

So there is a basic structure to this project that I was working with. I needed a way for the images of the baby and the activities that you do to make it stop crying (and other things) to be separate from one another so I chose to start the app in a service, and handled all the crying events in that area of the code.

Since I want the user to not be in control at all of their baby starting to cry I had the app start up an onBootReceiver. This allowed the service to check if the baby is on schedule to cry. If it is, then the phone starts to cry wherever you are and sends you a notification at the top of the screen. If you are in the app already and it reaches the time for it to cry, nothing different will happen because the events for crying are separated in the service.

My service currently runs a game loop thread that runs every couple seconds and checks to see what the current system time is. If it is time for an event, then it will start the event that is intended to start. In this case, the only event that starts is crying. The phone will make a cry sound and the service will wait until the rock action is preformed. I will talk about this later. Here is some service side code:

Service:

Event Handling:

On to the main part of the app. For this i needed a way to be able to easily manipulate the images on the screen.For the main screen i used the traditional stuff that android lets you use for GUI, creating an XML for it along the way.

Now on to the events (rock burp etc.). At first i tried to do this the traditional way, with layouts and such, but i ended up coming to a dead end when i was trying to handle the animation of the rocking. Eventually I used my own surfaceView. There is a surfaceView for every action that the user could take to stop the baby from crying. However, the only one I got working was rocking the baby. I did this so i could play with the accelerometer. I knew this would be the hardest part of the project so i went with that first.

Basically the surfaceView just starts another game loop parallel to the services game loop. When the window is started, a red circle is painted on the screen. The red circle moves up an down the screen, accelerating  when it moving towards the middle of the screen and decelerating as it reaches the edge of the screen. There was a lot of calculations that went into this.

The second part of the rocking surfaceView is the green circle. The grey circle cannot be seen above because it is controlled by the accelerometer which, as you know, doesnt really show on the emulator. Basically, the object is to follow the constantly moving red circle, which is your guide to rocking. When the grey circle is inside the red circle, the red circle turns green, letting the user know that they are following the circle correctly.

Here is some code for the Main side of the program:

Main
this connects the service onto the main.

Main goes to Rock activity
here is where the accelerometer data is collected:

rockSurfaceView
Starting the loop and setting up the canvas

RockGuide
Here is where some collision detection and using onDraw is handled:

RockableObject
This is where the actual accelerometer data is used:

please let me know if you have any questions :)