Results 1 to 3 of 3
  1. #1
    ROD_PAX-A-LOT is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default enemies show up but not player?

    I started a youtube game program Java Game Tutorial - 1 - Setting Up - YouTube. I was able to get an image up at first, then realized it would no longer load. I continued the program and thought it was something to do with netbeans so I did it in Eclipse. I can get the enemies to show up randomly with boxes around them, but player has been unavailable. Here is my GameFrame class, followed by Player class, Enemy class, and KeyAdapt, and Main. Maybe the problem is around line 39 (public void paint(Graphics g) in GameFrame, not calling the player class properly? No, errors or exceptions when running. I have used different images and they will show as enemies and even used the same one for both, so I don't think it is a bad link.

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class GameFrame extends JPanel implements ActionListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	Timer mainTimer;
        Player player;
        
        int enemyCount = 5;
        
        static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
        Random rand = new Random();
        
        public GameFrame() {
            setFocusable(true);
            
            player = new Player(100, 100);
            addKeyListener(new KeyAdapt(player));
            
            mainTimer = new Timer(10, this);
            mainTimer.start();
            for (int i = 0; i < enemyCount; i++) {
            	addEnemy(new Enemy(rand.nextInt(800), rand.nextInt(600)));
            }
        }
       
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            
            player.draw(g2d);
            
            for (int i = 0; i< enemies.size(); i++) {
            	Enemy tempEnemy = enemies.get(i);
            	tempEnemy.draw(g2d);
            }
        }
       @Override 
       public void actionPerformed(ActionEvent arg0) {
           player.update();
           repaint();
            
        }
        public void addEnemy(Enemy e) {
        	enemies.add(e);
        }
        public static void removeEnemy(Enemy e) {
        	enemies.remove(e);
        }
        public static ArrayList<Enemy> getEnemyList() {
        	return enemies;
        	
        }
        
    }
    Player Class

    Java Code:
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    
    
    public class Player extends Entity { 
        int velX =0, velY=0;
        int speed = 5;
        
        public Player (int x, int y) {
            super(x,y);
        }
       
        @Override
        public void update() {
            y += velY;
            x += velX;
            
            checkCollisions();
            
        }
       
        @Override
        public void draw(Graphics2D g2d) {
           g2d.drawImage(getPlayerImg(), x, y, null);
           g2d.draw(getBounds());
        }
        public Image getPlayerImg() {
            ImageIcon ic = new ImageIcon("C:/Users/Dad/Desktop/man.jpg‪");
            
            return ic.getImage();  
        }
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_W) {
              velY = -speed;
            } else if (key == KeyEvent.VK_S) {
              velY = speed;
            } else if (key == KeyEvent.VK_A) {
              velX = -speed;
            } else if (key == KeyEvent.VK_D) {
              velX = speed;
         }  
      }
        public void keyReleased(KeyEvent e) {
           int key = e.getKeyCode();
            
           if (key == KeyEvent.VK_W) {
              velY = 0;
           } else if (key == KeyEvent.VK_S) {
              velY = 0;
           } else if (key == KeyEvent.VK_A) {
              velX = 0;
           } else if (key == KeyEvent.VK_D) {
              velX = 0;
         }  
       }
        public void checkCollisions() {
        	ArrayList<Enemy> enemies = GameFrame.getEnemyList();
        	
        	for (int i = 0; i < enemies.size(); i++) {
        		Enemy tempEnemy = enemies.get(i);
        		
        		if (getBounds().intersects(tempEnemy.getBounds())) {
        			GameFrame.removeEnemy(tempEnemy);
        			
        		}
        	}
        }
        public Rectangle getBounds() {
        	return new Rectangle(x, y, getPlayerImg().getWidth(null),
        			getPlayerImg().getHeight(null));
        }
    }
    Enemy class

    Java Code:
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    
    import javax.swing.ImageIcon;
    
    
    public class Enemy extends Entity{
    
    	public Enemy(int x, int y) {
    		super(x, y);
    		
    	} public void update() {
    		
    	} public void draw(Graphics2D g2d) {
    		g2d.drawImage(getEnemyImg(), x, y, null);
    		g2d.draw(getBounds());
    	
    	} public Image getEnemyImg() {
    		ImageIcon ic = new ImageIcon("C:/Users/Dad/Desktop/man.jpg");
    		return ic.getImage();
    	
    	} public Rectangle getBounds() {
        	return new Rectangle(x, y, getEnemyImg().getWidth(null),
        			getEnemyImg().getHeight(null));
        }
    }
    KeyAdapt Class

    Java Code:
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    /**
     *
     * @author Dad
     */
    public class KeyAdapt extends KeyAdapter{
        Player p;
        
        public KeyAdapt(Player player) {
            p = player;
            
        
        }
        
        
        @Override
        public void keyPressed(KeyEvent e) {
            p.keyPressed(e);
        }
        
        @Override
        public void keyReleased(KeyEvent e) {
            p.keyReleased(e);
        }
    }
    Main
    Java Code:
    import javax.swing.JFrame;
    
    public class Main {
    
    	public static void main(String[] args) {
    		
    		       JFrame frame = new JFrame("FirstGame");
    		       frame.setSize(1200,1000);
    		       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		       frame.setResizable(false);
    		       frame.add(new GameFrame());
    		       frame.setVisible(true);
    		       
    		       
    		    }
    
    	}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: enemies show up but not player?

    Don't override paint().
    Override paintComponent().
    That's a general thing. If the tutorial you are using recommends overriding paint() in a Swing app then I would say you need to find another tutorial.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ROD_PAX-A-LOT is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: enemies show up but not player?

    Working now, thank you Tolls! It was my fault, not following the tutorial properly

Similar Threads

  1. Spawning enemies for my game
    By Kridane in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 12:46 AM
  2. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  3. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Posting Permissions

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