Monday, January 25, 2010

the sky book

sky book is a flash animation that i make three year ago(in 2006 summer, maybe). it is quite unbelievable when i remind that period. sky book, could be say, is the most challenging thing i have done. but, definitely, i must can be improved. therefore, i post the code here and hope someone can help me to modify it.

here is the code
var tUpBlank:Number = 0;
var tLeftBlank:Number = 0;
var tWindowHeight:Number = 600;
var tWindowWidth:Number = 1000;
var tMode:Number = 0;// indicate if there is action for mousedown
var tMidUpPoint_x:Number = tLeftBlank+tWindowWidth/2;
var tMidUpPoint_y:Number = tUpBlank;
var tMidBtomPoint_x:Number = tLeftBlank+tWindowWidth/2;
var tMidBtomPoint_y:Number = tUpBlank+tWindowHeight;
var tLine:Number = Math.sqrt(tWindowHeight*tWindowHeight+(tWindowWidth/2)*(tWindowWidth/2))/2;
L5 = L6=L8=L9=1;
var used:Number = 0;// if it is currently fliping. 1:flip left page; -1:flip right page
var i:Number = 0;
var endOf:Boolean = false;// indicate if the mouse is still holding
var tPage:Number = 0;// the current page 
var tMaxPage:Number = 6;// total page
var auto:Boolean = false;// indicate if it is automatically fliping
attachMovie("page"+tPage,"page",1,{_x:tLeftBlank, _y:tUpBlank});

attachMovie("maskShadow","maskShadow",3);
attachMovie("layerShadow","layerShadow1",4);
layerShadow1.setMask(maskShadow);
maskShadow._x = tLeftBlank;
maskShadow._y = tUpBlank;
maskShadow._width = tWindowWidth;
maskShadow._height = tWindowHeight;
layerShadow1._visible = false;
prevbtn._alpha = 0;
prevbtn.ta = 0;
nextbtn._alpha = 100;
nextbtn.ta = 100;
mouseControl = new Object();


mouseControl.onMouseDown = function() {
 if (tMode == 1 && used == 0) {
  if (_xmouse < tLeftBlank+tWindowWidth/5 && _xmouse > tLeftBlank) {
   s1x = tLeftBlank;
   ox = tLeftBlank+tWindowWidth/4;
   used = 1;
   k = tPage-1;
   attachMovie("page"+k+"b","page1",5);
   attachMovie("page"+k+"a","page2",2);
  } else if (_xmouse < tLeftBlank+tWindowWidth && _xmouse > tLeftBlank+4*tWindowWidth/5) {
   s1x = tLeftBlank+tWindowWidth;
   ox = tLeftBlank+3*tWindowWidth/4;
   used = -1;
   k = tPage+1;
   attachMovie("page"+k+"a","page1",5);
   attachMovie("page"+k+"b","page2",2);
  }
  layerShadow1._visible = true;
  page1._x = tMidUpPoint_x-used*3*tWindowWidth/4;
  page1._y = tUpBlank+tWindowHeight/2;
  page2._x = tMidUpPoint_x-used*tWindowWidth/4;
  page2._y = tUpBlank+tWindowHeight/2;
  i = 0;
  s1y = _ymouse;
  s2tx = _xmouse;
  s2ty = s1y;
  s2x = s1x;
  s2y = s1y;
  oy = tUpBlank+tWindowHeight/2;
  h1 = tUpBlank+tWindowHeight/2-s1y;
  h7 = (s1y-tMidUpPoint_y)*(s1y-tMidUpPoint_y)+(s1x-tMidUpPoint_x)*(s1x-tMidUpPoint_x);
  h8 = (s1y-tMidBtomPoint_y)*(s1y-tMidBtomPoint_y)+(s1x-tMidBtomPoint_x)*(s1x-tMidBtomPoint_x);
 }
};
nextbtn.onPress = function() {
 flipPage(tPage+1);
};
prevbtn.onPress = function() {
 flipPage(tPage-1);
};
function flipPage(n:Number) {
 if (n != tPage && n <= tMaxPage && used == 0) {
  //trace("tPage:"+tPage+" n:"+n);
  if (n>tPage) {
   tPage = n-1;
   s1x = tLeftBlank+tWindowWidth;
   ox = tLeftBlank+3*tWindowWidth/4;
   used = -1;
   k = tPage+1;
   s2tx = tLeftBlank+20;
   attachMovie("page"+k+"a","page1",5);
   attachMovie("page"+k+"b","page2",2);
  } else {
   tPage = n+1;
   s1x = tLeftBlank;
   ox = tLeftBlank+tWindowWidth/4;
   used = 1;
   k = tPage-1;
   s2tx = tLeftBlank+tWindowWidth+20;
   attachMovie("page"+k+"b","page1",5);
   attachMovie("page"+k+"a","page2",2);
  }
  layerShadow1._visible = true;
  page1._x = tMidUpPoint_x-used*3*tWindowWidth/4;
  page1._y = tUpBlank+tWindowHeight/2;
  page2._x = tMidUpPoint_x-used*tWindowWidth/4;
  page2._y = tUpBlank+tWindowHeight/2;
  auto = true;
  endOf = false;
  i = 0;
  s1y = tUpBlank;
  s2ty = -80;
  s2x = s1x;
  s2y = s1y;
  oy = tUpBlank+tWindowHeight/2;
  h1 = tUpBlank+tWindowHeight/2-s1y;
  h7 = (s1y-tMidUpPoint_y)*(s1y-tMidUpPoint_y)+(s1x-tMidUpPoint_x)*(s1x-tMidUpPoint_x);
  h8 = (s1y-tMidBtomPoint_y)*(s1y-tMidBtomPoint_y)+(s1x-tMidBtomPoint_x)*(s1x-tMidBtomPoint_x);
  return true;
 }
 return false;
}

