OOPs: Creating a Bobber Class

I will just explain the code for the bobbing fish.

Get Adobe Flash player

This is the code for the fish movie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//variables
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

//initialize
this.addEventListener(Event.ENTER_FRAME,frames);

//functions;
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
      speed=speed*-1;
    }//turnAround
    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
      speed=speed*-1;
   }
   else {
      fish.x = stage.stageWidth + fish.width;//reenter on right
   }
  }
}//frames
CODE

NEXT: Creating the Bobbing Class