- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,对于编码作业,我们必须制作一款坦克游戏。我用这个创建了一个项目符号类:
package com.MyName.battletanks;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Bullet {
public Sprite b;
public float vx = 0;
public float vy = 0;
public Bullet(Sprite temp) { b = temp;}
public void move() { b.translate(vx*3,vy*3);}
}
我的变量如下:
Sprite Player1;
Sprite Player2;
ArrayList<Bullet> bullets;
单击空间后,它会使用以下命令创建项目符号:
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
Bullet bullet = new Bullet(new Sprite(new Texture("Bullet1.png")));
bullet.b.setPosition(Player1.getX() + Player1.getWidth() / 2, Player1.getY() + Player1.getHeight() / 2);
float rotation = (float) Math.toRadians(Player1.getRotation());
bullet.vx = (float) Math.cos(rotation);
bullet.vy = (float) Math.sin(rotation);
bullets.add(bullet);
}
现在,这是我的代码,用于让我的坦克从屏幕的一侧穿过到另一侧:
if (Player1.getX() > Gdx.graphics.getWidth()){
Player1.setX(-64f);
} else if(Player1.getX()<-64f){
Player1.setX(Gdx.graphics.getWidth());
}
if (Player1.getY() > Gdx.graphics.getHeight()){
Player1.setY(-64);
} else if(Player1.getY() < -64f){
Player1.setY(Gdx.graphics.getHeight());
}
现在,玩家 1 是一个 Sprite ,但是,子弹是使用数组列表和自制的子弹类创建的。因此,我无法使用为子弹所做的 Player1 代码。所以,我的问题是,如何让子弹传到屏幕的另一边?
最佳答案
您可以使用模 %
运算符执行以下操作:
bullet.position.x %= Gdx.graphics.getWidth();
bullet.position.y %= Gdx.graphics.getHeight();
这尚未经过测试,但应该可以工作。另外,我注意到您使用 2 个 float 作为速度,并且您确实应该使用 Vector2
因为这样您就可以轻松缩放和标准化它,这对于速度很有用。
关于java - 让子弹穿过屏幕的两侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36651259/
是否可以用按钮替换 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 时,元素符号
我是一名优秀的程序员,十分优秀!