Skip to content

Eight Ball

Zach Kysar edited this page Mar 21, 2016 · 5 revisions

Overview

In this lesson we will be using a canvas component to draw text on an image background to emulate a magic eight ball. We will trigger these changes via sensors built into the phone.

New Concepts

Lists:

Add these block components to your code to create a list of variables. Lists

Random:

Given a list of elements we can select a random element with a simple call to this list procedure. random

Sensors:

Sensors are devices that are built into smartphones that detect certain important information about the phone. These sensors include motion detection sensors like the gyroscope and accelerometer.

sensors

In our case we will be working with the accelerometer which allows us to check when the device is being shook. shaking

Canvas:

canvas A canvas is basically a big white board. You can draw many things on a canvas And it will all stay on the screen until we clear the screen. Here are some examples of useful procedures in canvas

Let's Get Started:

User Interface Designer:

We will need a canvas with a background image (see the media and assets section). We will also add an accelerometer sensor here. ui

Block Logic Editor:

Using the tools provided:

  1. Create a list and fill it with text copies of each response you’re eight ball will give
  2. Draw text in the center of the canvas when the screen is loaded using the 'when screen initialize procedure' giving the user instructions to shake the phone.
  3. Using your sensor, change the text in the center to a random response selected from your list when the device is shook.

Media and Assets

  1. 8_ball.png

External References:

  1. Java Arrays
  2. Java Random
  3. Android Sensors
  4. Canvas

Clone this wiki locally