Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Tile map game  (Read 2659 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2007-06-09 00:02:33 »

Hi, i'm just starting to try game development using java
I already know the basic stuff like the collections, arrays, objects, classes, simple gui forms
I started gathering all the information i can get my hands on and started trying out easy coding examples. What i'm trying to create is a tilemap with predefined pictures, turn based, keybord input, 2 players.

I'm trying to use MIDP 2.0 because it includes a Game API and a layerManager wich i think is just what i need.
So i tried following this tutorial at http://developers.sun.com/techtopics/mobility/midp/articles/game/

But it gives an error when i try to run it.

I did the basic game loop with a rotating X  that can be controlled by the keybord...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class SimpleGameCanvas
    extends GameCanvas
    implements Runnable {
  private volatile boolean mTrucking;
  private long mFrameDelay;
 
  private int mX, mY;
  private int mState;
 
  public SimpleGameCanvas() {
    super(true);
    mX = getWidth() / 2;
    mY = getHeight() / 2;
    mState = 0;
    mFrameDelay = 20;
  }
 
  public void start() {
    mTrucking = true;
    Thread t = new Thread(this);
    t.start();
  }
 
  public void stop() { mTrucking = false; }
 
  public void run() {
    Graphics g = getGraphics();
   
    while (mTrucking == true) {
      tick();
      input();
      render(g);
      try { Thread.sleep(mFrameDelay); }
      catch (InterruptedException ie) { stop(); }
    }
  }
 
  private void tick() {
    mState = (mState + 1) % 20;
  }
 
  private void input() {
    int keyStates = getKeyStates();
    if ((keyStates & LEFT_PRESSED) != 0)
      mX = Math.max(0, mX - 1);
    if ((keyStates & RIGHT_PRESSED) != 0)
      mX = Math.min(getWidth(), mX + 1);
    if ((keyStates & UP_PRESSED) != 0)
      mY = Math.max(0, mY - 1);
    if ((keyStates & DOWN_PRESSED) != 0)
      mY = Math.min(getHeight(), mY + 1);
  }
 
  private void render(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
   
    g.setColor(0x0000ff);
    g.drawLine(mX, mY, mX - 10 + mState, mY - 10);
    g.drawLine(mX, mY, mX + 10, mY - 10 + mState);
    g.drawLine(mX, mY, mX + 10 - mState, mY + 10);
    g.drawLine(mX, mY, mX - 10, mY + 10 - mState);
   
    flushGraphics();
  }
}


I transferred the classes to the java dir and added them into the program.
But it gives an error at g.setColor(0xffffff);
g is null hence the java.lang.nullpointer exeption... how can a example be wrong, or is this only made for cellphones?
If i'm realy wrong what classes should i use then to create a tile map, letting 2d pictures move around it and turn based?
Offline keldon85

Senior Member


Medals: 1



« Reply #1 - Posted 2007-06-09 01:59:39 »

getGraphics() is not the way! Change render to the paint method of the Canvas and instead call repaint(). This will cause a call to Canvas.update(g), clearing the canvas with the background colour, which then calls Canvas.paint(g). If you're wondering where it gets g from, well that would be the Graphics g associated with the current draw of the frame.

Offline stef569

Junior Member





« Reply #2 - Posted 2007-06-09 14:41:42 »

I don't understand what you just said, the example given on the website iswrong?
I'm having a hard time finding out what the methods do. But i'll tell you what i did.

First i removed the line Graphics g = getGraphics(); because i shouldn't use it for some reason.
Then the name of the function render should be changed to paint
and i should call repaint, paint in it but there i lost you

When i enter the function paint( it needs Graphics) and won't compile Angry
I'm alsoo unsure if i'm ment to use the game classes in javax.microedition or mayby i need different ones, not for cellphones ?? I just want to run it on a pc and
if i keep using them can it be run on a compputer aswell then?
Mayby somebody know a site for starters in java game programming?
Note i use IntelJIdea 4.5
so many questions...

btw I found that this game http://www.java-gaming.org/forums/index.php?topic=15512.0 was +- what i had in mind.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2007-06-09 16:48:12 »

I can't help you because I don't what is the super class (GameCanvas) based on.

keldon85's advice were based on swing I guess... I don't think the example you have is based on swing.
Offline stef569

Junior Member





« Reply #4 - Posted 2007-06-09 16:56:01 »

ok, nevermind the example from the website.

let's put it in a short Question, I want to create a game on my local pc using 2D, Tile map using an Array, and Turn based.

What Game classes are there that support the above.
and can the MIDP 2.0 Game class be used not for mobil phone games but just for the pc.
http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/game/GameCanvas.html There is where i found it.
Offline keldon85

Senior Member


Medals: 1



« Reply #5 - Posted 2007-06-09 20:07:58 »

I can't help you because I don't what is the super class (GameCanvas) based on.

keldon85's advice were based on swing I guess... I don't think the example you have is based on swing.

Oh right; when I read in the documentation that GameCanvas extended Canvas I thought they meant the Java Canvas not their own random_canvas. For some reason GameCanvas is not loading!

Offline Kova

Senior Member





« Reply #6 - Posted 2007-06-09 22:02:54 »

but his, stef569's, link works ... sorry I'm not going into that deep to examine classes
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2007-06-09 23:23:13 »

I highly doubt that the MIDP library will work just on PC's. Looking at the GameCanvas documentation, I noticed one thing that makes me believe this:  It had static variables to represent various keys, and these keys were for ones that wouldn't be used on a PC for a game, they seemed tailored for cell phones.

Perhaps you chose to use an external library for your 2D game stuff to make it easier, and there probably is one out there, but it's not that hard to do what you need, especially because Swing and AWT do most of it.  If you do do it yourself, you will learn so much more about basic Java programming that wouldn't have learned by using someone else's work (not to say that reusing is bad, but for beginning things, it's a good learning experience to do it yourself).

Offline keldon85

Senior Member


Medals: 1



« Reply #8 - Posted 2007-06-09 23:52:44 »

One question; what programming experience do you have? What are you aiming to achieve? And what do you know about how you need to go about getting started with games development?

I am asking so that we all know where you are coming from, that way you can be better directed on an appropriate route. There is nothing wrong with asking such a question, especially if there is no FAQ in an obvious place telling you this answer.

Offline stef569

Junior Member





« Reply #9 - Posted 2007-06-10 01:29:57 »

Thanks for the replys!

Keldon i have no problem awnsering to your questions:
what programming experience do you have?
I'm a student First grade Informatics, and I got an introduction into java and c.
An introduction means the difference between classes and objects, inheritance, public/private, collections, arrays, simple gui forms.
I didn't had: Drawing, buffering, shading, big projects(never more then 5 classes), animation.

What are you aiming to achieve?
I'm trying to learn the language better by using it, i've been thinking a long time to make a game, we actualy got a project for the c language
where i made a 4 on a row game text based. But i couldn't draw on the screen too complicated.
Then Java was about objects and it looked much easier and clear.
Then i saw a simple game on nintendo ds and i tought This is not so hard, let's try that. It's called Advanced wars.

And what do you know about how you need to go about getting started with games development?
I've been reading alot about it on the internet, I tried code moving triangles over myown canvas, saw tile based maps wich realy looked easy, stumbled over a game api with all kind of special stuff but that didn't work
then I came here. Later i found out it was for mobile phones...

lhkbob, i'll start with using awt and swing and maybyi'll post myfinished program here in some umm months?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!