Results 1 to 6 of 6
  1. #1
    Tart is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Need Help making character move

    Hi I've started to make a 2d java game but the keyListener wont let me move. IF someone could help me it would be great. Im going to post my code in the section below.

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Board:


    package OurGame;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;


    public class Board extends JPanel implements ActionListener {
    Dude p;
    public Image img;
    Timer time;


    public Board() {
    p = new Dude();
    addKeyListener(new AL());
    setFocusable(true);
    ImageIcon i = new ImageIcon("C:/Users/Desktop/My Java Game/OurGame/Images/background2.jpg");
    img = i.getImage();
    time = new Timer(5, this);
    time.start();

    }

    public void actionPerformed(ActionEvent e){
    p.move();
    repaint();

    }

    public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.drawImage(img, 0, 0, null);
    g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    }

    private class AL extends KeyAdapter {
    public void KeyReleased(KeyEvent e){
    p.KeyReleased(e);

    }

    public void KeyPressed(KeyEvent e){
    p.KeyPressed(e);
    }
    }

    }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Dude:

    package OurGame;

    import java.awt.event.KeyEvent;
    import java.awt.*;

    import javax.swing.ImageIcon;


    public class Dude {
    int x, dx, y, dy;
    Image truck;

    public Dude(){
    ImageIcon i = new ImageIcon("C:/Users/Desktop/My Java Game/OurGame/Images/truck.png");
    truck = i.getImage();
    x = 2;
    y = 240;

    }
    public void move(){
    x = x + dx;
    y = y + dy;
    }

    public int getX(){
    return x;
    }

    public int getY(){
    return y;
    }

    public Image getImage(){
    return truck;
    }

    public void KeyPressed(KeyEvent e){
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_A){
    dx = -1;
    }

    if(key == KeyEvent.VK_D){
    dx = 1;
    }

    if(key == KeyEvent.VK_W){
    dy = 1;
    }

    }

    public void KeyReleased(KeyEvent e){
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_LEFT){
    dx = 0;
    }

    if(key == KeyEvent.VK_RIGHT){
    dx = 0;
    }

    if(key == KeyEvent.VK_UP){
    dy = 0;
    }
    }


    }
    Last edited by Tart; 09-04-2013 at 09:11 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Need Help making character move

    When posting code, please use the code tags and post it as an SSCCE.

    Time to do some debugging. You need to figure out where the program's execution differs from what you expected. Does the KeyListener ever get triggered? Do the if statements inside of it work as expected? If that works, where is the difference? What specific line is giving you trouble? The creation of an SSCCE will help with this process.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tart is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need Help making character move

    Ok, I will try takes for the help, and next time I will use code tags. This was my first post I and I didn't know.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Need Help making character move

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Need Help making character move

    tips4java - one of the best sites on Java IMO. The information is just so damned complete and idiot proof.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Re: Need Help making character move

    i wrote a keylistener here pixel perfect collision

    maybe take a look

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2013, 02:19 AM
  2. Replies: 1
    Last Post: 11-06-2012, 03:29 PM
  3. Replies: 0
    Last Post: 11-06-2012, 03:09 PM
  4. Replies: 0
    Last Post: 11-06-2012, 03:09 PM
  5. Replies: 2
    Last Post: 12-01-2007, 08:48 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
  •