Solved

Java tile map

Posted on 2011-03-02
1
695 Views
Last Modified: 2012-05-11
I am creating Road Rash in a Java midlet. I am trying to create the track using tiles which I lay out. I have followed an example I have. But when I run the program it just gives me a blank screen or doesn't run at all.  Could you guys take a look at my code to see if you can see anything wrong with it as I am stuck. Ill post the code and the tiles I am using below.

 Track
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class RoadRashCanvas extends GameCanvas implements Runnable {
  private Display  display;
  private boolean  sleeping;
  private long     frameDelay;
  private Random   rand;
  private Sprite   ufoSprite;
  private Sprite   background;
  private boolean  gameover;
  private LayerManager Layers;
  private TiledLayer RoadLayer;
  private int[]    RoadTile = { 1, 3 };
  private Player   Musicplayer;
  private int      ufoXSpeed, ufoYSpeed;
  int      Distance = 0;
  int      Speed = 5;
  int      Level = 1;

  int[] left = new int[200] ;
  int[] right = new int[200] ;

 // private Sprite[] roidSprite = new Sprite[3];
  private Player   tonePlayer;

  public RoadRashCanvas(Display d) {
    super(true);
    display = d;
    
    // Set the frame rate (30 fps)
    frameDelay = 33;
  }

  public void start() {
    // Set the canvas as the current screen
    display.setCurrent(this);

    // Initialize the random number generator
    rand = new Random();

    // Initialize the UFO and roids sprites
    ufoXSpeed = ufoYSpeed = 0;
    try {
      RoadLayer = new TiledLayer(9, 9, Image.createImage("/Track.png"), 9, 9);
      ufoSprite = new Sprite(Image.createImage("/Saucer.png"));
      background = new Sprite(Image.createImage("/background.png"));
      ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
        (getHeight() - ufoSprite.getHeight()) / 2);

      Image img = Image.createImage("/Roid.png");

      for (int i =  0; i < 200 ; i++) { left[i] = 100 ; right[i] = 300 ; }
      //roidSprite[0] = new Sprite(img, 42, 35);
      //roidSprite[1] = new Sprite(img, 42, 35);
     // roidSprite[2] = new Sprite(img, 42, 35);
    }
    catch (IOException e) {
      System.err.println("Failed loading images!");
    }

    int[] RoadMap = {
           0, 0, 0, 1, 1, 1, 0, 0, 0,
           0, 0, 0, 1, 1, 1, 0, 0, 0,
           0, 0, 0, 1, 1, 1, 0, 0, 0,
           0, 0, 0, 1, 1, 1, 0, 0, 0,
           0, 0, 0, 1, 1, 1, 0, 0, 0,
           0, 0, 0, 1, 1, 1, 0, 0, 0,


    };

    for (int i = 0; i < RoadMap.length; i++) {
      int column = i % 24;
      int row = (i - column) / 24;
      RoadLayer.setCell(column, row, RoadMap[i]);
    }


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

    Layers.append(RoadLayer);
  


    // Initialize the tone tune and play it once
    initTune();
    playTune();

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

  void nextFrame(int moveLines) {

    for (int i = 0 ; i < 200-moveLines ; i++) { left[i] = left[i+moveLines] ; right[i] = right[i+moveLines] ; }
    // Now fill in new values for left[200-movesLines] to left[200], these define the new road appearing
   // over the horizon.
}

  public void stop() {
    // Clean up the tone tune
    cleanupTune();

    // Stop the animation
    sleeping = true;
  }

  void drawFrame() {
   for (int i = 0 ; i < 200 ; i++) {
       // Draw the road
       int y = 400-i ;
      // drawLine(left[i],y, right[i],y) ;
   }
}

  public void run() {
    Graphics g = getGraphics();

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

  private void update() {
    // Randomly play the "encounters" tone tune
    if (rand.nextInt() % 500 == 0)
      playTune();

    // Process user input to control the UFO speed
    byte G4 = (byte)(ToneControl.C4 + 7);
    int keyState = getKeyStates();
   // ufoSprite.setPosition(0,0);
    if ((keyState & LEFT_PRESSED) != 0) {
      // Play a sound to indicate movement
      try {
       // Manager.playTone(G4, 100, 50);
        ufoSprite.move(-2, 0);
      }
      catch (Exception e) {
      }

       
    }
    else if ((keyState & RIGHT_PRESSED) != 0) {
      // Play a sound to indicate movement
      try {
        //Manager.playTone(G4, 100, 50);
        ufoSprite.move(2,0);
      }
      catch (Exception e) {
      }


    }
    if ((keyState & UP_PRESSED) != 0) {
      // Play a sound to indicate movement
      try {
      //  Manager.playTone(G4, 100, 50);
        Distance += 1;
       // ufoSprite.move(0,2);
        if(Speed < 150)
        {
            Speed += 5;
        }
      }
      catch (Exception e) {
      }

      //ufoYSpeed--;
    }
    else if ((keyState & DOWN_PRESSED) != 0) {
      // Play a sound to indicate movement
      try {
      //  Manager.playTone(G4, 100, 50);

       //  ufoSprite.move(0,-2);
         if (Speed > 1)
         {
             Speed -=5;

         }
      }
      catch (Exception e) {
      }


    }






    ufoXSpeed = Math.min(Math.max(ufoXSpeed, -8), 8);
    ufoYSpeed = Math.min(Math.max(ufoYSpeed, -8), 8);

    // Move the UFO sprite
    ufoSprite.move(ufoXSpeed, ufoYSpeed);
    }

  private void draw(Graphics g) {
    // Clear the display
    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());
    
    // Draw the UFO sprit
    background.paint(g);
    ufoSprite.paint(g);
   // Layers.paint(g,0,0);
   
    g.setColor(0xffffff);
    g.drawString("Speed mph" + Speed, 20, this.getHeight(), Graphics.BOTTOM|Graphics.LEFT);
    g.drawString("Level" + Level, 20, this.getWidth(), Graphics.TOP|Graphics.HCENTER);
   // NewGame();
    for (int i = 0 ; i < 200 ; i++) {
       // Draw the road
       int y = 400-i ;
       //int drawLine = 0;
     // int drawLine(left[i],y, right[i],y) ;

    if (Distance == 150)
    {

        Level += 1;
        Distance -= 150;
    }

    if (Level == 2)
    {

    }

    if (Level == 3)
    {

    }
    // Draw the roid sprites
  //  for (int i = 0; i < 3; i++)
     // roidSprite[i].paint(g);

    // Flush the offscreen graphics buffer
    flushGraphics();
  }
    }
  private void checkBounds(Sprite sprite) {
    // Wrap the sprite around the screen if necessary
    if (sprite.getX() < -sprite.getWidth())
      sprite.setPosition(getWidth(), sprite.getY());
    else if (sprite.getX() > getWidth())
      sprite.setPosition(-sprite.getWidth(), sprite.getY());
    if (sprite.getY() < -sprite.getHeight())
      sprite.setPosition(sprite.getX(), getHeight());
    else if (sprite.getY() > getHeight())
      sprite.setPosition(sprite.getX(), -sprite.getHeight());
  }

  private void initTune() {
    byte tempo = 30; // 120bpm
    byte d4 = 16;    // 1/4 note
    byte d2 = 32;    // 1/2 note

    byte C4 = ToneControl.C4;
    byte A6 = (byte)(C4 + 21);
    byte B6 = (byte)(C4 + 23);
    byte G5 = (byte)(C4 + 19);
    byte G4 = (byte)(C4 + 7);
    byte D5 = (byte)(C4 + 14);
    byte rest = ToneControl.SILENCE;

    byte[] encountersSequence = {
      ToneControl.VERSION, 1,
      ToneControl.TEMPO, tempo,
      ToneControl.BLOCK_START, 0,
      A6,d4, B6,d4, G5,d4, G4,d4, D5,d2, rest,d2,
      ToneControl.BLOCK_END, 0,
      ToneControl.PLAY_BLOCK, 0,
      ToneControl.PLAY_BLOCK, 0,
    };

    try {
      // Create the tone player
      tonePlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
      tonePlayer.realize();

      // Create the tone control and set the tone sequence
      ToneControl toneControl = (ToneControl)tonePlayer.getControl("ToneControl");
      toneControl.setSequence(encountersSequence);
    }
    catch (IOException ioe) {
    }
    catch (MediaException me) {
    }
  }

  private void NewGame() {

      // Layers.setViewWindow(10, 10, 10, 30);

  }

  private void playTune() {
    try {
      // Play the tone sequence
      tonePlayer.start();
    }
    catch (MediaException me) {
    }
  }

  private void cleanupTune() {
    // Close the tone player
    tonePlayer.close();
  }

    
}

Open in new window

0
Comment
Question by:tango2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
satsumo earned 500 total points
ID: 35026293
I like those tiles, though I'm not sure they would join up very neatly.  I don't know much about Java, however I can see all the drawLine functions in your code are commented out and those are what draws the road.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question