trying to make my player shoot.
when i hit space bar my player should shoot bullet and it should keep on going to right. but some some reason my when i hit space bar the bullet stays at one point and doesnt move.
i am trying to move bullet here. x is bullet x poistion and dx is bullet speed. note this method is in loop so it should move on bullet on right.
public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s) { x += dx; }/*** end of PLAYER_SHOOT method ***/
than i am print my bullet here. when user hit space bar. than variable hitSPACE get set to ture.
public void paint(Graphics g, Player p) { if(hitSPACE == true) { g.drawImage(this.getSIMAGE(), p.getX(), p.getY(), width, height, null); } }/*** paint method ***/
but the problem is that my bullet is not moving. any idea why this is hapening???
shoot.java
public class Shoot { private int x; private int y; private int dx; private int width ; private int height; private boolean hitSPACE = false; //user hit space bar private boolean dead = true; //shoot is dead private static ImageIcon shoot_image = new ImageIcon("Image/shoot/bull2.png"); public Shoot() { dx = 3; width = 20; height = 20; } /*** get/set methods ***/ public static Image getSIMAGE() { return shoot_image.getImage(); } public int getX() { return x; } public void setX(int value) { this.x = value; } public int getY() { return y; } public void setY(int value) { this.y = value; } public int getWIDTH() { return width; } public void setWIDTH(int value) { this.width = value; } public int getHEIGHT() { return height; } public void setHEIGHT(int value) { this.height = value; } public boolean isDEAD() { return dead; } public void setDEAD(boolean value) { this.dead = value; } /*** Key pressed ***/ public void hitSHOOT() //user hit spacebar { hitSPACE = true; dead = true; } public void stopSHOOT() //user stop spacebar { hitSPACE = false; } /******************/ /*** LOOP METHOD ***/ /*******************/ public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s) { x += dx; System.out.println(x); }/*** end of PLAYER_SHOOT method ***/ /*** paint method ***/ public void paint(Graphics g, Player p) { if(hitSPACE == true) { g.drawImage(this.getSIMAGE(), x, y, width, height, null); } }/*** paint method ***/ }
main.java
public class Main extends Applet implements Runnable, KeyListener, MouseMotionListener, MouseListener { //Double buffering variables Image dbImage; Graphics dbGraphics; Thread thread; //Class variables Background background_class; Score score_class; Ground ground_class; Sprite_Sheet sprite_sheet_class; Player player_class; Platform platform_class[] = new Platform[5]; Enemy enemy_class; Item item_class[] = new Item[3]; Shoot shoot_class; //is if game is running or not boolean running = true; boolean mouseIn = false; Container c; /*** init method ***/ @Override public void init() { setSize(900, 400); addKeyListener(this); addMouseListener(this); addMouseMotionListener(this); }/*** end of init method ***/ /*** start method ***/ @Override public void start() { /*** set variables ***/ Random r1 = new Random(); sprite_sheet_class = new Sprite_Sheet(this); background_class = new Background(); ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height player_class = new Player(5, 250); //x, y shoot_class = new Shoot(); for(int i = 0; i < platform_class.length; i++) { Random r = new Random(); platform_class[i] = new Platform(i*120, 300); //x,y --sub -40 so no cut off platform at bottom } //item_class = new Item_0_Background(200); //x print 1 item for(int i = 0; i < item_class.length; i++) { item_class[i] = new Item_1_Fast(r1.nextInt(900)); } //x - print 1 item than swich to 2nd item enemy_class = new Enemy(500, 345); //x, y score_class = new Score(5,30); /*** play music in main ***/ sprite_sheet_class.theme_music.loop(); thread = new Thread(this); //start run method thread.start(); }/*** end of start method ***/ /*** run method ***/ public void run() { while(player_class.isDEAD() == false) //if player is not dead keep going { Random r = new Random(); background_class.BACKGROUND_MOVE(this); ground_class.GROUND_COLLISION(this, player_class); //ground + player collions player_class.PLAYER_MOVE(this, ground_class); //player moves shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class); //player shoot bullet moves for(int i = 0; i < platform_class.length; i++) { platform_class[i].PLATFORM_ANIMATION(); platform_class[i].PLATFORM_COLLSION(this, player_class); } enemy_class.ENEMY_MOVE(this, sprite_sheet_class); enemy_class.PLAYER_ENEMY_COLLISION(this, player_class, enemy_class); //player + enemy collision for(int i = 0; i < item_class.length; i++) { item_class[i].PLAYER_ITEM_COLLISION(this, score_class, player_class, ground_class); if(item_class[i].getY() == 0) //if item is top left coner { item_class[i] = null; //delete item item_class[i] = new Item_0_Background(r.nextInt(900)-50);//create new item int ra = r.nextInt(4); if(ra == 0) { item_class[i] = new Item_0_Background(r.nextInt(900)-10); //create new item } else if(ra == 1) { item_class[i] = new Item_1_Fast(r.nextInt(900)-10); //create new item } else if(ra == 2) { item_class[i] = new Item_2_SLOW(r.nextInt(900)-10); // swich to different item } else if(ra == 3) { item_class[i] = new Item_3_Score(r.nextInt(900)-10); } } } //set score score_class.SCORE(); score_class.health(player_class, enemy_class, sprite_sheet_class); /* msuic when player is dead */ if(player_class.isDEAD() == true) { sprite_sheet_class.theme_music.stop(); sprite_sheet_class.dead_music.play(); } repaint(); try { Thread.sleep(10); } catch(InterruptedException e) { e.printStackTrace(); } } }/*** end of run method ***/ /*** update method ***/ @Override public void update(Graphics g) { if(dbImage == null) //if image is empty than create new image { dbImage = createImage(this.getSize().width, this.getSize().height); dbGraphics = dbImage.getGraphics(); } dbGraphics.setColor(getBackground()); //set the background color dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height); dbGraphics.setColor(getForeground()); paint(dbGraphics); //call paint method g.drawImage(dbImage, 0, 0, this); }/*** end of update method ***/ /*** paint method ***/ @Override public void paint(Graphics g) { background_class.paint(g, this); ground_class.paint(g); //call paint method from Ground class //draw ground than player on top player_class.paint(g); //call paint method from Player classr for(int i = 0; i < platform_class.length; i++) { platform_class[i].paint(g); } shoot_class.paint(g, player_class); //draw bullet if(enemy_class.isDEAD() == false) //draw enemy if it's not dead { enemy_class.paint(g); } for(int i = 0; i < item_class.length; i++) { item_class[i].paint(g); } score_class.paint(g, this, player_class); }/*** end of paint method ***/ /*** stop method ***/ @Override public void stop() { }/*** end of stop method ***/ /*** destroy method ***/ @Override public void destroy() { }/*** end of destroy method ***/ /************** key *********************/ @Override public void keyPressed(KeyEvent e) { int keys = e.getKeyCode(); if(keys == KeyEvent.VK_RIGHT) { player_class.hitRIGHT(); sprite_sheet_class.running_music.loop(); } else if(keys == KeyEvent.VK_LEFT) { player_class.hitLEFT(); sprite_sheet_class.running_music.loop(); } else if (keys == KeyEvent.VK_UP) { player_class.hitJUMP(ground_class); sprite_sheet_class.jump_music.play(); } else if(keys == KeyEvent.VK_SPACE) { shoot_class.hitSHOOT(); } } @Override public void keyReleased(KeyEvent e) { int keys = e.getKeyCode(); if(keys == KeyEvent.VK_RIGHT) { player_class.stopRIGHT(); sprite_sheet_class.running_music.stop(); } else if(keys == KeyEvent.VK_LEFT) { player_class.stopLEFT(); sprite_sheet_class.running_music.stop(); } else if(keys == KeyEvent.VK_SPACE) { shoot_class.stopSHOOT(); } } @Override public void keyTyped(KeyEvent e){} /*** mouse mostion ***/ @Override public void mouseDragged(MouseEvent e) {} @Override public void mouseMoved(MouseEvent e) { if(e.getX() > 270 && e.getX() < 460) { if(e.getY() > 310 && e.getY() < 350) { mouseIn = true; } } if(e.getX() < 280 || e.getX() > 460) { mouseIn = false; } if(e.getY() < 320 || e.getY() > 360) { mouseIn = false; } } /************ MOUSE **********/ @Override public void mousePressed(MouseEvent e) { //start game over if(mouseIn == true) { start(); } //mouseIn = false; } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} }