Guest User

Scrolling Example

a guest
Aug 9th, 2011
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         /**
  2.          * Switch into scroll mode to scroll to another room.
  3.          * @param   dir the direction in which to scroll.
  4.          */
  5.         private function startScroll(dir:int):void
  6.         {
  7.             scrollDirection = dir;
  8.             currMode = MODE_SCROLL;
  9.             FlxG.followTarget = null;
  10.             player.active = false;
  11.             currRoom.freeze();
  12.         }
  13.        
  14.         /**
  15.          * Records if we are set up to align the viewport for scrolling.
  16.          */
  17.         private var alignmentSetUp:Boolean = false;
  18.        
  19.         private function updateScroll():void
  20.         {
  21.             if (!alignmentSetUp)
  22.             {
  23.                 setupAlignment();
  24.             }
  25.             else if (nextRoom == null || nextRoom.dead)
  26.             {
  27.                 if (!isAlignedForScroll()) stepAlignScroll();
  28.                 else setupScroll();
  29.             }
  30.             else if (!isScrollingDone())
  31.             {
  32.                 stepScroll();
  33.             }
  34.             else //Done scrolling.
  35.             {
  36.                 finishScroll();
  37.             }
  38.         }
  39.        
  40.         /**
  41.          * Sets up the scroll destination for the alignment step of scrolling. The screen should be aligned to the integer map position containing the player.
  42.          */
  43.         private function setupAlignment():void
  44.         {
  45.             var mapPosition:Point = WorldMap.currWorld.currMapPosition;
  46.            
  47.             scrollDestination.x = -currRoom.x + (currRoom.room.x - mapPosition.x) * FlxG.width;
  48.             scrollDestination.y = -currRoom.y + (currRoom.room.y - mapPosition.y) * FlxG.height;
  49.            
  50.             alignmentSetUp = true;
  51.         }
  52.        
  53.         /**
  54.          * Checks to see if the current scroll position is ready to scroll from the current room to the next.
  55.          * @return true if the scroll position is aligned; false otherwise.
  56.          */
  57.         private function isAlignedForScroll():Boolean
  58.         {
  59.             return scrollDestination.x == FlxG.scroll.x && scrollDestination.y == FlxG.scroll.y;
  60.         }
  61.        
  62.         /**
  63.          * Move the screen one step to align it properly for room scrolling.
  64.          */
  65.         private function stepAlignScroll():void
  66.         {
  67.             var deltax:Number = scrollDestination.x - FlxG.scroll.x;
  68.             var deltay:Number = scrollDestination.y - FlxG.scroll.y;
  69.            
  70.             if (deltax != 0)
  71.             {
  72.                 if (deltax > 0)
  73.                 {
  74.                     FlxG.scroll.x += ALIGN_SCROLL_SPEED * FlxG.elapsed;
  75.                     if (FlxG.scroll.x > scrollDestination.x) FlxG.scroll.x = scrollDestination.x;
  76.                 }
  77.                 else
  78.                 {
  79.                     FlxG.scroll.x -= ALIGN_SCROLL_SPEED * FlxG.elapsed;
  80.                     if (FlxG.scroll.x < scrollDestination.x) FlxG.scroll.x = scrollDestination.x;
  81.                 }
  82.             }
  83.            
  84.             if (deltay != 0)
  85.             {
  86.                 if (deltay > 0)
  87.                 {
  88.                     FlxG.scroll.y += ALIGN_SCROLL_SPEED * FlxG.elapsed;
  89.                     if (FlxG.scroll.y > scrollDestination.y) FlxG.scroll.y = scrollDestination.y;
  90.                 }
  91.                 else
  92.                 {
  93.                     FlxG.scroll.y -= ALIGN_SCROLL_SPEED * FlxG.elapsed;
  94.                     if (FlxG.scroll.y < scrollDestination.y) FlxG.scroll.y = scrollDestination.y;
  95.                 }
  96.             }
  97.         }
  98.        
  99.         private function setupScroll():void
  100.         {
  101.             //Change our current map position to that of the next room.
  102.             switch(scrollDirection)
  103.             {
  104.                 case FlxSprite.LEFT:
  105.                     WorldMap.currWorld.currMapPosition.x--;
  106.                     break;
  107.                 case FlxSprite.RIGHT:
  108.                     WorldMap.currWorld.currMapPosition.x++;
  109.                     break;
  110.                 case FlxSprite.UP:
  111.                     WorldMap.currWorld.currMapPosition.y--;
  112.                     break;
  113.                 case FlxSprite.DOWN:
  114.                     WorldMap.currWorld.currMapPosition.y++;
  115.                     break;
  116.             }
  117.            
  118.             nextRoom.loadRoom(WorldMap.currWorld.getRoomAt(WorldMap.currWorld.currMapPosition.x, WorldMap.currWorld.currMapPosition.y));
  119.             nextRoom.initializeRoom();
  120.            
  121.             var newX:int;
  122.             var newY:int;
  123.            
  124.             //We need to pick a position for the next room and set the goal that we want to scroll to.
  125.             switch(scrollDirection)
  126.             {
  127.                 case FlxSprite.LEFT:
  128.                     newX = currRoom.x - nextRoom.width;
  129.                     newY = currRoom.y + (nextRoom.room.y - currRoom.room.y) * FlxG.height;
  130.                     scrollDestination.x = FlxG.scroll.x + FlxG.width;
  131.                     scrollDestination.y = FlxG.scroll.y;
  132.                     break;
  133.                 case FlxSprite.RIGHT:
  134.                     newX = currRoom.x + currRoom.width;
  135.                     newY = currRoom.y + (nextRoom.room.y - currRoom.room.y) * FlxG.height;
  136.                     scrollDestination.x = FlxG.scroll.x - FlxG.width;
  137.                     scrollDestination.y = FlxG.scroll.y;
  138.                     break;
  139.                 case FlxSprite.UP:
  140.                     newY = currRoom.y - nextRoom.height;
  141.                     newX = currRoom.x + (nextRoom.room.x - currRoom.room.x) * FlxG.width;
  142.                     scrollDestination.x = FlxG.scroll.x;
  143.                     scrollDestination.y = FlxG.scroll.y + FlxG.height;
  144.                     break;
  145.                 case FlxSprite.DOWN:
  146.                     newY = currRoom.y + currRoom.height;
  147.                     newX = currRoom.x + (nextRoom.room.x - currRoom.room.x) * FlxG.width;
  148.                     scrollDestination.x = FlxG.scroll.x;
  149.                     scrollDestination.y = FlxG.scroll.y - FlxG.height;
  150.                     break;
  151.             }
  152.            
  153.             //Using reset() ensures that members of nextRoom are also moved.
  154.             nextRoom.reset(newX, newY);
  155.         }
  156.        
  157.         /**
  158.          * Scroll the screen one step toward the next room.
  159.          */
  160.         private function stepScroll():void
  161.         {
  162.             //We'll never be scrolling diagonally, so we can shortcut the vector difference formula.
  163.             var scrollProgress:Number = 1 - (Math.abs(scrollDestination.x - FlxG.scroll.x) / FlxG.width + Math.abs(scrollDestination.y - FlxG.scroll.y) / FlxG.height);
  164.            
  165.             //Scroll the screen and move the player across the room border.
  166.             switch(scrollDirection)
  167.             {
  168.                 case FlxSprite.LEFT:
  169.                     FlxG.scroll.x += SCROLL_SPEED * FlxG.elapsed;
  170.                     if (FlxG.scroll.x > scrollDestination.x) FlxG.scroll.x = scrollDestination.x;
  171.                     player.x = currRoom.x - scrollProgress * (player.width + 1);
  172.                     break;
  173.                 case FlxSprite.RIGHT:
  174.                     FlxG.scroll.x -= SCROLL_SPEED * FlxG.elapsed;
  175.                     if (FlxG.scroll.x < scrollDestination.x) FlxG.scroll.x = scrollDestination.x;
  176.                     player.x = currRoom.x + currRoom.width + (scrollProgress - 1) * (player.width + 1);
  177.                     break;
  178.                 case FlxSprite.UP:
  179.                     FlxG.scroll.y += SCROLL_SPEED * FlxG.elapsed;
  180.                     if (FlxG.scroll.y > scrollDestination.y) FlxG.scroll.y = scrollDestination.y;
  181.                     player.y = currRoom.y - scrollProgress * (player.height + 1);
  182.                     break;
  183.                 case FlxSprite.DOWN:
  184.                     FlxG.scroll.y -= SCROLL_SPEED * FlxG.elapsed;
  185.                     if (FlxG.scroll.y < scrollDestination.y) FlxG.scroll.y = scrollDestination.y;
  186.                     player.y = currRoom.y + currRoom.height + (scrollProgress - 1) * (player.height + 1);
  187.                     break;
  188.             }
  189.         }
  190.        
  191.         private function finishScroll():void
  192.         {
  193.             FlxG.scroll.x = scrollDestination.x;
  194.             FlxG.scroll.y = scrollDestination.y;
  195.            
  196.             if (nextRoom.room.sector.music != Music.currMusic)
  197.             {
  198.                 if(nextRoom.bossPresent) Music.currMusic = Music.MusicQuiet; //Bosses have their own music.
  199.                 else Music.currMusic = nextRoom.room.sector.music;
  200.             }
  201.            
  202.             //Exchange the old room for the new room and discard the old room's state.
  203.             var r:RoomView = currRoom;
  204.             currRoom.kill();
  205.             currRoom = nextRoom;
  206.             nextRoom = r;
  207.            
  208.             currRoom.awaken();
  209.            
  210.             player.changeRoom();
  211.             player.active = true;
  212.            
  213.             redrawMap();
  214.            
  215.             adjustFollow();
  216.            
  217.             currMode = MODE_PLAY;
  218.            
  219.             alignmentSetUp = false;
  220.         }
  221.        
  222.         /**
  223.          * Checks to see if scrolling from one room to the next is done.  Note: the opposite of FlxG.scroll is equal to the position of the upper-left corner of the viewport.
  224.          * @return true if scrolling is done; false otherwise.
  225.          */
  226.         private function isScrollingDone():Boolean
  227.         {
  228.             return Math.abs(scrollDestination.x - FlxG.scroll.x) < 1 && Math.abs(scrollDestination.y - FlxG.scroll.y) < 1;
  229.         }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×