NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Help making a scroller with tile layers

 
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users
View previous topic :: View next topic  
Author Message
Ultra



Joined: 20 Mar 2011
Posts: 1
Location: Game Programming/Playing

PostPosted: Sun Mar 20, 2011 7:00 pm    Post subject: Help making a scroller with tile layers Reply with quote

Hello,

I need help producing a scroll-er that uses a velocity to scroll up through the tiles that I have implemented into a layer. But I keep getting black after a short time and I have added loads of tiles onto the layer and doesn't seem to get any longer through gametime.

Below is the code that I produced.
Code:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
/**
*
* @author p08292350
*/
public class RRCanvas extends GameCanvas implements Runnable {
  private Random        rand;
  private Display       display;
  private boolean       sleeping;
  private long          frameDelay;
  private Image         infoBar;
  private LayerManager  layers;
  private TiledLayer    backgroundLayer;
  private Player        player = null;
  private int           Width;
  private int           Height;
  private int           backWidth;
  private int           velocity;
  private int           xView,yView;
  ImageFunction imgFun = new ImageFunction();
  Image img = null;
  Image img2 = null;

  public RRCanvas(Display d) {
    super(true);
    display = d;
    Width = getWidth();
    Height = getHeight();
    // Initialize the random number generator
    rand = new Random();

    // Set the frame rate (30 fps)
    frameDelay = 33;
  }

  public void start() {
    // Set the canvas as the current screen
    display.setCurrent(this);
   
    backWidth = (int)(getWidth()/6)+1;
    try {
      img = Image.createImage("/infoImage.png");
      infoBar = imgFun.rescaleImage(img, Width, 20);
      img = Image.createImage("/tileset.png");
      backgroundLayer = new TiledLayer(12, 64, img, 32, 32);
      player = new Player(Image.createImage("/bikeImage.png"),0,0.0f);
    }
    catch (IOException e) {
      System.err.println("Failed loading images!");
    }
int[] layerMap = {
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
      5,  6,  7,  8,  1,  2,  3,  4,  8,  7,  6,  5,
    };
    for (int i = 0; i < layerMap.length; i++) {
      int column = i % 12;
      int row = (i - column) / 64;
      backgroundLayer.setCell(column, row, layerMap[i]);
    }
    backgroundLayer.setPosition((getWidth() - backgroundLayer.getWidth()) / 2,
      (getWidth() - backgroundLayer.getWidth()) / 2);


    // Create the layer manager
    layers = new LayerManager();

    layers.append(player);
    layers.append(backgroundLayer);


    NewGame();
    // Start the animation thread
    sleeping = false;
    Thread t = new Thread(this);
    t.start();
  }

  public void stop() {

    // Stop the animation
    sleeping = true;
  }

  public void run() {
    Graphics g = getGraphics();
    // The main game loop
    while (!sleeping) {
        update();
        draw(g);
      try {
        Thread.sleep(frameDelay);
      }
      catch (InterruptedException ie) {}
    }
  }

  public void update()
  {
         // Process user input to move the water layer and animate the player
    int keyState = getKeyStates();
    int xMove = 0;
    if ((keyState & LEFT_PRESSED) != 0) {
      xMove = -4;
    }
    else if ((keyState & RIGHT_PRESSED) != 0) {
      xMove = 4;
    }
    if ((keyState & UP_PRESSED) != 0) {
      velocity += 0.1;
    }
//    else if ((keyState & DOWN_PRESSED) != 0) {
//      velocity = 1;
//    }

      layers.setViewWindow(xView, player.getY()-player.getHeight(), getWidth(),
        getHeight() - infoBar.getHeight());
     
     // backgroundLayer.move(xView, -velocity);
      player.move(xMove, -velocity);

//    // Check for a collision with the player and the land tiled layer
//    if (playerSprite.collidesWith(landLayer, true)) {
//      // Restore the original view window and player sprite positions
//      layers.setViewWindow(xView, yView, getWidth(),
//        getHeight() - infoBar.getHeight());
//      playerSprite.move(-xMove, -yMove);
//    }
//    else {
//      // If there is no collision, commit the changes to the view window position
//      xView += xMove;
//      yView += yMove;
//    }
        velocity += 0.2;

  }

  private void draw(Graphics g) {
    g.setColor(0,0,0);
    g.fillRect(0, 0, Width, Height);
   
    g.drawImage(infoBar, 0, 0, Graphics.TOP | Graphics.LEFT);

    layers.paint(g, 0, infoBar.getHeight());
   
    // Flush the offscreen graphics buffer
    flushGraphics();
  }

  private void NewGame()
    {
      velocity = 2;
      player.setVisible(true);

      player.setPosition(getWidth()/2, player.getHeight());
      xView = 0;
      yView = player.getY() - ((getHeight() - player.getHeight()) / 2);
      layers.setViewWindow(xView, yView, getWidth(), getHeight() - infoBar.getHeight());
  }
}


Please help me....
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo