⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asflip.as

📁 as3.0 翻书效果
💻 AS
📖 第 1 页 / 共 2 页
字号:
/**
* 主控制器,单件类;
* @author crlnet
* @extends http://www.iparigrafika.hu/pageflip 
* @version 1.1
*/
package crlclr{
	
	import crlclr.Event.FlipEvent;
	import crlclr.Event.FlippingEventArg;
	import crlclr.Event.FlipResize;
	import crlclr.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class ASFlip extends Sprite {

		private var AM:Number = Math.PI/180;
		private var pageOrder:Array;
		private var pageCanTear:Array;
		private var pageNumber:Array;
		private var removedPages:Array = new Array();
		
		public var pages:Pages;
		private var pw:Number = 240;			
		private var ph:Number = 348;
			
		private var page:int=0;				//first page (normally it is 0 = the page before the cover = blank page)
		private var hcover:Boolean = false;		//hard cover on/off

		private var clickarea:Number = 64;		//pixel width of the click sensitive area at the edges..
		private var afa:Number = 56;			//width of the autoflip starter square.

		private var gs:Number = 2;				//goto page flip speed
		private var ps:Number = 5;				//mouse pursuit speed
		private var es:Number = 3;				//flip speed after mouse btn release

		private var canflip:Boolean = true;		//page flipping enabled

		private var transparency:Boolean = true;//use transparent pages or not (1 level transparency)
		
		
		private var lcover:Boolean = true;		//large cover on/off
		private var lcaddx:Number = 10;		    //width difference
		private var lcaddy:Number =0; //10;		//height difference on top/bottom

		private var SoundOn:Boolean = true;		//use page sounds

		private var mcnt:Number = 0;	        //counter (used on a page where is an animation)
		private var gpage:Number = 0;			//gotoPage No
		private var gflip:Boolean = false;		//gotoPage flip
		private var gdir:Number = 0;			//goto direction...
		private var gskip:Boolean = false;		//skip pages	***
		private var gtarget:Number = 0;		    //target when skipping

		private var aflip:Boolean = false;		//auto flip
		private var flip:Boolean = false;		//pageflip
		private var flipOff:Boolean = false;	//terminateflip
		private var flipOK:Boolean = false;		//good flip
		private var hflip:Boolean = false;		//hardflip (the cover of the book)
		private var rotz = -30;			        //hardflip max y difference

		private var preflip:Boolean = false;	//corner flip status
		private var ctear:Boolean = false;		//actual page status
		private var tear:Boolean = false;
		private var teard:Number = 0;
		private var tlimit:Number = 80;

		private var mpx:Number = 0;
		private var mpy:Number = 0;	            //mousepos at click
		
		private var tox:Number;
		private var toy:Number;
		private var r0:Number;
		private var r1:Number;
		
		private	var cy:Number;
		private var cx:Number;
		private	var ox:Number;
		private var oy:Number;
		private var nx:Number;
		private var ny:Number;
		private var Mx:Number = 0;	//mouse x,y
		private var My:Number = 0;
		private var sx:Number =0;
		private var sy:Number = 0;	//startpoint when flipping
		private var ax:Number = 0;	//auto x,y
		private var ay:Number = 0;
		private var acnt:Number = 0;
		private var aadd:Number = 0;
		
		private var offs:Number;
		
		private var maxpage:Number;
		
		//对像变量;
		var sp2:*;
		var sp3:*;
		var pageN:*;
		var pageO:*;
		var pleft:*;
		var pleftb:*;
		var pright:*;
		var prightb:*;
			
		/**
		* 此类也为一个单件类;
		*/
		public static var Instance:ASFlip=new ASFlip();
		public static function getInstance():ASFlip{
			return ASFlip.Instance;
		}
		public function ASFlip(){
			if(Instance!=null){
				throw new Error("不能创建对像");
			}			
			this.createChildren();
			//他妈的直接用通用的;
			this.pw=siteCommon.Data.PageWidth;
			this.ph=siteCommon.Data.PageHeight;
			//this.pageOrder=Book.getInstance().List;
			this.pageCanTear=Book.getInstance().canTearList;
			this.maxpage=Book.getInstance().Lenght-2;
			
			//把page归o;
			this.page=0;

			this.resetPages();
			this.reset();
		}
		
		private function createChildren():void{
			this.pages=new Pages();
			this.pages.center.visible=false;
			addChild(this.pages);
		}
		
		public function initEvent():void{			
			stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownHander);
			stage.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpHander)
			stage.addEventListener(Event.ENTER_FRAME,this.enterHander);
		}
		
		private function enterHander(event:Event):void{
			this.oef();
		}
		
		//initializing pages... 
		private function reset():void {					
			this.pages.p4.page.x = -this.pw;
			this.pages.p4.x = pw;
			this.pages.p1.page.x = -this.pw;
			this.pages.p1.x = 0;

			this.pages.flip.p2.page.x = -this.pw;
			this.pages.flip.p2.x = pw;
			this.pages.flip.p3.page.x = -this.pw;
			this.pages.flip.p3.x = 0;

			this.pages.p0.page.x = -this.pw;
			this.pages.p0.x = 0;
			this.pages.p5.page.x = -this.pw;
			this.pages.p5.x = pw;
			
			this.pages.pLL.page.x = -this.pw;
			this.pages.pLL.x = 0;
			this.pages.pLR.page.x = -this.pw;
			this.pages.pLR.x = this.pw;

			
			this.pages.pgrad.visible = this.pages.masker.visible = this.pages.flip.visible = false;
			this.pages.flip.p3mask.width = this.pages.pgmask.width = this.pw*2;
			this.pages.flip.p3mask.height = this.pages.pgmask.height = this.ph;
			this.pages.center.height = this.ph+2*this.lcaddy;
			this.pages.flip.fmask.page.pf.width = this.pw;
			this.pages.center.width = 6;
			this.pages.flip.fmask.page.pf.height = this.ph;
			this.pages.masker.height = this.pages.pgrad.height = this.pages.flip.p3shadow.height = this.pages.flip.fgrad.height = 2*Math.sqrt(this.ph*this.ph+this.pw*this.pw);
			
			this.pageNumber = new Array();
			for(var i:int=0;i<=(this.maxpage+1);i++){
				this.pageNumber[i] = i;
			}
			
		}
		
		/**
		* hittest at mouse clicks, if click is over the book -> determining turning direction
		*/
		private function hittest():int {
			var Mx:Number=this.pages.mouseX;
			var My:Number=this.pages.mouseY;
			var pmh = this.ph/2;
			//ha a megadott intervallumban klikkelunk, akkor lapozhatunk
			if(My<=pmh && My>=-pmh && Mx<=pw && Mx>=-pw) {	
				var r:Number = Math.sqrt(Mx*Mx+My*My);
				var a:Number = Math.asin(My/r);
				
				My = Math.tan(a)*pw;
				
				if(My>0 && My>ph/2){
					My = this.ph/2;
				}
				if(My<0 && My<-ph/2){
					My = - this.ph/2;
				}
				this.oy = this.sy = My;
				
				this.r0 = Math.sqrt((this.sy+this.ph/2)*(this.sy+this.ph/2)+this.pw*this.pw);
				this.r1 = Math.sqrt((this.ph/2-this.sy)*(this.ph/2-this.sy)+this.pw*this.pw);
				
				pageN = this.pages.flip.p2.page;
				pageO = this.pages.flip.p3;
				this.offs = -this.pw;
				this.pages.flip.fmask.x = this.pw;
				
				//>-----> flip backward
				if(Mx<-(this.pw-this.clickarea) && this.page>0) {		
					this.pages.flip.p3.x = 0;
					this.hflip = this.checkCover(this.page,-1);
					this.setPages(this.page-2,this.page-1,this.page,this.page+1);
					this.ctear = this.pageCanTear[page-1];
					return -1;
				}
				
				//<-----< flip forward
				if(Mx>(this.pw-this.clickarea) && this.page<this.maxpage) {	
					this.pages.flip.p3.x = this.pw;
					this.hflip = this.checkCover(this.page,1);
					trace(this.page)
					this.setPages(this.page,this.page+2,this.page+1,this.page+3);
					this.ctear = this.pageCanTear[page+2];
					return 1;
				}
			}
			//wrong click
			return 0;	
		}
		
		/**
		* 检查是否是封面,封底.
		* @param	p 当前页
		* @param	dir 翻动方向
		* @return
		*/
		private function checkCover(p:int,dir:int):Boolean {
			if(this.hcover) {
				if(dir>0) {
					if(p==(this.maxpage-2) || p==0) return true;
				} else {
					if(p==this.maxpage || p==2) return true;
				}
			}
			return false;	
		}
		
		
		/**
		* 判断是否在感应区内;
		* @return
		*/
		private function corner():Boolean {
			var Mx:Number = Math.abs(this.pages.mouseX);
			var My:Number = Math.abs(this.pages.mouseY);
			if(Mx>(this.pw-this.afa) && Mx<this.pw && My>(this.ph/2-this.afa) && My<(this.ph/2)) {
				return true;
			}
			return false;
		}

		/**
		* onEnterFrame;
		* @return
		*/
		private function oef():void {
			this.mcnt++;
			
			if(!this.flip && this.corner()) {
				this.preflip = true;
				if(!this.autoflip()) this.preflip = false;
			}
			if(this.preflip && !this.corner()) {
				this.preflip = false;
				this.flip = false;
				this.flipOK = false;
				this.flipOff = true;
			}
			this.getm();
			if(this.aflip && !this.preflip) {
				this.My = (this.ay += (this.sy-this.ay)/(this.gflip? this.gs: this.ps ));
				this.acnt += this.aadd;
				this.ax -= this.aadd;
				if(Math.abs(this.acnt)>this.pw) {
					this.flipOK = true;
					this.flipOff = true;
					this.flip = false;
					this.aflip = false;
				}
			}
			//page turning is in progress...
			if(this.flip) {							
				if(this.tear) {
					this.Mx = this.tox;
					this.My = (this.toy += this.teard);
					this.teard *= 1.2;
					if(Math.abs(this.teard)>1200) {
						this.flipOff = true;
						this.flip = false;
					}
				} else {
					this.Mx = (this.ox += (this.Mx-this.ox)/(this.gflip? this.gs: this.ps ));
					this.My = (this.oy += (this.My-this.oy)/(this.gflip? this.gs: this.ps ));
				}
				//positioning pages and shadows
				this.calc(this.Mx,this.My);						
			}
			/*terminating page turning effect... 
			(comlplete turning... dropped on the other side)*/
			if(this.flipOff) {
				if(this.flipOK || this.tear) {
					this.Mx = (this.ox += (-this.sx-this.ox)/(this.gflip? this.gs: this.es ));
					this.My = (this.oy += (this.sy-this.oy)/(this.gflip? this.gs: this.es ));
					this.calc(this.Mx,this.My);
					
					//we are done with turning, so stop all turning issue...
					if(this.Mx/-this.sx > 0.99 || this.tear) {			
						this.flip = false;
						this.flipOK = this.flipOff = false;
						this.pages.pgrad.visible = this.pages.flip.visible = false;
						
						//if tear: remove page!!!
						if(this.tear) {
							this.removePage((this.sx<0)? this.page: this.page+1);
							this.page += (this.sx<0)? -2: 0;
						} else {
							//and tourning pages at pagenumber level...
							this.page += (this.sx<0)? -2: 2;	
						}
						if(this.gskip){
							this.page = this.gtarget;
						}
						
						this.setPages(this.page,0,0,this.page+1);
						this.tear = false;
						
						if(this.gpage>0 && !this.gskip) {
							this.gpage--;
							this.autoflip();
						} else {
							this.gflip = this.gskip = false;
						}
					}
				} else {
					this.Mx = (this.ox += (this.sx-this.ox)/3);
					this.My = (this.oy += (this.sy-this.oy)/3);
					this.calc(this.Mx,this.My);
					if(this.Mx/this.sx > 0.99) {
						this.flip = false;
						this.flipOff = false;
						this.aflip = false;
						this.pages.pgrad.visible = this.pages.flip.visible = false;
						this.setPages(this.page,0,0,this.page+1);	
					}
				}
			}
		}
		
		
		private function calc(Mx:Number,My:Number):void {
			//hardflip...
			if(hflip) {	
				var xp:Number = (this.sx<0)? -Mx: Mx;
				if(xp>0) {
					this.sp2._visible = false;
					this.sp3._visible = true;
					this.scalc(this.sp3,Mx);
				} else {
					this.sp3._visible = false;
					this.sp2._visible = true;
					this.scalc(this.sp2,Mx);
				}
				this.pages.flip.mask=null;
				this.pages.flip.visible = true;
				this.pages.flip.fgrad.visible = false;
				this.pages.flip.p2.visible =this.pages.flip.p3.visible = false;
				return;
			} else {
				this.pages.flip.fgrad.visible = true;
			}
			
			var a:Number;
			var ad:Number
			//normal flipping process;
			var rr0:Number = Math.sqrt((My+this.ph/2)*(My+this.ph/2)+Mx*Mx);
			var rr1:Number = Math.sqrt((this.ph/2-My)*(this.ph/2-My)+Mx*Mx);
			
			if((rr0>this.r0 || rr1>this.r1) && !this.tear) {
				if(My<this.sy) {
					a= Math.asin((this.ph/2-My)/rr1);
					My = (this.ph/2-Math.sin(a)*r1);
					Mx = (Mx<0)? -Math.cos(a)*this.r1: Math.cos(a)*this.r1;
					if(My>sy) {
						if((this.sx*Mx)>0){ 
							My = this.sy;
							Mx = this.sx;
						}else{ 
							My = this.sy;
							Mx = -this.sx;
						}
					}
					if((rr1-this.r1)>this.tlimit && this.ctear) {
						this.teard = -5;
						this.tear = true;
						this.tox = this.ox = Mx;
						this.toy = this.oy = My;
					}
				} else {	
					// k0-val kell osszehasonlitani!
					a= Math.asin((My+this.ph/2)/rr0);
					My = Math.sin(a)*this.r0-this.ph/2;
					Mx = (Mx<0)? -Math.cos(a)*this.r0: Math.cos(a)*this.r0;
					if(My<this.sy) {
						if((this.sx*Mx)>0) {
							My = this.sy;
							Mx = this.sx;
						}else{
							My = this.sy;
							Mx = -this.sx;
						}
					}
					if((rr0-this.r0)>this.tlimit && this.ctear) {
						this.teard = 5;
						this.tear = true;
						this.tox = this.ox = Mx;
						this.toy = this.oy = My;
					}
				}
			}
			if((this.sx<0 && (Mx-this.sx)<10) || (this.sx>0 && (this.sx-Mx)<10)) {
				if(this.sx<0) {
					Mx = -this.pw+10;
				}
				if(this.sx>0){
					Mx = this.pw-10;
				}
			} 
			
			//calculating flipping process
			this.pages.flip.visible = true;
			this.pages.flip.p3shadow.visible = this.pages.pgrad.visible = !this.tear;
			this.pages.flip.p2.visible = this.pages.flip.p3.visible = true;
			
			//equation of the line
			var vx:Number = Mx-this.sx;
			var vy:Number = My-this.sy;
			var a1:Number = vy/vx;
			var a2:Number = -vy/vx;
			
			this.cx = this.sx+(vx/2);
			this.cy = this.sy+(vy/2);
			
			var r:Number = Math.sqrt((this.sx-Mx)*(this.sx-Mx)+(this.sy-My)*(this.sy-My));
			a= Math.asin((this.sy-My)/r);
			if(sx<0){
				a = -a;	
			}
			ad = a/this.AM;
			this.pageN.rotation = ad*2;
			//r = Math.sqrt((this.sx-Mx)*(this.sx-Mx)+(this.sy-My)*(this.sy-My));
			var rl:Number = (this.pw*2);
			if(sx>0) {
				this.pages.masker.scaleX = 1.0;
				this.nx = this.cx-Math.tan(a)*(this.ph/2-this.cy);
				this.ny = this.ph/2;
				if(this.nx>this.pw) {
					this.nx = this.pw;
					this.ny = this.cy+Math.tan(Math.PI/2+a)*(this.pw-this.cx);
				}
				this.pageN.pf.x = -(this.pw-this.nx);
				this.pages.flip.fgrad.scaleX = (r/rl/200)*this.pw;
				this.pages.pgrad.scaleX = -(r/rl/200)*this.pw;
				this.pages.flip.p3shadow.scaleX = (r/rl/200)*this.pw;
			} else { 
				this.pages.masker.scaleX = -1.0;
				this.nx = this.cx-Math.tan(a)*(this.ph/2-this.cy);
				this.ny = this.ph/2;
				if(this.nx<-this.pw) {
					this.nx = -this.pw;
					this.ny = this.cy+Math.tan(Math.PI/2+a)*(-this.pw-this.cx);
				}
				
				this.pageN.pf.x = -(this.pw-(this.pw+this.nx));
				this.pages.flip.fgrad.scaleX = -(r/rl/200)*this.pw;
				this.pages.pgrad.scaleX = (r/rl/200)*this.pw;
				this.pages.flip.p3shadow.scaleX = -(r/rl/200)*this.pw;
			}
			this.pages.masker.x = this.cx;
			this.pages.masker.y = this.cy;
			this.pages.masker.rotation = ad;
			this.pageN.pf.y = -ny;
			this.pageN.x = nx+offs;
			this.pageN.y = ny;
			
			this.pages.flip.fgrad.x = cx;
			this.pages.flip.fgrad.y = cy;
			this.pages.flip.fgrad.rotation = ad;
			this.pages.flip.fgrad.alpha = (r>(rl-50))? (100-(r-(rl-50))*2)/100: 1.0;
			
			this.pages.flip.p3shadow.x = cx;
			this.pages.flip.p3shadow.y = cy;
			this.pages.flip.p3shadow.rotation = ad;
			this.pages.flip.p3shadow.alpha = (r>(rl-50))? (100-(r-(rl-50))*2)/100: 1.0;
			
			this.pages.pgrad.x = cx;
			this.pages.pgrad.y = cy;
			this.pages.pgrad.rotation = ad+180;
			this.pages.pgrad.alpha = (r>(rl-100))? (100-(r-(rl-100)))/100: 1.0;
			
			this.pages.flip.fmask.page.x = this.pageN.x;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -