Results 1 to 10 of 10
Thread: Kinda stuck... help?
- 12-05-2011, 05:02 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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
ShipFrameJava 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. */
Thanks :)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) {} }
- 12-05-2011, 05:27 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Kinda stuck... help?
Remove the semi colons from ur if statements
- 12-05-2011, 05:33 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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
- 12-05-2011, 05:38 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
- 12-05-2011, 05:41 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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.
- 12-05-2011, 06:00 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Kinda stuck... help?
Velx should be set to positive 1 in the right method
- 12-05-2011, 06:04 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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.
- 12-05-2011, 06:10 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 12-05-2011, 06:40 AM #9
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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) {} }
- 12-05-2011, 02:45 PM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Kinda stuck... help?
Pretty sure you set the height to 400 not 600. Also
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.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();
Similar Threads
-
Simple triangle printer (kinda)
By 5myl in forum New To JavaReplies: 2Last Post: 08-30-2011, 03:29 AM -
initiating objects question(kinda)
By helpisontheway in forum New To JavaReplies: 4Last Post: 01-11-2010, 03:07 AM -
Kinda stuck of learning Java
By jurka in forum New To JavaReplies: 2Last Post: 02-14-2009, 04:00 PM -
need help, weird question kinda.
By carlos123 in forum New To JavaReplies: 6Last Post: 01-22-2008, 03:19 AM -
k this is my ultimate project. kinda
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 04:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks