I created my own ball class, the ball's movement is bouncing. My map class is a tiled layer. And I have player sprite class.
My problem now is that whenever I load the map class w/ ball class, the bouncing ball does not show, BUT if I disable my map class I can see the bouncing ball. Here is my code.
Ball class
Game CanvasCode:public class Ball { private int xball, yball; private int vY,vX; private int meX, meY; private int compX, compY; private int ballW; private int ballH; private Image bball; public Ball() throws Exception{ xball = 0; yball = 1; vY = 5; vX = 2; try{ bball =Image.createImage("/ball.png"); } catch (IOException e){ e.printStackTrace(); } } public void moveBall(){ try { if(yball >= 15) vY = vY * -1; else if(yball <= 0) vY = vY * -1; //repaint(); xball+=vX; yball+=vY; }catch(Exception e) { e.printStackTrace(); } } void paintBall(Graphics g){ g.drawImage(bball,meX + 15,yball,0); } }
tnx!Code:public class StreetCanvas extends GameCanvas implements Runnable, CommandListener { private Streetball midlet; private Option options; private Court court; private Ball ball; private int keyStatus = 0; private boolean isPlay = true; private int scrnW; //screen coords private int scrnH; //screen coords private TiledLayer tcourt; private Image tball; private Command backCommand = new Command("Back", Command.BACK,1); private Thread gameThread = null; private LayerManager layerManager; private long delay; /** Creates a new instance of StreetCanvas */ public StreetCanvas(Streetball midlet, Option options) throws Exception { super(false); this.midlet = midlet; this.setFullScreenMode(true); addCommand(backCommand); setCommandListener(this); scrnW = getWidth(); //176 scrnH = getHeight(); //208 delay = 0; isPlay = true; //BALL ball = new Ball(); //COURT court = new Court(); tcourt = court.getCourt(); //LAYER MANAGER layerManager = new LayerManager(); layerManager.append(tcourt); layerManager.append(ball); } public void start() { gameThread = new Thread(this); gameThread.start(); } public void stop() { gameThread = null; } public void run() { Graphics g = getGraphics(); while (isPlay == true) { drawScreen(g); ball.moveBall(); myMove(); repaint(); serviceRepaints(); try{ Thread.sleep(delay); } catch (InterruptedException ie){ } } } private void myMove() { switch(keyStatus) { case GameCanvas.KEY_NUM2: //up player[0].Up(); break; case GameCanvas.KEY_NUM8: //down player[0].Down(); break; case GameCanvas.KEY_NUM4: //left player[0].Left(); break; case GameCanvas.KEY_NUM6: //right player[0].Right(); break; default: } } protected void keyPressed(int keyCode) { if (this.getGameAction(keyCode) == UP) { keyStatus = KEY_NUM2; } else if (this.getGameAction(keyCode) == DOWN) { keyStatus = KEY_NUM8; } else if (this.getGameAction(keyCode) == LEFT) { keyStatus = KEY_NUM4; } else if (this.getGameAction(keyCode) == RIGHT) { keyStatus = KEY_NUM6; } else if (this.getGameAction(keyCode) == FIRE) { keyStatus = KEY_NUM0; } else { keyStatus = keyCode; } } protected void keyReleased(int keyCode) { keyStatus = 0; } private void drawScreen(Graphics g) { g.setColor(255,255,255); g.fillRect(0,0,getWidth(), getHeight()); ball.paintBall(g); try { // Get Current Map tcourt = court.getCourt(); } catch (IOException ex) { ex.printStackTrace(); } layerManager.paint(g,0,0); flushGraphics(); } public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.mainMenuScreenShow(null); return; } } }



