Har uploaded in video på youtube der viser problemet, og her er noget kode (der er ret meget selvom jeg kun har taget det jeg mener er vigtig, vis der skal mere til, så sig til)
GamePanel (gameloop osv. er her)
- package myGame;
-
- import javax.swing.*;
-
- import myGame.obejcts.*;
-
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
-
- import java.util.*;
-
- public class GamePanel extends JPanel implements Runnable {
- public KeyListener keyboard = new KeyboardInput();
-
- private GOKillBall bold;
-
- public GamePanel() {
- this.setPreferredSize(new Dimension(500, 500));
- this.setBackground(new Color(0, 0, 0));
-
- bold = new GOKillBall(10, 10, 2, Math.toRadians(0), "sprites\\sprite.png");
- }
-
- public void paint(Graphics g) {
- super.paint(g);
- Graphics2D g2 = (Graphics2D) g;
-
- bold.draw(g2);
- }
-
- private void processInput() {
- if(((KeyboardInput) keyboard).keydown(KeyEvent.VK_RIGHT)) {
- bold.move(GOKillBall.RIGTH);
- }
- if(((KeyboardInput) keyboard).keydown(KeyEvent.VK_LEFT)) {
- bold.move(GOKillBall.LEFT);
- }
- if(((KeyboardInput) keyboard).keydown(KeyEvent.VK_UP)) {
- bold.move(GOKillBall.UP);
- }
- if(((KeyboardInput) keyboard).keydown(KeyEvent.VK_DOWN)) {
- bold.move(GOKillBall.DOWN);
- }
- if(((KeyboardInput) keyboard).keydown(KeyEvent.VK_SPACE)) {
- bold.fireBullet();
- }
- }
-
- private void update() {
- bold.update();
- }
-
- boolean running = false;
-
- //GameLoop
- public void run() {
- running = true;
- while(running) {
- ((KeyboardInput) keyboard).poll();
- processInput();
- update();
- repaint();
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- System.out.println(e);
- }
- }
- }
- }
GOKillBall (min lille bold)
- package myGame.obejcts;
-
- import java.awt.*;
- import java.io.*;
- import java.util.*;
-
- import javax.imageio.ImageIO;
-
- public class GOKillBall {
- public static final int LEFT = 0;
- public static final int RIGTH = 1;
- public static final int UP = 2;
- public static final int DOWN = 3;
-
- private int x, y;
- private int vel;
- private double dir;
-
- private Vector<GOProjectile> bulletArray = new Vector<GOProjectile>();
-
- Image sprite;
-
- public GOKillBall(int xPos, int yPos, int velocity, double direction, String imageFilePath) {
- x = xPos;
- y = yPos;
- vel = velocity;
- dir = direction;
- try {
- sprite = ImageIO.read(new File(imageFilePath));
- }
- catch(IOException e) {
- System.out.println(e);
- }
- }
-
- public void move(int direction) {
- switch (direction) {
- case RIGTH:
- dir = Math.toRadians(0);
- x = x+vel;
- break;
- case LEFT:
- dir = Math.toRadians(180);
- x = x-vel;
- break;
- case UP:
- dir = Math.toRadians(270);
- y = y-vel;
- break;
- case DOWN:
- dir = Math.toRadians(90);
- y = y+vel;
- break;
- default:
- System.out.println("kan ikke bevæge mig i denne direction");
- break;
- }
- }
-
- public void fireBullet() {
- bulletArray.add(new GOProjectile(x, y, 5, 0, "sprites\\bullet.png"));
- }
-
- public void draw(Graphics2D g2) {
- g2.rotate(dir, (x + sprite.getWidth(null)/2), (y + sprite.getHeight(null)/2));
- g2.drawImage(sprite, x, y, null);
-
- for(Iterator<GOProjectile> i = bulletArray.iterator(); i.hasNext();) {
- GOProjectile temp = i.next();
- temp.draw(g2);
- }
- }
-
- public void update() {
-
- }
- }
GOProjectile (projektil)
- package myGame.obejcts;
-
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.io.*;
-
- import javax.imageio.ImageIO;
-
- public class GOProjectile {
- public static final int LEFT = 0;
- public static final int RIGTH = 1;
- public static final int UP = 2;
- public static final int DOWN = 3;
-
- private int x, y;
- private int vel;
- private double dir;
-
- Image sprite;
-
- public GOProjectile(int xPos, int yPos, int velocity, double direction, String ImageFilePath) {
- x = xPos;
- y = yPos;
- vel = velocity;
- dir = direction;
- try {
- sprite = ImageIO.read(new File(ImageFilePath));
- }
- catch(IOException e) {
- System.out.println(e);
- }
- }
-
- public void move() {
- if(dir == Math.toRadians(0)) {
- x = x + vel;
- }
- else if(dir == Math.toRadians(180)) {
- x = x - vel;
- }
- else if(dir == Math.toRadians(270)) {
- y = y - vel;
- }
- else if(dir == Math.toRadians(90)) {
- y = y + vel;
- }
- else {
- System.out.println("WTF en wierd direction du har sendt mig ud på ZOMG!!!");
- }
- }
-
- public void draw(Graphics2D g2) {
- g2.rotate(dir, (x + sprite.getWidth(null)/2), (y + sprite.getHeight(null)/2));
- g2.drawImage(sprite, x, y, null);
- }
- }
Filmen er her:
http://www.youtube.com/watch?v=Ap7udjc7erg<eidt>
projektilet burde bare bilve hvor det var
</edit>
håber at i forstår koden, ellers bare spørg, så kan jeg forklare den (håber jeg)
Indlæg senest redigeret d. 06.10.2008 16:06 af Bruger #13669