Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  walking and falling sprites  (Read 1513 times)
0 Members and 1 Guest are viewing this topic.
Offline bab

Senior Newbie




Java games rock!


« Posted 2003-09-16 03:33:54 »

i'm developing a game with a map of 4 floor levels.  every floor level has a hole.

question is how do i detect that a player has stepped in it?  

i got a suggestion to make all the floors, except the holes, as sprites.  i think its not practical to create several floor sprite objects considering we are on limited memory.

what do u think guys? Sad
Offline davidaprice

Junior Member





« Reply #1 - Posted 2003-09-16 05:58:29 »

Each time you move the player's sprite, check to see if its position is above the hole's position. If it is, they fall!

Note: if you're building the level background from a grid of tiles, you probably have some 2-D array of tiles (e.g. tileType[12][5] == 2, where 2 means 'ladder', 3 means 'hole', etc.).  Now just create an extra array isSolid[], where isSolid[2] == true and isSolid[3] == false, etc. Now you can just check whether the player's sprite's position is above a tile for which isSolid
  • [y] == true. (Typically they'll be above 2 or 3 tiles, so you may have to check each of these). If none of the tiles below them is solid, they fall!
Offline bab

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-09-17 01:25:22 »

yup. my background is a grid of tiles.  regarding the isSolid array, does that mean that my tiles will have to be sprites also?

btw, i encapsulated my image tiles into one single image. is that right?

tnx david  Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davidaprice

Junior Member





« Reply #3 - Posted 2003-09-17 07:28:56 »

Your tiles don't need to be sprites. Instead, your tiled background needs to have its own class. Have a look at how the MIDP 2.0 game API works, and write similar classes yourself.

The tile image PNG file could have all the different tile type images in e.g. a horizontal row. Then you build the real background image by drawing the 'frames' of that resource file image repeatedly onto a big blank image according to your tile grid (array 'tiles' below).

Since you've got the tile grid, you can also use it to find out which type of tile is at a certain grid position. If you have an array ('isSolid' below) telling which types of tile are solid, you can use that in an 'isSolid' method.

Warning: the code below is an adaptation from memory of part of a (working) demo game I wrote, but I just typed it in now and I haven't even tried to compile it - no guarantees!  Wink   I'm afraid YABB does wierd things with its indentation  Sad.

class Background {
   static final int TILE_WIDTH = 8;
   static final int TILE_HEIGHT = 8;
   static final int TILE_COLUMNS = 7;  // see grid below
   static final int TILE_ROWS = 5;        // see grid below
   static private int[][] tiles = new int[][]
   { { 0, 0, 0, 0, 0, 0, 0 },     // 0 is empty space
      { 1, 1, 0, 1, 1, 2, 1 },     // 1 is solid rock, 2 is a ladder
      { 0, 0, 0, 0, 0, 2, 0 },
      { 0, 0, 1, 0, 0, 2, 0 },
      { 1, 1, 1, 1, 0, 1, 1 } };
   static private boolean isSolid[] = new boolean[]
   { false, true, true };    // space, rock, ladder
   private Image backgroundImg;

   // tilesImg is 24x8 pixels, with the images for space,
   // then rock, then ladder in a horizontal row
   Background(Image tilesImg) {
       backgroundImg = Image.createImage(
           TILE_WIDTH * TILE_COLUMNS,
           TILE_HEIGHT * TILE_ROWS);
       Graphics bg = backgroundImg.getGraphics();
       for (int tx = 0; tx < TILE_COLUMNS; ++tx) {
           for (int ty = 0; ty < TILE_ROWS; ++ty) {
                bg.setClip(tx * TILE_WIDTH, ty * TILE_HEIGHT,
                                TILE_WIDTH, TILE_HEIGHT);
               // draw tilesImg offset left so correct frame hits clip window
               bg.drawImage(tilesImg,
                                        (tx - tiles[ty][tx]) * TILE_WIDTH,
                                        ty * TILE_HEIGHT,
                                        Graphics.TOP | Graphics.LEFT);
           }
       }
   }

   void paint(Graphics g, int x, int y) {
       g.drawImage(backgroundImg, x, y, Graphics.TOP | Graphics.LEFT);
   }

   // tx & ty are tile index, not pixel locations, i.e. 0..6 and 0..4
   boolean isSolid(int tx, int ty) {
       return isSolid[tile[ty][tx]];
   }
}


I leave you the fun of working out which tiles to check beneath your sprite.
Offline bab

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-09-17 09:07:56 »

tnx david  Smiley i'll study ur code first.

whew! how i wish all phones supports midp 2.0 so that it wouldn't be this tough!
Offline bab

Senior Newbie




Java games rock!


« Reply #5 - Posted 2003-09-18 07:17:08 »

hi again david!

the situation's like this:

my main character/sprite is fixed at the bottom left corner of the screen.  everytime there is a keypress, the background moves, not the character.

problem is i can't detect whether the sprite is already beneath the "not Solid" tile.  

need help very badly...tnx Embarrassed
Offline davidaprice

Junior Member





« Reply #6 - Posted 2003-09-18 07:57:59 »

Quote
problem is i can't detect whether the sprite is already beneath the "not Solid" tile.

Here's another method for that Background class that might help you - it checks whether a rectangle on the screen overlaps a solid tile in the background, by finding which tiles the rectangle overlaps and checking each one in turn:
boolean overlapsSolid(int x, int y, int width, int height) {
   boolean overlaps = false;
   int tileXmin = Math.max(0, x / TILE_WIDTH);
   int tileYmin = Math.max(0, y / TILE_HEIGHT);
   int tileXmax = Math.min(TILE_COLUMNS, (x + width - 1) / TILE_WIDTH);
   int tileYmax = Math.min(TILE_ROWS, (y + height - 1) / TILE_HEIGHT);
   for (int tx = tileXmin; tx <= tileXmax; ++tx) {
       for (int ty = tileYmin; ty <= tileYmax; ++ty) {
           if (isSolid(tx, ty)) {
               overlaps = true;
               break;
           }
       }
   }
   return overlaps;
}


I assume that you start the character sprite in a place that doesn't overlap any solid tiles. Now each time the player wants to move left, check that the one-pixel-wide column of pixels to his left doesn't overlap any solid tiles:

if (!background.overlapsSolid(sprite.getX()-1, sprite.getY(), 1, sprite.getHeight()) {
   // can move left
}


Similarly, to check whether the sprite is standing with nothing solid underneath, check the one-pixel-tall row of pixels below him:

if (!background.overlapsSolid(sprite.getX(), sprite.getY() + sprite.getHeight(), sprite.getWidth(), 1) {
   // sprite starts falling
}


One note: if you're not drawing the background at 0,0 on the screen, you need to think about how the background's position affects the 'x' & 'y' parameters you pass to 'overlapsSolid'.
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.

xsi3rr4x (51 views)
2014-04-15 18:08:23

BurntPizza (48 views)
2014-04-15 03:46:01

UprightPath (63 views)
2014-04-14 17:39:50

UprightPath (45 views)
2014-04-14 17:35:47

Porlus (62 views)
2014-04-14 15:48:38

tom_mai78101 (86 views)
2014-04-10 04:04:31

BurntPizza (146 views)
2014-04-08 23:06:04

tom_mai78101 (243 views)
2014-04-05 13:34:39

trollwarrior1 (202 views)
2014-04-04 12:06:45

CJLetsGame (209 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!