내 게임에서 플레이어는 화살표 키를 사용하여 회전 할 수 있습니다. 플레이어의 회전 각도에 따라 총알이 발사되면 플레이어가 향하는 방향으로 이동합니다.내 총알이 잘못된 위치에 생성됩니다.
그러나 그 부분은 작동하지만, 그 부분은 잘못된 위치에서 산란합니다. 아래 그림은 스폰의 차이를 보여주는 두 그림입니다. 첫 번째 그림에서는 총알이 빨간색 점에 나타납니다. 그러나 도트를 회전하면 그에 따라 이동하지 않아 총알이 올바르게 산란하지 않습니다.
참고 : 빨간 점은 (총알의 스폰 좌표 기준) 단지 영상입니다 여기
the bullet spawns correctly when facing upwards
the bullet spawns incorrectly when facing every other direction
는 관련 코드 :
메인의 KeyListener :
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
//Creates bullets when the spacebar is pressed, as long as the user is not exceeding 3 at a time
if(player.amountOfBullets() >= 0 && player.amountOfBullets() < 10)
{
player.setNB(player.getGunX(), player.getGunY(), player.getAngle());
}
}
플레이어 :
public class Player {
private BufferedImage spriteSheet;
private double x;
private double y;
private int height;
private int width;
private int gunX;
private int gunY;
private BufferedImage[] sprites = new BufferedImage[13];
private BufferedImage currentImg;
private int frame;
private double angle = 0;
private ArrayList<Bullet> nb = new ArrayList<Bullet>(10);
public Player(){
try {
spriteSheet = ImageIO.read(getClass().getResource("/Sprites.png"));
} catch (IOException e) {
System.err.println("Sprites3.png could not be found");
}
for (int i = 0; i < sprites.length; i++){
sprites[i] = grabImage(1, i+1, 100);
}
currentImg = sprites[0];
x = 10;
y = 10;
width = currentImg.getWidth();
height = currentImg.getHeight();
gunX = (int) x + width - 15;
gunY = (int) y;
}
public BufferedImage grabImage(int col, int row, int length){
BufferedImage img = spriteSheet.getSubimage(((col-1) * length), ((row-1) * length), length, length);
return img;
}
public void paint(Graphics2D g2d){
for(int i = 0; i < nb.size(); i++)
{
if(nb.get(i).getTime()*10 >= 15)
{
nb.set(0, null);
nb.remove(nb.get(0));//removes bullets based on the time they are in game
}
}
double rotationRequired = Math.toRadians (angle);
double locationX = currentImg.getWidth()/2;
double locationY = currentImg.getHeight()/2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
for (int i = 0; i < nb.size(); i++)
{
nb.get(i).move();//moves the bullets
nb.get(i).paint(g2d);
}
// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
//g2d.fillOval(x, y, 10, 10);
g2d.setColor(Color.RED);
int x = (int) this.x;
int y = (int) this.y;
g2d.fillOval(gunX, gunY, 10, 10);
}
public void update(){
currentImg = sprites[frame];
}
public void move(double xM, double yM){
if (yM == 5) xM = -5;
else xM = 5;
x += xM * Math.sin(Math.toRadians(angle));
y += yM * Math.cos(Math.toRadians(angle));
gunY += yM * Math.cos(Math.toRadians(angle));
gunX += xM * Math.sin(Math.toRadians(angle));
if (frame < 12){
frame++;
}
else frame = 0;
}
public void turn(double angle){
this.angle += angle;
}
public int getX() {
return (int) x;
}
public int getY(){
return (int) y;
}
public int getGunX() {
return gunX;
}
public int getGunY(){
return gunY;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public void setNB(int x, int y, double angle)//sets position and movement of bullet
{
if (nb.size() > 10)
{
nb.set(0, null);
nb.remove(nb.get(0));//removes the bullets from the game when tried to exceed 3
}
if (nb.size() < 10)//only 3 shots allowed at a time
{
nb.add(new Bullet(x, y, angle));//adds bullets to list when a new one is shot
}
}
public ArrayList<Bullet> getNB()//sets position and movement of bullet
{
return nb;
}
public void removeNB(int i)//removes bullet from game when it hits an asteroid
{
nb.set(i, null);
nb.remove(nb.get(i));
}
public int getBulletX(int i)//gets bullet X coord
{
return nb.get(i).getX();
}
public int getBulletY(int i)//gets bullet Y coord
{
return nb.get(i).getY();
}
public int getBulletWidth(int i)//gets bullet width
{
return nb.get(i).getWidth();
}
public int getBulletHeight(int i)//gets bullet height
{
return nb.get(i).getHeight();
}
public int getBulletAngle(int i)//gets bullet angle
{
return nb.get(i).getAngle();
}
public double getAngle() {
return angle;
}
public int amountOfBullets() //checks amount of bullets in game
{
return nb.size();
}
}
총알 :
public class Bullet
{
//width = 6px, height = 8px
private BufferedImage bullet = null;
private BufferedImage img2 = null;
private double x, y;
private double angle;
//Creates an instance of timer since each bullet is supposed to only last 1.5 seconds
private Stopwatch timer = new Stopwatch();
public Bullet(int x, int y, double angle)
{
//Get the image of the normal bullet
try {
bullet = ImageIO.read(getClass().getResource("/Bullet.png"));
} catch (IOException e) {
System.err.println("Bullet.png could not be found");
}
//Sets the position and direction of the bullet relative to the spacecraft when it is created
timer.start();
this.x=x;
this.y=y;
this.angle=angle+90;
}
public void paint(Graphics2D g2d)
{
//Since the bullet sprite is not always oriented in the direction of the shot, orient it properly
angle-=90;
double rotationRequired = Math.toRadians(angle);
double locationX = bullet.getWidth()/2;
double locationY = bullet.getHeight()/2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
int x = (int) this.x;
int y = (int) this.y;
//g.drawImage(img2, x - 150, y - 150, null);
g2d.drawImage(op.filter(bullet, null), x, y, null);
//Change the angle back to the original for the bullet to travel in the correct direction
angle+=90;
}
public void move()
{
//Move the projectile in the direction it is facing
x+=-5* Math.cos(Math.toRadians(angle));
y+=-5* Math.sin(Math.toRadians(angle));
}
//Returns how long the bullet has been in existence
public long getTime()
{
return timer.getElapsedTime();
}
public int getX()//returns the x position of the bullet for collision detection
{
return (int)x;
}
public int getY()//returns the y position of the bullet for collision detection
{
return (int)y;
}
public int getWidth()//returns the width of the sprite for collision detection
{
return bullet.getWidth();
}
public int getHeight()//returns the height of the sprite for collision detection
{
return bullet.getHeight();
}
public int getAngle()//returns the angle of the bullet
{
return (int)angle;
}
}
어떤 경우에도 paint 또는 paintComponent 메소드에서 데이터 객체의 상태를 수정해야합니다. 당신의 통제하에 있지 않은 모든 종류의 이유 때문에 초에 여러 번 회화를 시작할 수 있습니다. 가끔 마우스를 움직이면 트리거됩니다! – VGR
"어떤 상황에서도 paint 또는 paintComponent 메소드의 데이터 객체 상태를 수정해야합니다.": 주요 문제를 해결하지 않고 반복적으로 이러한 환상을 제안함으로써 여러분이 어떻게 당황해하는지 궁금합니다. – gpasch