game character movement according to the map?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

game character movement according to the map?

Postby itskrishnaraj » Wed Sep 03, 2008 5:06 am

hi guys

in my game there is a player(Actor) and the player is standing on a horizontally mapped tile...
my problem is i just want to move the player according to the tiles . the player is a set of frames using drawRegion i am drawing that player ..when i press right key he has to move right in a position wise manner

any pls give me a solution


***********here is my code***************
Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class ExampleGameCanvas extends GameCanvas implements Runnable
{
    private boolean isPlay;   // Game Loop runs when isPlay is true
    private long delay=8;   // To give thread consistency
 
    private int k;
    private int counter=0;
   
    private int xpos=50;
    private int ypos=60;   
   
    private byte ROTATE_NONE = Sprite.TRANS_NONE;
    private byte ROTATE_90 = Sprite.TRANS_ROT90;
    private byte ROTATE_180= Sprite.TRANS_ROT180;
    private byte ROTATE_270= Sprite.TRANS_ROT270;      
   
    private byte CURRENT_ROTATE=ROTATE_NONE;
   
        private Image clwalk;   
        private Image tilesImg,bottomboard,clownIdle;                //Image varibale declaration-kris

        private int clwalkXstart=0;
        private int clnX=0;
       
        private int tileXPos = 0;
   private int tileYPos = 150;
   private byte tileWidth = 21;
   private byte tileHeight = 21;
   private int tileStartXPosition = 0;
   private int tileStartYPosition = 0;
   private int numberOfRows = 12;
   private int numberOfColumns = 11;
   private int tileStartY = 39;
       
           private int sampleMap[][] = {
         //1, 2, 3, 4, 5, 6, 7, 8, 9,10,11
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},            //11x12
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1, 1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
         {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},           //-1 for empty space
         {0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0}                      //0 for square tile

   };
               
  private int current_Map[][] = new int[numberOfRows][numberOfColumns];        //this will store the current level
 
// Constructor and initialization
public ExampleGameCanvas()
   {
     
    super(true); 
    try
      {
       
        tilesImg = Image.createImage("/tiles_240x320.png");
        bottomboard = Image.createImage("/bottomboard_240x320.png");
        clwalk = Image.createImage("/cl_walk_240x320.png");
       
      }catch(Exception ee){}
   
  }

  // Automatically start thread for game loop
  public void start()
   {
    isPlay = true;
    Thread t = new Thread(this);
    t.start();
   }


  // Main Game Loop
  public void run()
    {
     Graphics g = getGraphics();
     while (isPlay == true)
      {
       input();     
       drawMap(g);
       drawPlayer(g);
       try { Thread.sleep(delay); }
       catch (InterruptedException ie) {}
      }
    }

  // Method to Handle User Inputs
  private void input()
  {
    int keyStates = getKeyStates();



    // Right
    if ((keyStates & RIGHT_PRESSED) !=0 )
   
      {
       System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbb");
       clnX=clnX+2;
      }
    else if ((keyStates & LEFT_PRESSED) !=0 )
   
      {
       System.out.println("1111111111111111111111111111111111");
       clnX=clnX-2;
      }
     
   
  }

 
  private void drawMap(Graphics g)
   {             
      System.out.println("jjjjjjjjjjjjjjjjjjjjjjj");
      System.out.println("oooooooooooooooooooo");           
      
         
      g.setClip(0, 0, getWidth(), getHeight());
               
      g.drawImage(bottomboard, 0, getHeight()-bottomboard.getHeight(), 0);
               
                current_Map=sampleMap;   
      for(byte row = 0; row < numberOfRows; row++)
      {
         for(byte col =0; col  < numberOfColumns; col++)
         {
            switch(current_Map[row][col])
            {
            case 0:               
               g.drawRegion(tilesImg, tileStartXPosition, tileStartYPosition, tileWidth, tileHeight, ROTATE_NONE, tileXPos,220, Graphics.TOP|Graphics.LEFT);

               break;
            }
                               
                             
            if(tileXPos < 215)
                 tileXPos += tileWidth;
                 else
                 tileXPos = 5;
                               
                               
         }
                      if(row >= 11)
            tileYPos = tileStartY;
            else
                                tileYPos += tileHeight;
         
         
      }

      
         // repaint();
        //  flushGraphics();

   }
 
   private void drawPlayer(Graphics g)
   {
       g.setColor(0xffffff);
       g.fillRect(0,0,getWidth(),155+clwalk.getHeight());
       
       g.drawRegion(clwalk,clwalkXstart, 0, 26, 41, ROTATE_NONE, clnX,180, Graphics.TOP|Graphics.LEFT);
       clwalkXstart=clwalkXstart+26;
       if(clwalkXstart==390)
           clwalkXstart=0;
        flushGraphics();
        repaint();
   }
 
}






thanks in advance
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: game character movement according to the map?

Postby itskrishnaraj » Thu Sep 04, 2008 7:24 am

any body pls help me to sort this out...


how can move a player or a strip of image on a tiled map...?

thanks in advance
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: game character movement according to the map?

Postby DevelopmentTeam » Thu Sep 04, 2008 7:27 am

You can keep 2 layers, one has the hero character and the other has the tiled map.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: game character movement according to the map?

Postby itskrishnaraj » Thu Sep 04, 2008 8:39 am

DevelopmentTeam wrote:You can keep 2 layers, one has the hero character and the other has the tiled map.




can u jus send me a sample code ..it will be very helpful


thanks in advance
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: game character movement according to the map?

Postby DevelopmentTeam » Thu Sep 04, 2008 9:28 am

We dont post sample code for everything, this is a simple implementation and if you have any doubt doing it post here.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: game character movement according to the map?

Postby itskrishnaraj » Thu Sep 04, 2008 9:34 am

DevelopmentTeam wrote:We dont post sample code for everything, this is a simple implementation and if you have any doubt doing it post here.



thanks nate...its solved

thank you for the suggestions
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: game character movement according to the map?

Postby DevelopmentTeam » Thu Sep 04, 2008 10:39 am

your welcome...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: Bing [Bot] and 3 guests

cron