- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个小程序,其中一只羊被一只狗放牧,当狗对象靠近羊时,两者都是对象,而羊则以随机方向远离。 (很抱歉,我知道我已经问了很多关于这个小程序的问题)。到目前为止,一切正常,除了羊对象不会停留在我设置的范围内并最终完全从屏幕上消失。我尝试了很多不同的方法,但似乎没有任何效果。知道为什么绵羊对象不会停留在边界内吗?任何帮助将非常感激。这是代码,有两个单独的java文件:
MainPanel.java
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements MouseMotionListener
{
private static final long serialVersionUID = 1L;
Dog dog;
Sheep sheep;
int[] directionNumbersLeft = {0, 1, 3};
int[] directionNumbersRight = {1, 2, 3};
int[] directionNumbersUp = {0, 1, 2};
int[] directionNumbersDown = {0, 2, 3};
int x;
int selection;
int xposR;
int yposR;
int sheepx;
int sheepy;
int sheepBoundsx;
int sheepBoundsy;
int MAX_DISTANCE = 50;
int direction;
int distance;
public MainPanel()
{
addMouseMotionListener(this);
sheepx = 175;
sheepy = 75;
dog = new Dog(22,22);
sheep = new Sheep(sheepx, sheepy);
sheepBoundsx = 30;
sheepBoundsy = 30;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
dog.display(g);
sheep.display(g);
g.drawRect(20, 20, 600, 600);
}
public void mouseDragged(MouseEvent e)
{
xposR = e.getX();
yposR = e.getY();
dog.setLocation(xposR, yposR);
sheep.setLocation(sheepx, sheepy);
direction = (int)(Math.random()*4);
Random rand = new Random();
x = rand.nextInt(3);
if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
&& yposR < sheepy+sheepBoundsy && direction == 0){
sheepx = sheepx + 50;
}
if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
&& yposR < sheepy+sheepBoundsy && direction == 1){
sheepy = sheepy + 50;
}
if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
&& yposR < sheepy+sheepBoundsy && direction == 2){
sheepx = sheepx - 50;
}
if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
&& yposR < sheepy+sheepBoundsy && direction == 3){
sheepy = sheepy - 50;
}
if (sheepx <= 20){
direction = directionNumbersLeft[x];
}
if (sheepy <= 20){
direction = directionNumbersUp[x];
}
if (sheepx >= 600){
direction = directionNumbersRight[x];
}
if (sheepy >= 600){
direction = directionNumbersDown[x];
}
if (xposR < sheepx){
direction = directionNumbersLeft[x];
}
if (xposR > sheepx){
direction = directionNumbersRight[x];
}
if (yposR > sheepy){
direction = directionNumbersDown[x];
}
if (yposR < sheepy){
direction = directionNumbersUp[x];
}
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}
SheepDog.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class SheepDog extends JApplet
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
MainPanel mainPanel;
public void init()
{
mainPanel = new MainPanel();
add(mainPanel);
}
}
class Dog
{
int xpos;
int ypos;
int circleWidth = 30;
int circleHeight = 30;
public Dog(int x, int y)
{
xpos = x;
ypos = y;
}
public void setLocation(int lx, int ly)
{
xpos = lx;
ypos = ly;
}
public void display(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(xpos, ypos, circleWidth, circleHeight);
}
}
class Sheep
{
int xpos;
int ypos;
int circleWidth = 30;
int circleHeight = 30;
public Sheep(int x, int y)
{
xpos = x;
ypos = y;
}
public void setLocation(int lx, int ly)
{
xpos = lx;
ypos = ly;
}
public void display(Graphics g)
{
g.setColor(Color.green);
g.fillOval(xpos , ypos, circleWidth, circleHeight);
g.drawOval(xpos - 10, ypos - 10, 50, 50);
}
}
最佳答案
尝试将 else 添加到长串 if() 语句中,如下所示:
if (sheepx <= 20){
direction = directionNumbersLeft[x];
} else if (sheepy <= 20){
direction = directionNumbersUp[x];
} else if (sheepx >= 600){
direction = directionNumbersRight[x];
} else ...
按照你之前的方式,如果羊 x 剩下 20 个,并且小于狗的 x,它仍然会远离狗,即使它应该担心边界。这是因为两个 if 语句都会触发并修改 direction
,但由于您首先检查边界,因此尝试从狗身上逃跑会覆盖该边界。尝试这个解决方案,告诉我它是如何工作的
关于java - 移动物体不会停留在设定的范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12273164/
大家好,本文实现了相机碰撞检测,使相机不穿墙壁、物体,并给出了思路和代码,感谢大家~ 关键词:数字孪生、three.js、Web3D、WebGL、相机碰撞、游戏相机 我正在承接Web3D数字孪生项
我有一个大小为 12*30 的字符串的图像。我想创建一个动画,让它给人一种拉伸(stretch)字符串的感觉。我通过缩放图像来做到这一点,但我面临的问题是缩放图像没有发生碰撞。它仅出现在原始图像大小的
我的对象列表是如此初始化: $( function() { var $container = $('div.hikashop_products'); $container.isotop
我听说 swing 默认情况下是双缓冲的。我不想让 Swing 双缓冲。我正在使用双缓冲,我想添加一些 Swing 对象(现在只是添加到 JPanel 中的 JButton,然后再添加到 JFrame
几天来我一直在思考最好的解决方案,但似乎无法找到正确的想法。 我有一 block (物体),我想将它们放入尽可能小的空间中。我最终寻找的是这样的东西 http://i.stack.imgur.com/
我的纹理不仅仅是一个盒子或圆形,我的 body 需要与这个形状相同,所以我想结合多个 body 来达到我想要的形状,这甚至可能吗?或者有更好的方法吗?我正在使用带有 libgdx 框架的 java。
我遇到的情况是,我有很多计算机并且需要有唯一的 ID。 他们会通过 API 请求发送其 uniqueId。该对象看起来像 class ID { long timestamp; int id; }
我正在尝试检测一张卡片,但问题是有时图像不好并且有多个背景,如下所示: 没有很好地定义边缘 ![没有很好地定义边缘][1] 示例背景 ![示例背景][2] 我这样做了: gray = cv2.cvtC
我正在尝试从仅包含一辆车和简单背景的图像中分割汽车,如 但是我从我的实现中得到的是这个 和 分别 但它非常容易处理几乎已经分割的图像,例如。 给出类似 的结果 我使用的代码是 import cv2 i
我正在开发一个项目,在该项目中我从另一个对象/函数中引用一个变量。然而我总是返回 false。我不确定我是否正确调用它。 这是验证函数: app.validation = function(){
数组只是伪装的对象吗?为什么/为什么不呢?他们以什么方式(这样/不是)? 我一直认为 JS 中的数组和对象本质上是相同的,主要是因为访问它们是相同的。 var obj = {'I': 'me'}; v
我正在使用 PlayN 构建一个涉及石头的游戏,用户必须在物理世界中移动(通过重力等)。我希望用户能够使用触摸板直接操纵石头,并通过以下方式给它们一个速度拖拽并扔掷它们。 现在我有一个实现,其中每个石
http://jsfiddle.net/goldrunt/jGL84/42/这是来自这个 JS fiddle 的第 84 行。通过取消注释第 141-146 行,可以对球应用 3 种不同的效果。 'b
我学习Linux平台下的OpenGL。最近,我尝试使用 glutBitmapCharacter() 创建的文本作为 glu 或 glut 提供的一些二次对象的纹理。但是,glutBitmapChara
我正在使用 AndEngine 创建一个带有 box2d 扩展名的游戏。我想实现一个条件,当两个物体碰撞时,它们应该被移除或重生。最初我尝试使用 if(sprite1.collidesWith(spr
我制作了一个小型 3d 引擎。 但我在旋转功能方面遇到了一些问题。它们使物体不时拉伸(stretch)。这是数学: this.rotateX = function(angle) { var c
我在 Canvas 上制作了一个矩形 mask ,我需要 mask 外的任何东西都具有 0.8 的不透明度,因此 mask 外的所有对象都被视为不透明请看一下 fiddle 。 http://jsfi
我是 Direct3D 的新手,我在一个项目中从网络摄像头拍照并在它前面绘制一些 3D 对象。 我能够使用正交投影将网络摄像头图像渲染为背景。 //init matrix D3DXMatri
我是一名优秀的程序员,十分优秀!