![](/img/trans.png)
[英]How to convert from a mouse position to a character position in a JEditorPane in Java Swing
[英]JAVA Efficient way to draw bullets firing from character to mouse position?
我正在創建一個2D游戲,我想實現射擊。 但是,由於某種原因,子彈只能以-45°角射擊。 下面提供的是我的代碼。 if語句也很重要,需要知道項目符號何時超出范圍。 我查找了該問題的答案,但似乎都沒有用。
同樣,.getEntity返回實體的JLabel。
public class Bullet extends Entity
{
private double speed, dmg, dist, vX, vY, xDest, yDest;
private double traveledDist = 0;
private JLabel bullet;
private String img;
public Bullet(double nSpeed, double nDmg, double xDest, double yDest, double nDistance, String img, double vX,
double vY)
{
super(80, 80, "res/img/bullet.png");
speed = nSpeed;
dmg = nDmg;
dist = nDistance;
this.xDest = xDest;
this.yDest = yDest;
this.img = img;
this.vX = vX;
this.vY = vY;
Image image = new ImageIcon("res/img/bullet.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
ImageIcon imageicon = new ImageIcon(image);
JLabel bullet = new JLabel(imageicon);
bullet.setSize(80, 80);
bullet.setVisible(true);
}
public String toString()
{
return ("[Speed: " + speed + ", Damage: " + dmg + ", xDest: " + xDest + ", yDest: " + yDest + ", vX:" + vX
+ ", vY" + vY + ", Distance: " + dist + ", Traveled Distance: " + traveledDist + ", Image Path: " + img
+ "]");
}
public double getxDest()
{
return xDest;
}
public double getyDest()
{
return yDest;
}
public double getvX()
{
return vX;
}
public double getvY()
{
return vY;
}
public JLabel newBullet()
{
return bullet;
}
public double getSpeed()
{
return speed;
}
public double getDmg()
{
return dmg;
}
public double getDist()
{
return dist;
}
public String getImg()
{
return img;
}
public double getTraveledDist()
{
return traveledDist;
}
public void setTraveledDist(double nDist)
{
traveledDist = nDist;
}
}
private void fireBullet()
{
System.out.println("bullet");
double originX = lblCross.getX() - lblChar.getX();
double originY = lblCross.getY() - lblChar.getY();
double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
double bulletVelocity = 1.0;
//mouseX/Y = current x/y location of the mouse
//originX/Y = x/y location of where the bullet is being shot from
double angle = Math.atan2(mouseX - originX, mouseY - originY);
double xVelocity = (bulletVelocity) * Math.cos(angle);
double yVelocity = (bulletVelocity) * Math.sin(angle);
Bullet tempBullet = new Bullet(((ProjectileWeapon) sWeapon).getProjectileSpeed(), sWeapon.getDamage(), 0, 0,
sWeapon.getRange() * 100, ("res/img/bullet.png"), xVelocity, yVelocity);
tempBullet.getEntity().setVisible(true);
room.add(tempBullet.getEntity());
room.repaint();
tempBullet.getEntity().setLocation(lblChar.getX(), lblChar.getY());
bullets.add(tempBullet);
}
private void updateBullet()
{
for (int i = 0; i < bullets.size(); i++)
{
bullets.get(i).getEntity().getY());
if (true)
{
System.out.println("updating" + bullets.get(i));
Bullet b = bullets.get(i);
b.getEntity().setLocation((int) (b.getEntity().getLocation().getX() + (b.getvX() * 10)),
(int) (b.getEntity().getLocation().getY() + (b.getvY() * 10)));
b.setTraveledDist(b.getTraveledDist() + 1);
room.repaint();
}
else
{
room.remove(bullets.get(i).getEntity());
bullets.remove(i);
room.repaint();
}
}
}
非常感謝您的幫助。
這是我最終使用的代碼:
double theta = Math.atan2(destination.getY() - origin.getY(), destination.getX() -
origin.getX());
xVelocity = Math.cos(-theta);
yVelocity = -Math.sin(-theta);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.