it have 3 classes.
main class = run every thing
player class = create player and create his moves
level class = render map and player

when i run this code below it render a map and two players.

1st player in redering in main when can move. if user hit right or left key it moves fine. (its moves bc of a method player_class.player_move(). i am calling this method in main)

but problem is in 2nd player which is redering in level class. it does render but it doesnt move bc player_move() method is not appliet on 2nd player.

i want some way so player_move() method is applied on 2nd player. which will make him move too.

/*** main.java ***/

public class main exted JApplet implement ActionListener
{
        player player_class = new player();
    level level_class = new level();
        ....

     //useing timer and this is a main game loop
      public void actionPerformed(ActionEvent e)
        {
          player_class.player_move() ; 
          repaint();
        }

        public void paint()
        {
            super.paint(g);
            player_class.paint(g);
            level_class.paint(g);
        }
}

/*** player.java/

public class player
{
    ....

   public void player_move()
   {
      //if user hit right key than move player right
     //if user hit left key than move player left
     //x+=2; or x-=2
   }

    //my player which is just a box
     public void paint(Graphics g)
    {
        g.drawIRect(x, y, width, height); 
       }
}

/*** level.java ***/

public class level 
{
    private int level01[][] =            {  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,1,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                {2,2,2,2,2,2,2,0,0,2,2,2,2,2,2} };

    int image_tile_size = 32;  //32 by 32 image



    public level()
    {
    }


    @Override
    public void paint(Graphics g)
    {


//render map. 
//draw green box where its '2'
//draw player where its '1'
//draw blue box for sky where its '0'

        for(int y = 0; y < level01.length; y++) //rows
        {
            for(int x = 0; x < level01[y].length; x++) //cols
            {
                if(level01[y][x] == 0)
                {
                    g.setColor(Color.blue);
                    g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size); //bc image_title_size is the image size
                }
                if(level01[y][x] == 1)
                {
                    //draw player where there is '1'
                    player player_class = new player(x*image_tile_size, y*image_tile_size);
                    player_class.paint(g);
                }
                if(level01[y][x] == 2)
                {
                    g.setColor(Color.green);
                    g.fillRect(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size);
                }
            }
        }//end of main for loop
    }//end of paint method
}//end of level class

I don't think you should be creating a new player every time you paint. I'm not certain I fully understand what you are trying to accomplish, but painting and creating players seem to be completely unrelated concepts to me. On the other hand, perhaps creating the player doesn't matter since you immediately throw the player away once it has done its painting, but it is still contrary to what people would commonly expect.

player one which i am creating in main is just for testing. i will del it later.

i am only create player onces in level class. but iam not sure how to appily player_move() method in level class.

On line 48 of level.java you create a player every time you paint. I don't know what player is supposed to represent, so I'll just assume that is correct.

If you want to call player_move() on that player, the first step will be to not throw it away. You will probably need to have a field like:

private java.util.List<player> players = new java.util.ArrayList<player>();

Every time you create a player you can players.add(player_class) to keep track of all the players you have created. If you do that then be sure to remove players from the list when you are finished with them or else the list will quickly become huge.

sorry i am not doing a good job at explaining. let me try again

i am making a game with 3 classes.

player class has two methods -paint- and -player_move()-
-paint() which render a box(this is my player).
-player_move() it uses keypressed etc.. methods. this method works fine and it will let me move the player(which is a box) left or right.

to render and move player in main method: this is what i did:

main.java

public class main exted JApplet implement ActionListener
    {
            player player_class = new player();       
            ....

         //useing timer and this is a main game loop
          public void actionPerformed(ActionEvent e)
            {
              player_class.player_move() ;             //make him move
              repaint();
            }

            public void paint()
            {
                super.paint(g);
                player_class.paint(g);                  //render player(which is a box)
            }
    }

this method above works fine. this will render a player and it will allow me to move him.

now less take a look in level class:
i want to create a map in here so i did it in 2-array

  private int level01[][] =            {  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {0,1,0,0,0,0,0,0,0,0,0,2,0,0,0},
                                {2,2,2,2,2,2,2,0,0,2,2,2,2,2,2} }; 

now i want to render the player(which is a box) where its '1' in 2-array. its at bottom left.

 for(int y = 0; y < level01.length; y++) //rows
        {
            for(int x = 0; x < level01[y].length; x++) //cols
            {
                if(level01[y][x] == 1)
                {
                    //draw player where there is '1'
                    player player_class = new player(x*image_tile_size, y*image_tile_size);
                    player_class.paint(g);
                }
            }
        }//end of main for loop

this code above will render the player where every you put '1' in 2-array.

so far good.

the problem is that player doesnt move. its bc player_move() method a applied in main method and not in level class. i want to appliy player_move method to player in level class.

The key concept of the new keyword is that it creates new things, not old things that exist previously or in other places. So when you call paint on a player that you just created using new, that is literally painting a new player, not the same player that you used to call player_move(). The player that you create in that rendering loop is used just once for painting and then forgotten, only to have yet another new player created for the same purpose next time the level is rendered. Don't expect these new players to remember that some other player was moved.

aw i c it now. les you say before i need to add it to arraylist when i create a player:
so for ex level.java

...
private ArrayList<player> players = new ArrayList<player>();
...
player player_class = new player(x*image_tile_size, y*image_tile_size, image_tile_size, image_tile_size);
player_class.paint(g);
players.add(player_class);

after that i use the player_move() method on 'players' in level.java?

Remember that players is a list, not a player, so you cannot call players.player_move(). You can instead call player_move on each player contained in players if that is what you want. For example:

for(player p: players)
    p.player_move();

But remember that you are creating a new player every time you paint, so if you move all of the players that you have created so far and then paint to show them in their new positions, then there will still be a player at the player starting position because a new player pops up there as part of the rendering loop. I expect your players will move in something like a conga line that gets longer and longer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.