package com.illuzor.display { | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.Sprite; | |
import flash.events.TimerEvent; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import flash.utils.Timer; | |
/** | |
* Класс для создания ротаторов (preloaders.net) | |
* Класс получает битмапдату и показывает её отдельные части через определённые заданные времени | |
* | |
* @author illuzor | |
*/ | |
public class Rotator extends Sprite { | |
private var totalElements:uint; | |
private var counter:uint; | |
private var rectangle:Rectangle; | |
private var bitmap:Bitmap; | |
private var bitmapData:BitmapData; | |
private var elementWidth:uint; | |
private var elementHeight:uint; | |
private var timer:Timer; | |
/** | |
* @param bitmapData битмапдата прелоадера | |
* @param rectangle прямоугольник, равный размеру одного элемента | |
* @param autoplay начать автоматическое воспроизведение | |
* @param interval интервал смены кадров в миллисекундах | |
*/ | |
public function Rotator(bitmapData:BitmapData, rectangle:Rectangle, autoplay:Boolean = true, interval:uint = 35) { | |
this.bitmapData = bitmapData; | |
this.rectangle = rectangle; | |
totalElements = bitmapData.width / rectangle.width; | |
elementWidth = rectangle.width; | |
elementHeight = rectangle.height; | |
bitmap = new Bitmap(); | |
bitmap.smoothing = true; | |
addChild(bitmap); | |
timer = new Timer(interval); | |
timer.addEventListener(TimerEvent.TIMER, onUpdate); | |
onUpdate(); | |
if (autoplay) play(); | |
} | |
public function play():void { | |
timer.start(); | |
} | |
public function pause():void { | |
timer.stop(); | |
} | |
private function onUpdate(e:TimerEvent = null):void { | |
if (counter < totalElements - 1) counter++; | |
else counter = 0; | |
rectangle.x = elementWidth * counter; | |
var bdata:BitmapData = new BitmapData(elementWidth, elementHeight); | |
bdata.copyPixels(bitmapData, rectangle, new Point()); | |
bitmap.bitmapData = bdata; | |
bitmap.smoothing = true; | |
} | |
public function dispose():void { | |
timer.stop(); | |
timer.removeEventListener(TimerEvent.TIMER, onUpdate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment