Results 1 to 10 of 10
  1. #1
    Samir4021 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Kinda stuck... help?

    OK So I got my java project working. All I need now is help moving the ship. Everytime I click any of my arrow keys, the ship moves automatically to the left and goes off screen. I need it to be able to move freely whenever i click up down left or right. It doesnt seem to want to. Any help? Thanks.

    ShipFrame1
    Java Code:
    import javax.swing.*;
    
    public class ShipFrame1
    {
      public static void main(String [] args)
      {
        ShipFrame s = new ShipFrame();
        JFrame f = new JFrame();
        f.add(s);
        f.setVisible(true);
        f.setSize(600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Moving Ship");
      }
    }
    
    /*Directions: In this lab you will write the beginnings of a simple game. In particular
    you will write a program that can control the motion of a spaceship displayed on the screen. For the spaceship you may 
    use the image ship.gif located on the webpage. Your ship should move across the screen without bouncing and should be 
    controlled using a KeyListener. */
    ShipFrame
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ShipFrame extends JPanel implements ActionListener, KeyListener
    {
      Timer t = new Timer(5, this);
      ImageIcon image;
      double x = 0, y = 0, velX = 0, velY = 0;
      
      public ShipFrame()
      {
        image = new ImageIcon ("ship.gif");
        setBackground(Color.BLACK);
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
      }
      
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        int i = (int)x;
        int t = (int)y;
        image.paintIcon (this, g, i, t);
        
      }
      
      public void actionPerformed(ActionEvent e)
      {
        repaint();
        x += velX;
        y += velY;
      }
      
      public void up()
      {
        velY = -1;
        velX = 0;
      }
      
      public void down()
      {
        velY = 1;
        velX = 0;
      }
      
      public void left()
      {
        velX = -1;
        velY = 0;
      }
      
      public void right()
      {
        velX = -1;
        velY = 0;
      }
      
      public void keyPressed(KeyEvent e)
      {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_UP);
        {
          up();
        }
        if (code == KeyEvent.VK_DOWN);
        {
          down();
        }
        if (code == KeyEvent.VK_RIGHT);
        {
          right();
        }
        if (code == KeyEvent.VK_LEFT);
        {
          left();
        }
      
      }
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
    }
    Thanks :)

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Kinda stuck... help?

    Remove the semi colons from ur if statements

  3. #3
    Samir4021 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Kinda stuck... help?

    If i do, I get this error:

    Java Code:
    4 errors found:
    File: /Users/sami40211/Documents/DrJava/ShipFrame.java  [line: 66]
    Error: Syntax error, insert ";" to complete BlockStatements
    File: /Users/sami40211/Documents/DrJava/ShipFrame.java  [line: 70]
    Error: Syntax error, insert ";" to complete BlockStatements
    File: /Users/sami40211/Documents/DrJava/ShipFrame.java  [line: 74]
    Error: Syntax error, insert ";" to complete BlockStatements
    File: /Users/sami40211/Documents/DrJava/ShipFrame.java  [line: 78]
    Error: Syntax error, insert ";" to complete BlockStatements

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Kinda stuck... help?

    Make sure the semi colons you removed are the ones in front of the closing ) in the if statements head not the code in their bodies

  5. #5
    Samir4021 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Kinda stuck... help?

    Awesome! Thats done. But ship still wants to move to the left "off screen" for up, down, left right whichever i press. Any reason for that? I could send you the files if you want to try it out and see whats happening.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Kinda stuck... help?

    Velx should be set to positive 1 in the right method

  7. #7
    Samir4021 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Kinda stuck... help?

    Thanks so much! You rock! Now what if I wanted to make it so if it goes off screen, it comes back on the other side? i + repped you
    Last edited by Samir4021; 12-05-2011 at 06:08 AM.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Kinda stuck... help?

    You will need the ships width, the frame width, and the current x,y position of the ship. Write up some code first any kind of real attempt. And I will have another look.

  9. #9
    Samir4021 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Kinda stuck... help?

    ok so I got it when it goes to the right and off screen, it appears on the left. but if I go right, up, or down, it disappears. Know why?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ShipFrame extends JPanel implements ActionListener, KeyListener
    {
      final int WIDTH = 600, HEIGHT = 600;
      final int DELAY = 20, IMAGE_SIZE = 35;
      
      Timer t = new Timer(5, this);
      ImageIcon image;
      double x = 0, y = 0, velX = 0, velY = 0;
      
      public ShipFrame()
      {
        image = new ImageIcon ("ship.gif");
        setBackground(Color.BLACK);
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
      }
      
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        int i = (int)x;
        int t = (int)y;
        image.paintIcon (this, g, i, t);
        
      }
      
      public void actionPerformed(ActionEvent e)
      {
        x += velX;
        y += velY;
        x = x%(WIDTH-IMAGE_SIZE);
        y = y%(HEIGHT-IMAGE_SIZE);
        repaint();
      }
      
      public void up()
      {
        velY = -1.5;
        velX = 0;
      }
      
      public void down()
      {
        velY = 1.5;
        velX = 0;
      }
      
      public void left()
      {
        velX = -1.5;
        velY = 0;
      }
      
      public void right()
      {
        velX = 1;
        velY = 0;
      }
      
      public void keyPressed(KeyEvent e)
      {
        int code = e.getKeyCode();
        
        if (code == KeyEvent.VK_UP)
        {
          up();
        }
        if (code == KeyEvent.VK_DOWN)
        {
          down();
        }
        if (code == KeyEvent.VK_RIGHT)
        {
          right();
        }
        if (code == KeyEvent.VK_LEFT)
        {
          left();
        }
      
      }
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
    }

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Kinda stuck... help?

    Pretty sure you set the height to 400 not 600. Also

    Java Code:
    x = x%(WIDTH-IMAGE_SIZE); // This will not work if x is less than 0
    y = y%(HEIGHT-IMAGE_SIZE); // This will not work if y is less than 0
    repaint();
    Do the math on pen and piece of paper. Add some println statements to print out the values of x and y before the repaint() call. This will give you an insight into what values they are being converted to. Compare those values to what you expect to see.

Similar Threads

  1. Simple triangle printer (kinda)
    By 5myl in forum New To Java
    Replies: 2
    Last Post: 08-30-2011, 03:29 AM
  2. initiating objects question(kinda)
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 03:07 AM
  3. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 04:00 PM
  4. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 03:19 AM
  5. k this is my ultimate project. kinda
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 04:47 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
  •