//MOON FLies
//Marlon Barrios Solano
// associate a function with the enterFrame event
this.onEnterFrame = updateFrame;
// flies follow the mouse in different ways
circle2.onRollOver = activaterest;
circle2.onRollOut = activaterebound;
circle2.onMouseDown = activatechange;
circle2.onMouseUp = activateback;
//
//
//
//
//
// associate a function with the enterFrame event
Mouse.hide(); f//hide cursor
//
// state variables
//
// for bouncing
var state = "rebound";
var xDir = 1;
var yDir = 1;
var maxSpeed = 20;
var speed = new Array(newSpeed(maxSpeed), newSpeed(maxSpeed));
var radius = circle._width*.5;
var tRest = 0;
var tInc = .08;
// some initial variables
var time = 0;

//
////
function updateFrame() {
circle3._x = this._xmouse+50;
circle3._y = this._ymouse+50;
circle4._x = this._xmouse+Math.random()*200;
circle4._y = this._ymouse+Math.random()*100;
circle5._x = this._xmouse+Math.random()*100;
circle5._y = this._ymouse+Math.random()*200;

////STATES for interaction


switch (state) {
case "rebound" :
rebound();
break;
case "rest" :
rest();
break;
case "change" :
change();
break;
case "back" :
back();
break;
}
}
///funtions to activate states

function activaterest() {
state = "rest";
}
//
function activaterebound() {
state = "rebound";
}
function activatechange() {
state = "change";
}
function activateback() {
state = "back";
}

//
//
///behavior functions
//bouncy moon
function rebound() {
circle2._x += speed[0]*xDir;
circle2._y += speed[1]*yDir;
if (circle2._x<radius) {
circle2._x = radius;
xDir *= -1;
speed[0] = newSpeed(maxSpeed);
} else if (circle2._x>Stage.width-radius) {
circle2._x = Stage.width-radius;
xDir *= -1;
speed[0] = newSpeed(maxSpeed);
}
if (circle2._y<radius) {
circle2._y = radius;
yDir *= -1;
speed[1] = newSpeed(maxSpeed);
} else if (circle2._y>Stage.height-radius) {
circle2._y = Stage.height-radius;
yDir *= -1;
speed[1] = newSpeed(maxSpeed);
}
fly3._alpha=0;

}
//stopo moon bacuse flies
function rest() {
//
circle2._xscale = rangedSin(tRest, 6, 0, 80, 105);
//
fly3._alpha=0;
fly3._alpha=0;
}
///fly face apears when mause down
function change() {
fly3._alpha = 80;

fly3._yscale = Math.random() * 500 ;
fly3._xscale = 100 ;

}
//
//fly face disappears mouse up
function back() {
fly3._alpha= 0;

}
//helper functions

function newSpeed(tops) {
var base = tops*.4;
var extra = tops*.6;
return (Math.random()*extra+base);
}
//
// ranged sine returns a sine-driven value within the range given
function rangedSin(t, f, p, lo, hi) {
// t = time (varying value, should range [0,2PI])
// f = frequency (waves per time, >= 1.0)
// p = phase shift (affects timing of wave, should range [0,2PI])
// lo = lower bound of arbitrary range
// hi = upper bound of arbitrary range
var a = (hi-lo)*.5;
var mid = lo+a;
return mid+(a*Math.sin(f*t+p));
}
//