An interesting scroll background------ActionScript3.0

package 
{
/*
*@ ClassName : package::backGround
*@ INTRO : the continuously scroll background
*@ Author : feiruo
*@ LanguageVersion : 3.0
*@ SDKVersion : Flash Player 9.0
*@ Date : 2015.09.09
*/

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

dynamic public class backGround extends MovieClip //dynamic表示可以向该类进行动态的添加属性
{
public var myAcc:Number=20; //缓动系数
public var mouseAcc:Number=10; //鼠标缓动系数
public var autoPlay_speed:Number=3; //自动滚动速度初始值
public var mouseOverPlay_speed:Number=-3; //鼠标经过时背景播放的速度,正数向右,负数向左
private var autoPlay:Boolean=false;
private var mouseF:Number=0;
private var mouseover:Boolean=false;
private var isDrag:Boolean;
private var speed:Number=0;
private var startX2:Number=0;
private var startX:Number=0;
private var memory_x:Number=0;
private var _width:Number;

public function backGround()
{
addFrameScript(0, frame1); //在影片剪辑backGround元件中时间轴第一帧添加代码
this._width=this.width/3;
return;

}
public function frame1():void
{
isDrag=false;
speed=5; //设定初始滚动速度
addEventListener(Event.ENTER_FRAME, update);//添加帧事件
this.addEventListener(MouseEvent.MOUSE_OVER,function(e:MouseEvent):void{mouseover=true;});
this.addEventListener(MouseEvent.MOUSE_OUT,function(e:MouseEvent):void{mouseover=false;});
if (autoPlay == false) //如果没有自动滚动,添加鼠标事件
{
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
}
return;
}//end function 
private function mouseUpdate()
{
startX2 = startX2 + (mouseX - startX - startX2) / 3;
//计算鼠标位置差值
bg_mc.x = memory_x + startX2;
//计算当前背景图应该所在的新位置
if (bg_mc.x > 0)
{
bg_mc.x = bg_mc.x - this._width;
//返回到第一张图或第二张图之间,所显示画面与当前的位置其实一样
}
if (bg_mc.x <= -this._width)
{
bg_mc.x = bg_mc.x + this._width;
//返回到第二张图或第三张图之间
}
return;
}//end function

private function mouseHandler(e:MouseEvent):void
{
if (e.type == MouseEvent.MOUSE_DOWN)
{
memory_x = bg_mc.x; //临时存储当前背景图的x坐标
startX = mouseX; //鼠标按下,临时存储鼠标x坐标
startX2 = mouseX - startX; //按下移动后,计算移动距离
mouseF = mouseX; //记录当前鼠标位置
isDrag = true; //拖动属性设置为true,可以拖动
mouseUpdate();
}
else if (e.type == MouseEvent.MOUSE_UP)
{
if (isDrag)
{
speed = (-(mouseF - mouseX)) / mouseAcc; //计算鼠标弹起是背景图应该滚动的速度
isDrag = false; //拖动为false
}
}
return;

}//end function

private function update(e:Event):void
{
mouseover ? bg_mc.x+=mouseOverPlay_speed : bg_mc.x+=0;

if (autoPlay == false) //如果背景图没有自己滚动,并且速度不为0,计算当前位置
{
if (Math.abs(speed) >= 0)
{
bg_mc.x = bg_mc.x + speed;
}
}
else
{
bg_mc.x = bg_mc.x + autoPlay_speed; //否则自动滚动
}
if (bg_mc.x > 0)
{
bg_mc.x = -this._width + speed;
}
if (bg_mc.x <= -this._width)
{
bg_mc.x = speed;
}

mouseF = mouseF + (mouseX - mouseF) / mouseAcc;
//计算非拖动与拖动情况下的速度

if (!isDrag)
{
speed = speed + (-speed) / myAcc;
}
else
{
speed = speed + (-speed) / myAcc;
mouseUpdate();
}
if (Math.abs(speed) < 0.5) 
//如果速度绝对值小于0.5,可以默认为0
{
speed = 0;
}
return;
}
}

}

the background download link 

你可能感兴趣的:(An interesting scroll background------ActionScript3.0)