Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iMackshun.Games.Pixelformer.Objects;
- public class ScrollingBackground {
- //Object Properties
- private int BgX;
- private int BgY;
- private int Width;
- private int Height;
- private int SpeedX;
- private int SpeedY;
- private int XToLoop;
- private int YToLoop;
- private String Direction;
- //Constructor, Every instance must have these properties.
- public ScrollingBackground(int x, int y, int width, int height, int speedx, int speedy, String dir){
- x = BgX;
- y = BgY;
- width = Width;
- height = Height;
- speedx = SpeedX;
- speedy = SpeedY;
- dir = Direction;
- }
- //Update method. Called every frame. Maybe in the render method?
- public void update(){
- //Scroll the Background.
- BgX += SpeedX;
- BgY += SpeedY;
- //I'm assuming I could use a switch here... But this should work just the same.
- if (Direction.equals("Left")){
- XToLoop = BgX - Width;
- LoopLeftScroll();
- }
- if (Direction.equals("Right")){
- XToLoop = BgX + Width;
- LoopRightScroll();
- }
- if (Direction.equals("Up")){
- YToLoop = BgY + Height;
- LoopUpScroll();
- }
- if (Direction.equals("Down")){
- YToLoop = BgY - Height;
- LoopDownScroll();
- }
- }
- //Reset the Background,assuming it is moving up.
- private void LoopUpScroll() {
- if (BgY >= YToLoop){
- BgY -= Height;
- }
- }
- //Reset the Background,assuming it is moving down.
- private void LoopDownScroll() {
- if (BgY <= YToLoop){
- BgY += Height;
- }
- }
- //Reset the Background,assuming it is moving to the left.
- private void LoopLeftScroll() {
- if (BgX <= XToLoop){
- BgX += Height;
- }
- }
- //Reset the Background,assuming it is moving to the right.
- private void LoopRightScroll() {
- if (BgX >= XToLoop){
- BgX -= Height;
- }
- }
- //Getters and Setters
- public int getBgX() {
- return BgX;
- }
- public int getBgY() {
- return BgY;
- }
- public int getWidth() {
- return Width;
- }
- public int getHeight() {
- return Height;
- }
- public int getSpeedX() {
- return SpeedX;
- }
- public int getSpeedY() {
- return SpeedY;
- }
- public int getXToLoop() {
- return XToLoop;
- }
- public int getYToLoop() {
- return YToLoop;
- }
- public String getDirection() {
- return Direction;
- }
- public void setBgX(int bgX) {
- BgX = bgX;
- }
- public void setBgY(int bgY) {
- BgY = bgY;
- }
- public void setWidth(int width) {
- Width = width;
- }
- public void setHeight(int height) {
- Height = height;
- }
- public void setSpeedX(int speedX) {
- SpeedX = speedX;
- }
- public void setSpeedY(int speedY) {
- SpeedY = speedY;
- }
- public void setXToLoop(int xToLoop) {
- XToLoop = xToLoop;
- }
- public void setYToLoop(int yToLoop) {
- YToLoop = yToLoop;
- }
- public void setDirection(String direction) {
- Direction = direction;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment