Changing Game Physics in Bullseye

Author: Matt Slaybaugh
Date: October 25, 2006
Level: Beginner

Summary

Bullseye is a game in which the player uses the mouse to aim a bow and fire an arrow. In this tutorial, you will adjust the arithmetic used to determine the wind speed.

Experience Needed
  • Basic knowledge of Flash
Software
  • Flash 8

Files — Archery.zip (1.3m)


  • myglife_archery.fla
Description
Change how the wind speed is calculated.

The Bullseye game relies on 'virtual physics' to control the movement of the arrows, specifically gravity and wind speed.

The movement of the arrows is determined by a mixture of simple arithmetic, complex trigonometry, and random numbers.

You can ignore the more complex math for now. We will focus on a single line of code:

this.wind = Math.round(Math.random()*56)-28;

In the code above, this.wind is a variable holding the value of the wind speed.

The part to the right of the equals sign begins with Math.round, which simply tells Flash that whatever is inside the parentheses should be rounded off to the nearest integer. (eg. rounding the number '9.135' would give you '9')
Flash includes many built-in functions in its library. The mathematical ones are in a library named 'Math', so all built-in math functions are referred to with 'Math.[name of function]'

Inside the parentheses is Math.random which generates a random number between 0 and 1 (eg. 0.6574839201).
This is then multiplied by 56, meaning the number is now a random number between 0 and 56.
Finally, 28 is subtracted from the number, giving a random number between -28 and +28

This is the full range of the possible wind speeds the player will encounter when playing in 'compete' mode. (-28 is blowing hard to the left, 28 is blowing hard to the right, and 0 is no wind)
The game can be very challenging with the wind blowing so much, but we can make it easier by reducing the amount of wind.


The code movie clip contains most of the scripting in this game.
  1. Open myglife_archery.fla in Flash.
  2. Look in the Library (Windows menu -> Library) and locate the movie clip named code. Double-click it to open.
  3. Expand the Actions palette (Windows menu -> Actions) and scroll down to line 24, which should look like code above.
  4. Change the code so that instead of a wind speed range of -28 to 28, the range is only -10 to 10. The code should look like this:

    this.wind = Math.round(Math.random()*20)-10;

  5. Save your changes, publish the application, and open myglife_archery.swf in a browser and play in 'compete' mode. You should see that the wind blows much less and has less effect on where your arrows land.
  6. Try replacing the two numbers with other numbers and see what happens.