mouseControl.onMouseUp = function() {
 if (used != 0 && endOf == false && !auto) {
  if (_xmouse*used > used*(tMidUpPoint_x+used*tWindowWidth/4)) {
   s2tx = tMidUpPoint_x+used*tWindowWidth/2;
  } else {
   s2tx = s1x+used*3;
  }
  s2ty = s1y;
  s3x = s1x;
  s3y = s1y;
  endOf = true;
 }
};
Mouse.addListener(mouseControl);
onEnterFrame = function () {
 mc = page1;
 mc1 = page2;

 if (((tPage >0 && _xmouse< tLeftBlank+tWindowWidth/5 && _xmouse >tLeftBlank) || (tPage< tMaxPage && _xmouse< tLeftBlank+tWindowWidth && _xmouse >tLeftBlank+4*tWindowWidth/5)) && _ymouse >tUpBlank && _ymouse< tUpBlank+tWindowHeight) {
  tMode = 1;
 } else {
  tMode = 0;
 }
 //trace("tMode:"+tMode+" used:"+used+" endOf:"+endOf+" auto:"+auto);
 if (endOf == false && used != 0 && (_xmouse*used < used*(tMidUpPoint_x-used*tWindowWidth/2) && !auto)) {
  s2tx = s1x+used*3;
  s2ty = s1y;
  endOf = true;
 }
 if (endOf == true && !auto && Math.abs(mc._y-(tUpBlank+tWindowHeight/2))<1 && Math.abs(mc._x-(tMidUpPoint_x+used*tWindowWidth/4)) < 5) {
  tPage = tPage-used;
  layerShadow1._visible = false;
  used = 0;
  endOf = false;
  s2tx = s1x;
  s2ty = s1y;
  attachMovie("page"+tPage,"page",1,{_x:tLeftBlank, _y:tUpBlank});
  //---------------------------------------------------------------
  /*this.createEmptyMovieClip("page",1);
  page.loadMovie("./pages/page"+tPage+".swf");
  page._x = tLeftBlank;
  page._y = tUpBlank;
  //---------------------------------------------------------------*/
  mc.removeMovieClip();
  mc1.removeMovieClip();
  _parent.ReturnPage(tPage);
  if (tPage<=0) {
   prevbtn.ta = 0;
  } else {
   prevbtn.ta = 100;
  }
  if (tPage>=tMaxPage) {
   nextbtn.ta = 0;
  } else {
   nextbtn.ta = 100;
  }
 } else if (endOf == true && !auto && Math.abs(mc._y-(tUpBlank+tWindowHeight/2)) < 8 && Math.abs(mc._x-(tMidUpPoint_x-used*3*tWindowWidth/4)) < 8) {
  mc._visible = false;
  layerShadow1._visible = false;
  used = 0;
  endOf = false;
  s2tx = s1x;
  s2ty = s1y;
  mc.removeMovieClip();
  mc1.removeMovieClip();
 }
 if (used != 0 && endOf == false && !auto) {
  limit(mc._x,mc._y);
 }
 if (used != 0) {
  //setXY(s2x,s2tx,s2y,s2ty,6);
  //trace("s2tx:"+s2tx+" s2x:"+s2x+" auto:"+auto);
  if (auto && !endOf && Math.abs(s2tx-s2x) < 300) {
   //trace("used:"+used);
   if (used == -1) {
    s2tx = tLeftBlank;
   } else {
    s2tx = tLeftBlank+tWindowWidth;
   }
   auto = false;
   s2ty = tUpBlank;
   endOf = true;
  } else if (Math.abs(s2tx-s2x)>1 || Math.abs(s2ty-s2y)>1) {
   s2y = s2y-(s2y-s2ty)/6;
   s2x = s2x-(s2x-s2tx)/6;
  } else {
   s2y = s2ty;
   s2x = s2tx;
  }
  controlPage(s2x,s2y);
  mc._x = px;
  mc._y = py;
  mc._rotation = pr;
  setShadow(mc,mc1);
 }
 setA(nextbtn,6);
 setA(prevbtn,6);
};
function controlPage(xx, yy) {
 h2 = Math.sqrt((s1y-yy)*(s1y-yy)+(s1x-xx)*(s1x-xx));
 L1 = Math.atan((yy-s1y)/(xx-s1x));
 s3x = s1x+used*h2/(2*Math.cos(L1));
 s3y = s1y;
 if (ox < s3x) {
  L2 = Math.atan((oy-s3y)/(ox-s3x))+Math.PI;
 } else {
  L2 = Math.atan((oy-s3y)/(ox-s3x));
 }
 if (xx < s3x) {
  L3 = Math.atan((yy-s3y)/(xx-s3x));
 } else {
  if (yy > s3y) {
   L3 = Math.atan((yy-s3y)/(xx-s3x))-Math.PI;
  } else {
   L3 = Math.atan((yy-s3y)/(xx-s3x))+Math.PI;
  }
 }
 L4 = L2-L3-Math.PI/2;
 h3 = Math.sqrt((s3y-oy)*(s3y-oy)+(s3x-ox)*(s3x-ox));
 px = s3x-used*h3*Math.sin(L4);
 py = s3y-used*h3*Math.cos(L4);
 pr = L3*180/Math.PI+90+used*90;
}
function limit(xx, yy) {
 if ((s2ty-tMidUpPoint_y)*(s2ty-tMidUpPoint_y)+(s2tx-tMidUpPoint_x)*(s2tx-tMidUpPoint_x)>=h7 && (s2ty-tMidBtomPoint_y)*(s2ty-tMidBtomPoint_y)+(s2tx-tMidBtomPoint_x)*(s2tx-tMidBtomPoint_x)>=h8) {
  s2tx = tMidUpPoint_x+used*tWindowWidth/2;
  s2ty = s1y;
  s3x = s1x;
  s3y = s1y;
  endOf = true;
 } else if ((_ymouse-tMidUpPoint_y)*(_ymouse-tMidUpPoint_y)+(_xmouse-tMidUpPoint_x)*(_xmouse-tMidUpPoint_x)>h7 && i != 2) {
  i = 1;
  h5 = Math.sqrt((tMidUpPoint_y-s1y)*(tMidUpPoint_y-s1y)+(tMidUpPoint_x-s1x)*(tMidUpPoint_x-s1x));
  if (_xmouse<=tMidUpPoint_x) {
   L7 = -Math.atan((tMidUpPoint_y-_ymouse)/(tMidUpPoint_x-_xmouse));
  } else {
   L7 = Math.PI-Math.atan((tMidUpPoint_y-_ymouse)/(tMidUpPoint_x-_xmouse));
  }
  s2tx = tMidUpPoint_x-h5*Math.cos(L7);
  s2ty = tMidUpPoint_y+h5*Math.sin(L7);
 } else if ((_ymouse-tMidBtomPoint_y)*(_ymouse-tMidBtomPoint_y)+(_xmouse-tMidBtomPoint_x)*(_xmouse-tMidBtomPoint_x)>h8 && i != 1) {
  i = 2;
  h6 = Math.sqrt((tMidBtomPoint_y-s1y)*(tMidBtomPoint_y-s1y)+(tMidBtomPoint_x-s1x)*(tMidBtomPoint_x-s1x));
  if (_xmouse<=tMidUpPoint_x) {
   L10 = -Math.PI/2-Math.atan((tMidBtomPoint_y-_ymouse)/(tMidBtomPoint_x-_xmouse));
  } else {
   L10 = -3*Math.PI/2-Math.atan((tMidBtomPoint_y-_ymouse)/(tMidBtomPoint_x-_xmouse));
  }
  s2tx = tMidBtomPoint_x+h6*Math.sin(L10);
  s2ty = tMidBtomPoint_y+h6*Math.cos(L10);
 } else {
  i = 0;
  s2tx = _xmouse;
  s2ty = _ymouse;
 }
}
function setShadow(obj1, obj2) {
 layerShadow1._x = (obj1._x+ox)/2;
 layerShadow1._y = (obj1._y+oy)/2;
 layerShadow1._rotation = obj1._rotation/2+90+used*90;
 obj1.setMask(layerShadow1.layerMask1);
 obj2.setMask(layerShadow1.layerMask2);
}
function setXY(ox, tx, oy, ty, a) {
 if (tx != ox || ty != oy) {
  oy = oy-(oy-ty)/a;
  ox = ox-(ox-tx)/a;
 }
}
function setR(obj, a) {
 if (Math.abs(obj.tr-obj._rotation)>20) {
  obj._rotation = obj._rotation-(obj._rotation-obj.tr*2)/a;
 } else {
  obj._rotation = obj.tr;
 }
}
function setA(obj, a) {
 if (Math.abs(obj.ta-obj._alpha)>10) {
  obj._alpha = obj._alpha-(obj._alpha-obj.ta)/a;
 } else {
  obj._alpha = obj.ta;
 }
}

this is how it looks like

No comments:

Post a Comment