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
