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);
}
}