Created
December 25, 2014 21:51
-
-
Save illuzor/8afa7d63aef4b814ed4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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