Advertisement
NightFox

AS3 Parallax Scroll management

Mar 8th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Package de metodos de gestion de fondos graficos
  2. // (c)2012-2013 Cèsar Rincón Nadal
  3. // EMAID - Escola municipal d'art i diseny de Vilanova
  4.  
  5. package emaid.classes {
  6.    
  7.     // Importa los archivos de clases necesarios
  8.     import flash.display.DisplayObject;
  9.    
  10.     // Clase cInput
  11.     public class cBackground {
  12.  
  13.         // Contructor de la clase
  14.         public function cBackground() {
  15.         }
  16.        
  17.  
  18.         /*
  19.             Variables de la clase
  20.         */
  21.  
  22.         // Variables para almacenar los fondos
  23.         public static var bgGfx:Array = new Array();                // Almacena el grafico del fondo
  24.         public static var stageWidth:Number, stageHeight:Number;    // Tamaño del stage
  25.         public static var mapWidth:Number, mapHeight:Number;        // Tamaño del mapa cargado
  26.        
  27.        
  28.         /*
  29.             Metodos de la clase
  30.         */
  31.        
  32.         // Añade una capa al sistema de fondos
  33.         public static function AddLayer(object:DisplayObject) {
  34.             bgGfx.push(object);     // Grafico del fondo
  35.         }
  36.        
  37.         // Elimina una capa del sistema de fondos
  38.         public static function RemoveLayer(object:DisplayObject) {
  39.            
  40.             // Variables
  41.             var i:Number = 0;
  42.            
  43.             // Si hay algun fondo registrado...
  44.             if (bgGfx.length > 0) {
  45.                 // Busca y elimina del array el fondo seleccionado (buscalo por el nombre)
  46.                 for (i = 0; i < bgGfx.length; i ++) {
  47.                     // Si el nombre de instancia coincide, eliminalo
  48.                     if (bgGfx[i].name == object.name) {
  49.                         // Borra el elemento del array
  50.                         bgGfx.splice(i, 1);
  51.                         // Sal del bucle inmediatamente
  52.                         break;
  53.                     }
  54.                 }
  55.             }
  56.            
  57.         }
  58.        
  59.        
  60.         // Scroll parallax automatico del sistema de fondos
  61.         public static function ScrollBg(posX:Number, posY:Number):void {
  62.            
  63.             // Variables internas
  64.             var i:Number;                               // Indice
  65.             var maxWidth:Number, maxHeight:Number;      // Posicion maxima del fondo
  66.             var pX:Number, pY:Number;                   // Posicion actual del fondo
  67.            
  68.             // Scroll de todos los fondos
  69.             for (i = 0; i < bgGfx.length; i ++) {
  70.                 // Calcula la posicion maxima de este fondo
  71.                 maxWidth = (bgGfx[i].width - stageWidth);
  72.                 maxHeight = (bgGfx[i].height - stageHeight);
  73.                 // Calcula la posicion actual del fondo
  74.                 pX = ((posX * maxWidth) / (mapWidth - stageWidth));
  75.                 pY = ((posY * maxHeight) / (mapHeight - stageHeight));
  76.                 // Aplica los limites del decorado
  77.                 if (pX < 0) pX = 0;
  78.                 if (pX > maxWidth) pX = maxWidth;
  79.                 if (pY < 0) pY = 0;
  80.                 if (pY > maxHeight) pY = maxHeight;
  81.                 // Mueve el fondo a la posicion calculada
  82.                 bgGfx[i].x = -pX;
  83.                 bgGfx[i].y = -pY;
  84.             }
  85.            
  86.         }
  87.        
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement