【问题标题】:How do I check an array against itself in java [closed]java - 如何在java中检查数组自身
【发布时间】:2012-10-13 04:41:04
【问题描述】:

我正在尝试在 java 中创建一个基本的落沙游戏,我让每个粒子将它的 X 和 Y 存储在一个 Point 数组中。每个点数组都特定于它的元素,所以沙子和水是两个不同的数组,我想知道的是如何检查我的沙子数组以查看两个粒子是否具有相同的位置,如果是,移动它们?

这是我的沙课

    package testingzone;

import java.awt.Point;
import java.util.Stack;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Sand {

    public Graphics g = new Graphics();
    public float sizeX = 1;
    public float sizeY = 1;
    public int speed = 1;
    public String BaseState = "fall";
    public String state = "fall";
    public String Name = "Sand";
    public Point[] point = new Point[(600 * 800)];
    public Point[] pointcheck = new Point[(600 * 800)];
    public int num = 0;
    public boolean check = false;

    public org.newdawn.slick.Color c = org.newdawn.slick.Color.yellow;

    public void drawParticle(float x, float y) throws SlickException{
        g.setColor(c);
        g.drawRect(x, y, sizeX, sizeY);
    }

    public void createParticle() throws SlickException{
        if(state == "fall"){
            fall(point);
        } else {

        }
        if(state == "stay"){
            stay(point);
        } else {

        }
    }

    public void fall(Point[] point) throws SlickException{
        for (int index = 0; index < point.length; index++) {
            Point p = point[index];
            if (p != null) {
                if (p.y >= 598) {
                    drawParticle(point[index].x, 598);
                } else {
                    drawParticle(p.x, p.y);
                    p.y += speed;
                }
              }
            }
          }

    public void stay(Point[] point) throws SlickException{
        for (int index = 0; index < point.length; index++) {
            Point p = point[index];
            if (p != null) {
                drawParticle(p.x, p.y);
                }
              }
            }

}

这是我的主要课程的简化版本

    package testingzone;
import java.awt.Point;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
    public static final int ID = 2;

    public Sand s = new Sand();

    public Run run = new Run();
    int maxnum = (800 * 600);
    int pressedX;
    int pressedY;
    int num = 0;
    String Build = "1.4";
    String Element = "sand";
    String name;
    public boolean endgame = false;


    public void init(GameContainer container, StateBasedGame game) throws SlickException{
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        if(Element == "sand"){
            s.createParticle();
            name = s.Name;
        }
        g.setColor(Color.yellow);
        g.drawRect(240, 10, 15, 15);
        g.fillRect(240, 10, 15, 15);
        if(Element == "sand"){
            // g.setColor(Color.white);
            // g.drawRect(235, 5, 25, 25);
            g.drawLine(235, 35, 260, 35);
        }
        g.setColor(Color.white);
        g.drawString("Open Sand", 700, 0);
        g.drawString("Build: " + Build, 700, 15);
        g.drawString("Total Pixels: " + num, 10, 25);
        g.drawString("Current Type: " + name, 10, 40);
        g.drawString("Mouse X: " + Mouse.getX(), 10, 55);
        g.drawString("Mouse Y: " + Mouse.getY(), 10, 70);
        }


    public void update(GameContainer container, StateBasedGame game, int delta) {
        if(endgame == true){
            container.exit();
        }
    }

    public void mouseDragged(int oldx, int oldy, int newx, int newy) {
        pressedX = newx;
        pressedY = newy;
        num = num + 1;
        if(Element == "sand"){
            s.num = s.num + 1;
            s.point[s.num] = new Point(pressedX, pressedY);
            s.pointcheck[s.num] = new Point(pressedX, pressedY);
        }
    }

    public void keyPressed(int key, char c) {
        if (key == Input.KEY_ESCAPE) {
            endgame = true;
        }
        if (key == Input.KEY_1) {
            Element = "sand";
    }

    public int getID() {
        return ID;
    }

}

【问题讨论】:

  • 这里没有足够的上下文让任何人弄清楚你在问什么。您需要提供一些代码和示例来说明您想要完成的任务,否则可能会被否决并被视为“不是一个真正的问题”而被关闭。

标签: java arrays slick2d particles


【解决方案1】:

一个建议: 粒子类有一个 Point(x-y 坐标)。 实现粒子的equals方法

boolean equals(Object o){
   if(! o instanceof Particle) return false;
   if(o.getPoint().equals(this.getPoint())) return true;

现在为 Point 类实现等于:

    boolean equals(Object o){
       if(! o instanceof Point) return false;
       if(o.getXCoordinate.equals(this.getXCoordinate()) 
&& o.getYCoordinate.equals(this.getYCoordinate())) return true;

您可以根据需要检查索引,而不是在 Point 类上使用 getXCoordinategetYCoordinate 方法。

【讨论】:

  • 因为 Point 是一个数组,我无法检查 X 和 Y。
  • @user1610541 所以有一个java.awat.Point的数组。您想比较该数组中的两个元素是否相同?它是否正确?如果是这样,Point 类提供了 equals 方法 docs.oracle.com/javase/6/docs/api/java/awt/Point.html
【解决方案2】:

我不确定我是否理解您的问题..
检查这是不是你想要的..

// These loops will make each of the points compare to each other..
Point sands[] = new Point[10];
for ( int i = 0; i < sands.length - 1; i++ ) {
    for ( int k = i + 1; k < sands.length; k ++ ) {
        if ( sands[i].equals(sands[k]) ) {
            // Move Them
        }
    }
}

【讨论】:

  • 我将它放入一个名为 check 的方法中,并在每次创建新粒子时运行 check 并告诉它根据结果绘制真或假。直到我点击它才会崩溃:L
  • 当它崩溃时,你得到任何堆栈跟踪吗?你能发布一些你认为可能是问题所在的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
相关资源
最近更新 更多