Java 中的寻的导弹行为
也许是一个初学者问题,但感谢您的阅读。我正在开发一个 2d Java 游戏,其中有导弹使用以下代码跟踪对象的位置。我希望导弹在距目标达到一定的最小位移时停止寻的,此时它们应该垂直落下。现在,只要导弹处于最小位移范围内,寻的行为就会关闭,如果位移增加,寻的行为就会再次打开。 我希望该行为仅关闭一次,并在导弹的剩余使用寿命内保持关闭状态。我该如何实现此目的?
public void home(int targetX, int targetY, int scale){
int dy = targetY - y;
int dx = targetX - x;
double speed, sep;
sep = Math.sqrt(dx * dx + dy * dy);
speed = scale/sep;
if(dy > 50 || dx > 50){
x += dx * speed;
y += dy * speed;
}
else{
x += 0;
y += scale;
}
}
Maybe a beginner question, but thank you for reading. I'm working on a 2d Java game in which I have missiles that track the position of an object using the following code. I'd like the missiles to stop homing when they reach a certain minimum displacement from their target, at which point they should fall straight down. Right now, the homing behavior turns off whenever the missile is within the minimum displacement, and turns on again if the displacement increases. I'd like the behavior to turn off only once, staying off for the remainder of the missile's lifespan. How can I accomplish this?
public void home(int targetX, int targetY, int scale){
int dy = targetY - y;
int dx = targetX - x;
double speed, sep;
sep = Math.sqrt(dx * dx + dy * dy);
speed = scale/sep;
if(dy > 50 || dx > 50){
x += dx * speed;
y += dy * speed;
}
else{
x += 0;
y += scale;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加成员变量,例如
boolean homing = true
。然后,将您的条件更改为如下所示:您基本上只需要打破导弹行为与其状态之间的相互依赖即可。
Add a member variable, such as
boolean homing = true
. Then, change your conditional to something like the following:You basically just need to break the mutual dependence between you missile's behaviour and its state.