Jump to content
  • Advertisement

joe96

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

102 Neutral

About joe96

  • Rank
    Newbie
  1. I'm working on a 2d game in java. I have a problem using the key listener to move the character. I might be typing the code wrong or something but i need help fixing it. This is my player.java class:   import java.awt.event.*;   public class Player implements KeyListener{   public int x = 500, y = 300;  public int health = 100; public int dx; public int dy;   public Player(){ x = x + dx; y = y + dy; }   public int getXPos(){ return x; }   public int getYPos(){ return y; }   public int getHealth(){ return health; }     public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_W){ dy = 2; } if(keyCode == KeyEvent.VK_A){ dx = -2; } if(keyCode == KeyEvent.VK_S){ dy = -2; } if(keyCode == KeyEvent.VK_D){ dx = 2; } }   public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_W){ dy = 0; } if(keyCode == KeyEvent.VK_A){ dx = 0; } if(keyCode == KeyEvent.VK_S){ dy = 0; } if(keyCode == KeyEvent.VK_D){ dx = 0; } }     }     In a different class i draw the character as a rectangle like this:   public void paint(Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.fillRect(p.getXPos(), p.getYPos(), 25, 25); }     The problem is the character doesn't move when i press the buttons. I'm relatively new to java. Any help will be greatly appreciated.
  2. Well, you know where your player is on the screen (playerX and PlayerY), you know where the mouse was clicked (mouseX and mouseY), and you have a general velocity for the bullet (float BulletVelocity) right? You have 2 different methods you can use to set the bullet's X and Y velocity: #1, The Trigonometry method (recommended), you set the position of the bullet at the player, and set velocity of the bullet to be: // get the angle from the player to the mouse clicked position float Angle = atan2(mouseX - playerX, mouseY - playerY); VelocityX = cos(Angle)*BulletVelocity; VelocityY = sin(Angle)*BulletVelocity; Or, you can do it the conceptual/geometric method, by determining how far the mouse is from the player, and the time it should take for the bullet to get there: // Get the distance of the mouse from the player (A^2 + B^2 = C^2) float distanceX = mouseX - playerX; float distanceY = mouseY - playerY; float distance = sqrt(distanceX*DistanceX + DistanceY*DistanceY); // get time it would take the bullet to get there travelling at BulletVelocity (assuming velocity is pixels/second) float Time = distance/BulletVelocity; // now give the X and Y speeds for the bullet to get to that point: VelocityX = distanceX/Time; VelocityY = distance/Time; [/quote] in the first example what is the atan2 supposed to mean. thanks for your reply.
  3. I've been working on a top - down 2d zombie shooter. I've got all the player movement down but i have been having trouble with the shooting. I cant figure out how to make it shoot in the direction of where the mouse was clicked. I use java to program it. If anyone can help that would be great.
  4. i use java and im kinda new to making games with it
  5. I'm making a 2d side scrolling game. I want a randomly generated world similar to Terraria. If you've never heard of it the website is here: http://www.terraria.org . I've tried many different things but I can't figure out how to. If anyone can help that would be great.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!