Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Package de metodos de gestion de fondos graficos
- // (c)2012-2013 Cèsar Rincón Nadal
- // EMAID - Escola municipal d'art i diseny de Vilanova
- package emaid.classes {
- // Importa los archivos de clases necesarios
- import flash.display.DisplayObject;
- // Clase cInput
- public class cBackground {
- // Contructor de la clase
- public function cBackground() {
- }
- /*
- Variables de la clase
- */
- // Variables para almacenar los fondos
- public static var bgGfx:Array = new Array(); // Almacena el grafico del fondo
- public static var stageWidth:Number, stageHeight:Number; // Tamaño del stage
- public static var mapWidth:Number, mapHeight:Number; // Tamaño del mapa cargado
- /*
- Metodos de la clase
- */
- // Añade una capa al sistema de fondos
- public static function AddLayer(object:DisplayObject) {
- bgGfx.push(object); // Grafico del fondo
- }
- // Elimina una capa del sistema de fondos
- public static function RemoveLayer(object:DisplayObject) {
- // Variables
- var i:Number = 0;
- // Si hay algun fondo registrado...
- if (bgGfx.length > 0) {
- // Busca y elimina del array el fondo seleccionado (buscalo por el nombre)
- for (i = 0; i < bgGfx.length; i ++) {
- // Si el nombre de instancia coincide, eliminalo
- if (bgGfx[i].name == object.name) {
- // Borra el elemento del array
- bgGfx.splice(i, 1);
- // Sal del bucle inmediatamente
- break;
- }
- }
- }
- }
- // Scroll parallax automatico del sistema de fondos
- public static function ScrollBg(posX:Number, posY:Number):void {
- // Variables internas
- var i:Number; // Indice
- var maxWidth:Number, maxHeight:Number; // Posicion maxima del fondo
- var pX:Number, pY:Number; // Posicion actual del fondo
- // Scroll de todos los fondos
- for (i = 0; i < bgGfx.length; i ++) {
- // Calcula la posicion maxima de este fondo
- maxWidth = (bgGfx[i].width - stageWidth);
- maxHeight = (bgGfx[i].height - stageHeight);
- // Calcula la posicion actual del fondo
- pX = ((posX * maxWidth) / (mapWidth - stageWidth));
- pY = ((posY * maxHeight) / (mapHeight - stageHeight));
- // Aplica los limites del decorado
- if (pX < 0) pX = 0;
- if (pX > maxWidth) pX = maxWidth;
- if (pY < 0) pY = 0;
- if (pY > maxHeight) pY = maxHeight;
- // Mueve el fondo a la posicion calculada
- bgGfx[i].x = -pX;
- bgGfx[i].y = -pY;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement