OOPs: Creating a Bobber Class

We will go through all the steps to creating a class.

I would like to create a bobbing action that I can use to animate a fish. I have started with just one fish and created a movie so that I can test some of the settings that I may want to change.

Get Adobe Flash player

Download the demo movie.

This is the code for the demo movie:

var angle:Number=0;
var distance:Number=20;  //how high up and down the y value goes
var origY:Number=fish.y; //where the fish started
var rate:Number=0.5; //how fast it bobs up and down, if 0 there is no bobbing
var speed:Number=5; //x distance fish moves
var turnAround:Boolean=false; //fish can leave and enter on other side or turnaround

btnChange.addEventListener(MouseEvent.CLICK, goChange);

function frames(e:Event):void {
  fish.y = origY + Math.sin(angle) * distance;
  angle = angle + rate;
  fish.x +=  speed;
  if (fish.x > stage.stageWidth + fish.width)
  {//went off on right
    if (turnAround) {
     fish.scaleX *=  -1;//turn around or flip over horizontally
    else {
      fish.x = 0 - fish.width;//reenter on left
  if (fish.x < 0 - fish.width)
  {//went off on left
    if (turnAround) {
     fish.scaleX *=  -1;//turn around
    else {
      fish.x = stage.stageWidth + fish.width;//reenter on right

function goChange(e:MouseEvent):void  {
  distance = parseInt(lblDistance.text);
  rate = parseInt(lblRate.text);
  rate = Number(lblRate.text);
  speed = parseInt(lblSpeed.text);
  turnAround = chkTurnAround.selected;
  if (speed<0)  {
    fish.scaleX = -1;
  else {
    fish.scaleX = 1;

NEXT: One Bobbing Fish