iMackshun

ScrollingBackground.java

Feb 13th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package com.iMackshun.Games.Pixelformer.Objects;
  2.  
  3. public class ScrollingBackground {
  4.    
  5.     //Object Properties
  6.     private int BgX;
  7.     private int BgY;
  8.     private int Width;
  9.     private int Height;
  10.     private int SpeedX;
  11.     private int SpeedY;
  12.     private int XToLoop;
  13.     private int YToLoop;
  14.     private String Direction;
  15.    
  16.     //Constructor, Every instance must have these properties.
  17.     public ScrollingBackground(int x, int y, int width, int height, int speedx, int speedy, String dir){
  18.         x = BgX;
  19.         y = BgY;
  20.         width = Width;
  21.         height = Height;
  22.         speedx = SpeedX;
  23.         speedy = SpeedY;
  24.         dir = Direction;
  25.     }
  26.     //Update method. Called every frame. Maybe in the render method?
  27.     public void update(){
  28.         //Scroll the Background.
  29.         BgX += SpeedX;
  30.         BgY += SpeedY;
  31.         //I'm assuming I could use a switch here... But this should work just the same.
  32.         if (Direction.equals("Left")){
  33.             XToLoop = BgX - Width;
  34.             LoopLeftScroll();
  35.         }
  36.         if (Direction.equals("Right")){
  37.             XToLoop = BgX + Width;
  38.             LoopRightScroll();
  39.         }
  40.         if (Direction.equals("Up")){
  41.             YToLoop = BgY + Height;
  42.             LoopUpScroll();
  43.         }
  44.         if (Direction.equals("Down")){
  45.             YToLoop = BgY - Height;
  46.             LoopDownScroll();
  47.         }
  48.     }
  49.     //Reset the Background,assuming it is moving up.
  50.     private void LoopUpScroll() {
  51.         if (BgY >= YToLoop){
  52.             BgY -= Height;
  53.         }
  54.     }
  55.     //Reset the Background,assuming it is moving down.
  56.     private void LoopDownScroll() {
  57.         if (BgY <= YToLoop){
  58.             BgY += Height;
  59.         }
  60.     }
  61.     //Reset the Background,assuming it is moving to the left.
  62.     private void LoopLeftScroll() {
  63.         if (BgX <= XToLoop){
  64.             BgX += Height;
  65.         }
  66.     }
  67.     //Reset the Background,assuming it is moving to the right.
  68.     private void LoopRightScroll() {
  69.         if (BgX >= XToLoop){
  70.             BgX -= Height;
  71.         }
  72.     }
  73.     //Getters and Setters
  74.     public int getBgX() {
  75.         return BgX;
  76.     }
  77.     public int getBgY() {
  78.         return BgY;
  79.     }
  80.     public int getWidth() {
  81.         return Width;
  82.     }
  83.     public int getHeight() {
  84.         return Height;
  85.     }
  86.     public int getSpeedX() {
  87.         return SpeedX;
  88.     }
  89.     public int getSpeedY() {
  90.         return SpeedY;
  91.     }
  92.     public int getXToLoop() {
  93.         return XToLoop;
  94.     }
  95.     public int getYToLoop() {
  96.         return YToLoop;
  97.     }
  98.     public String getDirection() {
  99.         return Direction;
  100.     }
  101.     public void setBgX(int bgX) {
  102.         BgX = bgX;
  103.     }
  104.     public void setBgY(int bgY) {
  105.         BgY = bgY;
  106.     }
  107.     public void setWidth(int width) {
  108.         Width = width;
  109.     }
  110.     public void setHeight(int height) {
  111.         Height = height;
  112.     }
  113.     public void setSpeedX(int speedX) {
  114.         SpeedX = speedX;
  115.     }
  116.     public void setSpeedY(int speedY) {
  117.         SpeedY = speedY;
  118.     }
  119.     public void setXToLoop(int xToLoop) {
  120.         XToLoop = xToLoop;
  121.     }
  122.     public void setYToLoop(int yToLoop) {
  123.         YToLoop = yToLoop;
  124.     }
  125.     public void setDirection(String direction) {
  126.         Direction = direction;
  127.     }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment