Sensors: How to use the Orientation Sensor

Smart phones and tablets contain several kinds of “sensors” to sense information about their environment. For example, an accelerometer provides information about movement of the phone (or tablet) – letting us know the speed and direction of travel in x, y and z coordinates (three dimensions).

The orientation sensor tells us if the phone is tilted to the left or right, or up or down (or flipped over). App Inventor provides a simple to use interface to the orientation sensor. We can use this to control a sprite or ball on the screen – by tilting our phone, we can make graphic items move around on the screen!

Sample source code that you can download is at the bottom of this post!

After going through this tutorial, you now know enough to create your own interactive smart phone game!

Review

Before we get started, please review these earlier tutorials:

Continue reading Sensors: How to use the Orientation Sensor

Some history behind App Inventor

The history of App Inventor extends, in part, back to the 1980s, at least, which may sound odd.

App Inventor is a project, today, of the Massachusetts Institute of Technology but is based on the App Inventor project that was originally started by Google. Google’s project, turn, was based on an MIT Master’s thesis by Ricarose Roque.

The graphic building blocks approach to assembling program functionality was itself inspired by Scratch. Scratch is a simple programming system used for teaching programming concepts (especially to those age 8 to 16). Like App Inventor, Scratch has taken on a life of its own and is also hosted at MIT and runs in your Internet browser (plus a download version that runs on your own computer, is available). If you are interested, you can learn and use Scratch at scratch.mit.edu. You can learn about the history and philosophy of Scratch in this Communications of the ACM article. (ACM is a professional society for computer scientists and publishes many journals in the field of computer science.)

Continue reading Some history behind App Inventor

Controlling the sprite animation with your finger on the screen

In the prior tutorial, we created a sprite-based bouncing animation – which is surprisingly easy to do in App Inventor.

But how might we make the animation interactive? Such as touching the bouncing object with a finger and flinging it (throwing it) off in another direction? In other words, as the object travels around the screen, we would like to touch it with our finger and then “drag” the object off in a new direction.

To implement this, just handle the Image Sprite’s “Flung” event, like this:

ImageSprite-Flung

 

App Inventor (and the Android OS) handle the details of sensing our finger on the screen and translating that into some values we can use in our app.

When the “Flung” event occurs, App Inventor passes along several values – including the (x,y) coordinate values at the time we touch the object, the speed and heading in which we’ve dragged the object with our finger, and the x and y velocity.  When this event is detected, our program sets the sprite’s current heading and speed to the values determined by our finger drag across the screen.

Click on the “speed” and “heading” parameters in the Flung event to find the “get heading” and “get speed” values.

I multiplied the speed parameter by 3 as the values returned by Flung were small and resulted in slow movement of the sprite. You can set this value to a smaller or larger value based on your preference.

Here’s the app screen, when in use:

Screenshot_2014-10-20-15-53-14

 

As the sprite moves across the screen, touch it with your finger and “fling” it off in a new direction. (You may have to experiment for a bit in terms of how you do your “flings”).

Stopping the Sprite When Touched

If the sprite is moving quickly, it moves out from under your finger before you can take action. To fix this, we sense the finger touching the sprite and stop the sprite while it is being touched.

We implement the “TouchDown” event for ImageSprite1. The moment our finger touches the sprite, the sprite is stopped by setting its Speed property to zero. Now we can casually “fling” the sprite in a new direction.  When we stop touching the sprite, the “TouchUp” event occurs – here we restore the original speed so that the sprite resumes moving. Of course, the “Flung” event occurs next and sets a new speed for the sprite based on our finger movement.

Update: Delete – do not use – the .Touchup block, shown below. It is not needed. Instead, when you lift your finger up as part of a “flung” operation, the flung event handler will set the sprite’s speed proportional to your finger movement. Thus, it is not necessary to restore the saved speed value.

ImageSprite-Touch

What type of App Inventor tutorials do you prefer?

I have created an online poll – see side bar at right of this post – to learn about what type of tutorials you prefer? Please click on your choice —> at right! 

  • Do you like printed or text examples, like these blog posts, to read?
  • Do you like having or using online video tutorials?
  • Would your prefer both?

Help me know what works best for the most readers by answering the simple poll at right – you do not need to log in or go through extra hurdles. Just click on one of the selections.

Thank you!

Using ImageSprites for animated App Inventor graphics

I described the basics of creating a bouncing ball animation earlier and mentioned that “image sprites” are similar to the bouncing ball graphic, but provide a way to specify your own image for the ball. To introduce a bit about images in App Inventor, we took a quick look at using the phone’s camera.

This tutorial starts with the original bouncing ball program and then modifies it to use an Image Sprite instead of a ball. Be sure to read the bouncing ball and camera tutorials first!

Previous:

The ImageSprite component is almost identical to the ball component!

Continue reading Using ImageSprites for animated App Inventor graphics

Using Lists in App Inventor

A frequent search topic on this blog is how to use lists. “Lists” are a way of storing a collection of data in App Inventor programs. Many other programming languages provide a number of methods for storing collections of data including arrays, records, even lists, and even something called “collections” (imagine that!)

App Inventor provides Lists, plus you can also use components like the TinyDB.

For a detailed description of using Lists, please refer to Chapter 8 – Introduction to Lists in my e-book “App Inventor 2: Tutorial”, available for $2.99 from Amazon. I hope to make this text available in additional e-reader formats during November.