×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Ball is behind the map.

    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
    Code:
    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);
    
       }
    
    
     }
    Game Canvas
    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;
            }
        }
        
    }
    tnx!

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

    Re: Ball is behind the map.

    ball.paintBall() needs to go after layerManager.paint().

    shmoove

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Re: Ball is behind the map.

    I got it tnx shmoove!

    But my problem now is that the ball should follow my player, in short dribbling player. What should I add so that the ball should be part of the player? whenever I move the ball should follow.. Thx guys! by the way here is my player sprite class:

    Code:
    public class PlayerSprite extends Sprite {
        
           private Ball ball = null;
       
           private int xPos;
           private int yPos;
           
           private int fWidth;
           private int fHeight;
           
           private int scnWidth = 176;
           private int scnHeight = 208;
           
           private Image image;
           
           private boolean isRun;
    
                 
        public PlayerSprite(Image image, int fWidth, int fHeight, int scnWidth, int scnHeight) throws Exception {
            
         super(image,fWidth,fHeight);
         
         ball = new Ball();
         
         fWidth = getWidth();
         fHeight = getHeight();
         
         xPos = fWidth / 2;
         yPos = fHeight / 2;
         
         this.scnWidth = scnWidth;
         this.scnHeight = scnHeight;
         this.fWidth = fWidth;
         this.fHeight = fHeight;
         
         this.defineReferencePixel(xPos,yPos);
     }
        
       public void sPos(){
           setPosition(75,95);
       }
       
       public void drib(){
           ball.moveBall();
           
       }
       
       public void noKey(){
           setFrame(1);
       }
       
       public void Left() {
       
           move(-1, 0);
           setFrame(7);
    
     }
    
       public void Right() {
          
           move(1, 0);
           setFrame(8);
    
     }
    
       public void Up() {
     
           move(0, -1);
           setFrame(3);
            
     }
       public void Down() {
      
           move(0, 1);
           setFrame(2);       
     }
       
       public void Shoot(){
           
       }
       
       public void Pass(){
    
       }
       
      
     public void display(Graphics g) {
       this.paint(g);
     }
        
    }
    Last edited by th3martian; 2007-02-22 at 06:39.

Similar Threads

  1. game with a map
    By alojix in forum Mobile Java Media (Graphics & Sounds)
    Replies: 7
    Last Post: 2012-09-12, 10:15
  2. map 3D view
    By xDone in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-11-17, 13:47
  3. Save Point in a Large Map
    By hellowuin in forum Mobile Java General
    Replies: 0
    Last Post: 2005-11-13, 14:45
  4. City Map for 3650
    By malaysianppl in forum Symbian
    Replies: 0
    Last Post: 2004-03-17, 03:46
  5. Efficiently implementing a scrolling map system in J2ME
    By dwneonstar in forum Mobile Java General
    Replies: 3
    Last Post: 2003-05-21, 17:57

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×