Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Norm

Thread: Platform Game |Player Not Showing Up|

  1. #1
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default Platform Game |Player Not Showing Up|

    Hi, im making a platform game and right now i just have the player movement and the background.
    But when i start up the game, The background shows up, but no player.
    My code:
    Player.java
    Java Code:
    package Blocky;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
     
    import javax.swing.ImageIcon;
     
    public class Player {
            int x, dx, y, dy;
            Image PlayerI;
     
            public Player() {
                    ImageIcon i = new ImageIcon("imgs/blockp.png");
                    PlayerI = i.getImage();
                    x = 10;
                    y = 172;
            }
     
            public void PlayerMove() {
                    x = x + dx;
            }
     
            public int getX() {
                    return x;
            }
     
            public int getY() {
                    return y;
            }
     
            public Image getImage() {
                    return PlayerI;
            }
     
            public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT)
                            dx = -1;
     
                    if (key == KeyEvent.VK_RIGHT)
                            dx = 1;
            }
     
            public void keyReleased(KeyEvent e) {
                    int key = e.getKeyCode();
     
                    if (key == KeyEvent.VK_LEFT)
                            dx = 0;
     
                    if (key == KeyEvent.VK_RIGHT)
                            dx = 0;
            }
     
    }
    Board.java
    Java Code:
    package Blocky;
    
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    public class Board extends JPanel implements ActionListener {
    	
    	Player BlChar;
    	Image PlayerImage;
    	Image Background;
    	Timer time;
    
        public Board() {
            BlChar = new Player();
            addKeyListener(new ActionL());
            setFocusable(true);
            ImageIcon i = new ImageIcon(this.getClass().getResource("imgs/BlBk.png"));
            Background = i.getImage();
            time = new Timer(5, this);
            time.start();
        }
        
        public void actionPerformed(ActionEvent e){
        	BlChar.PlayerMove();
        	repaint();
        }
        public void paint(Graphics g) {
        	super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.drawImage(Background, -18, -24, null); 
            g2d.drawImage(BlChar.getImage(),BlChar.getX(),BlChar.getY(),null);
        }
        
        private class ActionL extends KeyAdapter{
        	public void keyReleased(KeyEvent e){
        		BlChar.keyReleased(e);
        	}
        	public void keyPressed(KeyEvent e){
        		BlChar.keyPressed(e);
        	}
        }
    }
    Frame.java
    Java Code:
    package Blocky;
    
    import javax.swing.*;
    
    public class Frame {
     
            public Frame(){
                    JFrame frame = new JFrame();
                    frame.add(new Board());
                    frame.setTitle("Blocky");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(480,480);
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
            }
            public static void main(String[] args){
                    new Frame();
            }
    }
    What do i do wrong? Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    but no player.
    Where is the player being drawn?

    You should override paintComponent (vs paint) in Swing components.

    It works for me. I changed your images to mine.
    Last edited by Norm; 06-28-2011 at 01:17 AM.
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I agree with Norm: your code works if the images are "findable". I see that the background image is obtained as a resource while your player image isn't -- perhaps both should be obtained the same way, the one that is successful. I also agree with not drawing in paint but rather in paintComponent.

  4. #4
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Ahh. its shows up now. Thanks! But i have one more problem. The movement isnt really that smooth. You hold down the right(or left) key and it moves for a little then stops. So you keep having to press it for it to move. Thanks Again!

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    it moves for a little then stops.
    Even when the key is held done continuously?
    How is the motion supposed to react to key presses?
    Hold a key down, the image moves.
    Release the key the image stops.

  6. #6
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Ahh so when you press a key, then without releasing, you press another key and release instantly after, It takes a second until the player actually moves in the other direction.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Check your logic to see if there is a conflict with the motion flag/value settings.

  8. #8
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Im sorry but i dont know what those are. Im a newb to java coding >.<

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Sorry, I thought you wrote the code shown in post #1.
    The motion flag/value settings would be the dx and x values. dx control which way to move, x is the location of the image.

  10. #10
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    I did right the code >.<, well most of it. I got the movement off a video.
    And there doesnt seem to be any problem. my movement code
    Java Code:
            public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT)
                            dx = -1;
     
                    if (key == KeyEvent.VK_RIGHT)
                            dx = 1;
            }
     
            public void keyReleasedLeft(KeyEvent e){
                    int key = e.getKeyCode();
     
                    if (key == KeyEvent.VK_LEFT)
                            dx = 0;
    
            }
            public void keyReleasedRight(KeyEvent e){
            	 int key2 = e.getKeyCode();
                if (key2 == KeyEvent.VK_RIGHT)
                        dx = 0;
            }

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Could be some OS delay between sending press and release notifications.

  12. #12
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Is there a way to fix that?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    I don't know. What OS are you on?

    You could see when a key event occurs by printing out the time. For example
    System.out.println("kPress at " + System.currentTimeMillis());

  14. #14
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    I use Windows 7. I did what you suggested and it prints on the exact second i press down.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    It should be closer to the millisec than the second.
    If you do your key pressing that causes the delay, do you see time differences in the printout that correspond to the delay's wait time?

  16. #16
    petur170 is offline Member
    Join Date
    May 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    It should be closer to the millisec than the second.
    If you do your key pressing that causes the delay, do you see time differences in the printout that correspond to the delay's wait time?

    Well yeah its about the same millisec. Annd yes i see time differences

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    i see time differences
    If those delays come from the OS, then I don't know how to program around it. Perhaps there is a Control Panel setting that would change when the key press/release is passed to your code.

Similar Threads

  1. Help with Mp3 player
    By Crathes in forum New To Java
    Replies: 36
    Last Post: 05-16-2011, 12:50 AM
  2. Rpg game player wont move help please
    By askumi in forum New To Java
    Replies: 19
    Last Post: 12-22-2010, 03:21 AM
  3. How to add online two player to a Java game?
    By michealsmith in forum Java Gaming
    Replies: 1
    Last Post: 11-30-2010, 08:43 PM
  4. how to log into a game with my player.java class
    By basketball8533 in forum New To Java
    Replies: 11
    Last Post: 10-13-2010, 01:40 AM

Tags for this Thread

Posting Permissions

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