#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Snake game in java


    the problem is that in Main.java line:
    playerObject.testMethod();
    playerObject is null right now. wasnt sure how to fix it.


    Player.java
    Code:
    ...
    public void testMethod(){
     ...
     //I want to run this method in main game loop.
    }
    
    //player class is just a green fill rect.
    public void paint(Graphics g)
    {
             g.setColor(Color.green);
           g.fillRect(x, y, width, height);
    }



    Level.java
    Code:
    2d array:
    
    000000
    000000
    010000
    
    
    public void paint(Graphics g, Player p){
         for(int y = 0; y < map01.length; y++){
              for(int x = 0; x < map01[y].length; x++){
                  if(map01[y][x] == 0)          //BACKGROUND
                        {
                            g.setColor(Color.black); 
                            g.fillRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
                          }
                     if(map01[y][x] == 1)          //PLAYER
                       {
                          p = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
                          p.paint(g);
                         }
                }
        }
    }

    Main.java
    Code:
    ....
    //start level
    public void start(){ 
        levelObject = new Level();
    ...
     
    //main game loop
     public void actionPerformed(ActionEvent e){
            playerObject.testMethod();
     
            repaint();
        }
     
    //paint method
    public void paint(Graphics g){ 
        levelObject.paint(g, playerObject);
    ...
    Last edited by game2d; May 27th, 2013 at 04:45 AM.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    playerObject = new Player( parameters );

IMN logo majestic logo threadwatch logo seochat tools logo