- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的游戏中,玩家可以使用箭头键进行旋转。根据玩家旋转的角度,当子弹发射时,它会朝玩家面对的方向行进。
该部分有效,但是,子弹在错误的位置产生。下面两张图片显示了生成的差异。在第一张图片中,子弹在红点上产生。然而,当旋转时,点不会相应移动,这会阻止子弹正确生成。
注意:红点仅用于视觉效果(基于子弹的生成坐标)
the bullet spawns correctly when facing upwards
the bullet spawns incorrectly when facing every other direction
相关代码如下:
主要监听器:
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;
}
}
最佳答案
好吧,这是你的新油漆移动和转动方法 - 你还需要改变枪的角度:
public void paint(Graphics g2d){
// Drawing the rotated image at the required drawing locations
if(op!=null) g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
else g2d.drawImage(currentImg, (int)x, (int)y, null);
g2d.setColor(Color.blue);
g2d.fillOval(gunX, gunY, 10, 10);
}
public void move(double xM, double yM){
x += xM * Math.cos(Math.toRadians(angle));
y += yM * Math.sin(Math.toRadians(angle));
gunY += yM * Math.sin(Math.toRadians(angle));
gunX += xM * Math.cos(Math.toRadians(angle));
repaint();
}
public void turn(double angle){
this.angle += angle;
double rotationRequired = Math.toRadians (this.angle);
double locationX = currentImg.getWidth() / 2;
double locationY = currentImg.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
int gx= currentImg.getWidth() - 15;
int gy=0;
Point pt1=new Point(gx, gy), pt2=new Point();
tx.transform(pt1, pt2);
gunX=(int)x+pt2.x; gunY=(int)y+pt2.y;
repaint();
}
您需要:
全局变量
AffineTransformOp op;
在播放器中。
关于java - 我的子弹出现在错误的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41366224/
是否可以用按钮替换 ul 元素符号?我知道您可以使用 CSS 更改为图像,但如果我想要一个实际的元素呢? 最佳答案 这不可能如您所问,但您也许可以效仿它: some button textL
我在让元素符号显示在无序列表中时遇到了一些问题。如果我更改 css 以在内部显示元素符号,它们应该向上,但紧挨着文本。我的伙伴想要的是元素符号显示在框架的最左侧,并且在它和文本之间有几个标签值的空间。
我正在使用 the Orbit component of Foundation 5并想知道是否有办法将出现在幻灯片下方的元素符号移动到幻灯片本身的内容区域中。基本上,将元素符号放在图片上方。 有很多
我有一个从 wordpress 帖子生成的 ul li 边栏。客户为每个 LI 的元素符号使用了不同的图像。与其手动添加单独的元素符号,不如从一组(比如说 10 个)选项中进行选择? 正在考虑 Jqu
我有一个带有方向键和 2 个按钮的游戏 handle 。所有这些都是数字的(不是模拟的)。 我有一个处理他们的事件的程序: -(void)gamepadTick:(float)delta {
稍微注释掉后,我发现当我在 JavaScript 中运行此命令时,我的无序列表中的元素符号消失了(每个“页面”都是我的无序列表中的一个元素。): pages[i].style.display = "b
假设我想存储所有子弹,任何人在我的游戏中射击以计算每帧的新位置等。 如果有 10 名玩家,并且每个人的射击速率为每秒 10 次射击,我们可能需要在 10 秒后跟踪 1000 个物体。 我们确实知道,数
这是我所拥有的: import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt
我正在通过 ant-design (antd) 库开发一个 reactjs 元素。我想在我的元素中使用 Carousel 组件。当我将内容的背景色 - 应显示在轮播内 - 设置为#FFF 时,元素符号
我是一名优秀的程序员,十分优秀!