move the player according to the tiled map?

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

move the player according to the tiled map?

Postby itskrishnaraj » Mon Sep 08, 2008 12:16 pm

hi all,


my game background is created using tiles
like

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},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1, 1, 0, 2,-1,-1,-1,-1},
{-1,-1,-1, 1, 0, 0, 0, 2,-1,-1,-1},
{-1,-1, 1, 0, 0, 0, 0, 0, 2,-1,-1},
{-1, 1, 0, 0, 0, 0, 0, 0, 0, 2,-1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

i jus want to move the player according to thid mao...or this arry .how?
thanks in advance
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby DevelopmentTeam » Tue Sep 09, 2008 7:35 am

everytime you move the player you have check what is below him from the corresponding map element. If the item is blocking then the player has to land on it or if it is empty space you have to make him fall.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: move the player according to the tiled map?

Postby itskrishnaraj » Wed Sep 10, 2008 4:52 am

DevelopmentTeam wrote:everytime you move the player you have check what is below him from the corresponding map element. If the item is blocking then the player has to land on it or if it is empty space you have to make him fall.



hi

your point is correct , but here i am using the player is a combination of frames . ya i can get the position of the player how i will get the position of the tile (map) according to the player.

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

Re: move the player according to the tiled map?

Postby DevelopmentTeam » Wed Sep 10, 2008 5:09 am

tilex = playerx/tilewidth
tiley = playery/tileeight

this calculation will give you the exact tile in the map where the player is placed so you have to add 1 in tiley to locate the tile below the player
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: move the player according to the tiled map?

Postby itskrishnaraj » Wed Sep 10, 2008 6:05 am

DevelopmentTeam wrote:tilex = playerx/tilewidth
tiley = playery/tileeight

this calculation will give you the exact tile in the map where the player is placed so you have to add 1 in tiley to locate the tile below the player



ya its true my friend, i am trying to do it in a different way like if i get which is the number (-1,0,1,2) where the player standing , i can give it it in the manner
like
private int playerMoveMap[][]={

//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},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1, 1, 0, 2,-1,-1,-1,-1},
{-1,-1,-1, 1, 0, 0, 0, 2,-1,-1,-1},
{-1,-1, 1, 0, 0, 0, 0, 0, 2,-1,-1},
{-1, 1, 0, 0, 0, 0, 0, 0, 0, 2,-1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
private int player1CurrentRow=11;
private int player1CurrentCol=0;
private int currentPosition;

now the player current position is 11x0 th position that is '0'

now i want to check next column after checking i can write some condition for each number 9-1,0,1,20 like if its -1 dont move its empty state,
0 its a rectangle its a block, 1 is left triangle we can easily move ,2 is right triangle


here ismy sample midlet

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class clsCanvas extends GameCanvas implements Runnable
{
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
private Image clwalk;
private int clwalkXstart;
private int clnX=3;
private int clnY=178;
private boolean playerRunning = false;
private boolean playerLeftRunning = false;

/*************FOR TILED MAP**************************/
private Image tileMap,bottomboard;
private int numberOfRows = 12; //index of rows
private int numberOfColumns = 11; //index of cols
private int tileStartXPosition= 0;
private int tileStartYPosition= 0;
private byte tileWidth = 21;
private byte tileHeight = 21;
private int tileXPos = 5;
private int tileYPos = 0;
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},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1, 1, 0, 2,-1,-1,-1,-1},
{-1,-1,-1, 1, 0, 0, 0, 2,-1,-1,-1},
{-1,-1, 1, 0, 0, 0, 0, 0, 2,-1,-1},
{-1, 1, 0, 0, 0, 0, 0, 0, 0, 2,-1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

private int playerMoveMap[][]={

//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},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1, 1, 0, 2,-1,-1,-1,-1},
{-1,-1,-1, 1, 0, 0, 0, 2,-1,-1,-1},
{-1,-1, 1, 0, 0, 0, 0, 0, 2,-1,-1},
{-1, 1, 0, 0, 0, 0, 0, 0, 0, 2,-1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
private int player1CurrentRow=11;
private int player1CurrentCol=0;
private int currentPosition;

private int current_Map[][] = new int[numberOfRows][numberOfColumns]; //this will store the current level



/***********MAP VARIABLES ENDS HERE*****************/

public clsCanvas(midMain m) //constructor to initilization
{
super(true);
fParent=m;
try {
clwalk = Image.createImage("/cl_walk_240x320.png");
tileMap = Image.createImage("/tiles_240x320.png");
bottomboard = Image.createImage("/bottomboard_240x320.png");
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void start() //start game thread here
{
Thread runner = new Thread(this);
runner.start();
}

public void run()
{ //run method
int iKey = 0;
g = getGraphics();
while(isRunning)
{
drawMap(g);
drawPlayer(g);



iKey = getKeyStates();

if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) //keyPress_RIGHT
{
playerRunning = true;
System.out.println("right_key_pressed");


}
else if ((iKey & GameCanvas.LEFT_PRESSED) != 0) //keyPress_RIGHT
{
playerLeftRunning = true;
System.out.println("Left_key_pressed");


}
if(playerRunning) {

moveStripRight();

}
if(playerLeftRunning){
moveStripLeft();
}
flushGraphics();

try {
Thread.sleep(30);
} catch (Exception ex) { }
}//while ends here

g = null;
fParent.destroyApp(false);
fParent = null;
}//run ends here
private void drawMap(Graphics g)
{
current_Map=sampleMap;
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.setClip(0, 0, getWidth(), getHeight());
g.drawImage(bottomboard, 0, getHeight()-bottomboard.getHeight(), 0);
for(byte row = 0; row < numberOfRows; row++)
{
for(byte col =0; col < numberOfColumns; col++)
{

switch(current_Map[row][col])
{
case 0:

g.drawRegion(tileMap, tileStartXPosition, tileStartYPosition, tileWidth, tileHeight, 0, tileXPos, tileYPos-55, Graphics.TOP|Graphics.LEFT);

System.out.println(tileYPos);
System.out.println("tileXpos-"+tileXPos);

break;
case 1:
g.drawRegion(tileMap, tileStartXPosition+tileWidth, tileStartYPosition, tileWidth, tileHeight, 2, tileXPos, tileYPos-55, Graphics.TOP|Graphics.LEFT);
break;
case 2:
g.drawRegion(tileMap, tileStartXPosition+tileWidth, tileStartYPosition, tileWidth, tileHeight, 0, tileXPos, tileYPos-55, Graphics.TOP|Graphics.LEFT);
break;
}
if(tileXPos < 215)
tileXPos += tileWidth;
else
tileXPos = 5;
}

if(row >= 11)
tileYPos = tileStartY;
else
tileYPos += tileHeight;


}

}

private void drawPlayer(Graphics g) //drawPlayer() Method [called by run()]
{

g.drawRegion(clwalk,clwalkXstart, 0, 26, 41, 0, clnX,clnY/*178*/, Graphics.TOP|Graphics.LEFT);

/*try {
Thread.sleep(40);
} catch (InterruptedException ex) {
ex.printStackTrace();
}*/

}//DrawPlayer ends here

private void moveStripRight() //moveStrip() method [called by run()]
{
clwalkXstart +=26; //clipX updating to 26
if(clwalkXstart==390) {
clwalkXstart=0;
playerRunning = false;
}

clnX +=1;
clnY -=1;//increasing the destinationX to 2


}
private void moveStripLeft() //moveStrip() method [called by run()]
{
clwalkXstart +=26; //clipX updating to 26
if(clwalkXstart==390) {
clwalkXstart=0;
playerLeftRunning = false;
}

clnX -=1;
clnY +=1;


}
private void movePlayer()
{




for(byte row_p=0;row_p<11;row_p++){
for(byte col_p=0;col_p<10;col_p++){
currentPosition=sampleMap[player1CurrentRow-1][player1CurrentCol+1];

System.out.println("currPos--------->"+currentPosition);
}
}



//System.out.println("nextPosition---------->"+PlayerNextPosition);



}






}
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby itskrishnaraj » Thu Sep 11, 2008 8:26 am

pls give me any suggestion
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby DevelopmentTeam » Thu Sep 11, 2008 9:08 am

you must consider the first index as 1 in calculations don't use 0. this should solve your problem I guess.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: move the player according to the tiled map?

Postby itskrishnaraj » Thu Sep 11, 2008 9:58 am

DevelopmentTeam wrote:you must consider the first index as 1 in calculations don't use 0. this should solve your problem I guess.


this is i am doing now but i am getting only a fixed number but i jus want the next position number ...then i wll easily calculate how to go?

private void movePlayer()
{
for(player1CurrentRow=10;player1CurrentRow>0;player1CurrentRow--){
for(player1CurrentCol=0;player1CurrentCol< 10;player1CurrentCol++ ){

currentPosition=sampleMap[player1CurrentRow][player1CurrentCol];
System.out.println("currpos>>>>>>>>>>>>>>>>>>>>>>>>>>"+currentPosition);
switch(currentPosition){
case -1:
break;
case 0:
clnX +=1;
break;
case 1:
clnX +=1;
clnY -=1;

break;
case 2:
clnX -=1;
clnY +=1;
break;

}
}
}
}
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby itskrishnaraj » Thu Sep 11, 2008 10:13 am

itskrishnaraj wrote:
DevelopmentTeam wrote:you must consider the first index as 1 in calculations don't use 0. this should solve your problem I guess.


this is i am doing now but i am getting only a fixed number but i jus want the next position number ...then i wll easily calculate how to go?

private void movePlayer()
{


currentPosition=sampleMap[player1CurrentRow][player1CurrentCol];
System.out.println("currpos>>>>>>>>>>>>>>>>>>>>>>>>>>"+currentPosition);
switch(currentPosition){
case -1:
break;
case 0:
clnX +=1;
break;
case 1:
clnX +=1;
clnY -=1;

break;
case 2:
clnX -=1;
clnY +=1;
break;

}

}


jus give me any solution to find out the next postion from the position player is standing?
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby DevelopmentTeam » Thu Sep 11, 2008 10:58 am

:shock: I dont understand what you are trying to do

What we usually do is, we find corresponding cell where the player is standing by diving the player position by size of each cell. This will give you the exact cell where the user is. You are complicating the problem I guess.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: move the player according to the tiled map?

Postby itskrishnaraj » Thu Sep 11, 2008 11:05 am

DevelopmentTeam wrote::shock: I dont understand what you are trying to do

What we usually do is, we find corresponding cell where the player is standing by diving the player position by size of each cell. This will give you the exact cell where the user is. You are complicating the problem I guess.




ok friend i got the player position on tile

tileX=PalyerX/tileWidth =0;
tileY=PlayerY/tileHeight =9;


then wat isthe next step according to you ? coz i didi this way but i am cofused with this concept
can you jus telll me?
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: move the player according to the tiled map?

Postby DevelopmentTeam » Fri Sep 12, 2008 4:53 am

Check what item is there in the Tile map array in the index 0,9 and check if it is a floor otherwise make the player fall down until you see a floor below him.
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 2 guests

cron