Results 1 to 2 of 2
Thread: Drawing moving object
- 02-16-2010, 10:15 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Drawing moving object
So... I continued to work on my little game code.
I currently don't have any error with it, but things aren't working the way I wish they were.:(Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GameNave2 extends JPanel implements Runnable, KeyListener{ public static void main (String args[]) { GameNave2 game = new GameNave2(); game.construirJanela(); } public void construirJanela() { JFrame janela = new JFrame(); janela.setTitle("Game: Nave"); janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); janela.setSize(800, 600); janela.setLocationRelativeTo(null); janela.add(this); setDoubleBuffered(true); setBackground(Color.black); addKeyListener(this); setFocusable(true); janela.setVisible(true); x = 400; y = 500; } private Thread animador; private int x, y, x2, y2; private Image nave; private Image tiro; private boolean atirar; private int tiroX, tiroY; public GameNave2() { nave = new ImageIcon("nave.png").getImage(); tiro = new ImageIcon("tiro.png").getImage(); } public void posicaoTiro() { tiroX = x; tiroY = y; } public void paintComponent (Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.drawImage(nave, x, y, this); if (atirar == true) { g2d.drawImage(tiro, tiroX, tiroY, this); } } public void addNotify() { super.addNotify(); animador = new Thread(this); animador.start(); } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { x2 = -5; } if (key == KeyEvent.VK_RIGHT) { x2 = 5; } if (key == KeyEvent.VK_UP) { y2 = -5; } if (key == KeyEvent.VK_DOWN) { y2 = 5; } if (key == KeyEvent.VK_SPACE){ atirar = true; } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { x2 = 0; } if (key == KeyEvent.VK_RIGHT) { x2 = 0; } if (key == KeyEvent.VK_UP) { y2 = 0; } if (key == KeyEvent.VK_DOWN) { y2 = 0; } } public void keyTyped (KeyEvent e) { } public void movimento() { x += x2; y += y2; } public void run() { while (true) { movimento(); repaint(); if (atirar == true) { tiroY -= 5; } try { Thread.sleep(25); } catch (InterruptedException e) { } } } }
For example, when the image tiro (wich is a shot) is drawn, it's draw at the position 0,0, when I wanted it to be drawn as tiroX and tiroY being a position near the spacecraft I have in the game.
This way, the image would be moving from the lower to the upper part of the screen.
Also, I wanted to be able to shoot more than just one time...
The method posicaoTiro(), when called inside the paintComponent() method just makes things worse...
If someone could help me... I'd be glad.Last edited by dotabyss; 02-16-2010 at 10:24 PM.
- 02-16-2010, 11:20 PM #2
I would suggest creating seperate classes for the ship and for the projectiles in order to keep track and manager the things that belong in your world. You can store them in a list that you can loop through during your update phase.
With that, when you create your "tiro" object you can give it a position relative to your ships object.My Hobby Project: LegacyClone
Similar Threads
-
moving square
By blindfolded in forum New To JavaReplies: 5Last Post: 01-22-2010, 05:58 PM -
moving a file
By swati.jyoti in forum New To JavaReplies: 8Last Post: 11-23-2009, 08:44 AM -
Moving Box
By anilanar in forum New To JavaReplies: 2Last Post: 08-30-2009, 12:29 PM -
Moving textboxes
By GabWit in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:07 PM -
moving a file
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks