Moving Squares Freeze Tag - java

For my project we have to have four square moving and bouncing off the sides and when it hits a side it changes color. When you click on one of the squares it freezes and turns red. So far i have the four squares and when I click them it says it freezes them. However, I'm not positive how to get them to move and bounce off the sides. Below is the code i have written so far.
MAIN:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StationarySquare[] squares = new StationarySquare[4];
Random rng = new Random();
for (int i = 0; i < squares.length; i++) {
squares[i] = new StationarySquare(rng.nextDouble(), rng.nextDouble());
}
while (true) {
StdDraw.clear();
int count = 0;
for (int i = 0; i < squares.length; i++) {
squares[i].draw();
if (StdDraw.mousePressed()
&& squares[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
squares[i].freeze();
}
if (squares[i].isFrozen()) {
count++;
}
}
StdDraw.text(0.5, 0.5, "Frozen: " + count);
StdDraw.show(25);
}
}
}
My other class
import java.util.Random;
public class StationarySquare {
private double x;
private double y;
private double halfLength;
private int red;
private int green;
private int blue;
private boolean isFrozen;
public StationarySquare(double x, double y) {
this.x = x;
this.y = y;
halfLength = 0.1;
isFrozen = false;
randomColor();
}
public void draw() {
if (isFrozen) {
StdDraw.setPenColor(StdDraw.RED);
} else {
StdDraw.setPenColor(red, green, blue);
}
StdDraw.filledSquare(x, y, halfLength);
}
public void randomColor() {
Random rng = new Random();
red = rng.nextInt(256);
green = rng.nextInt(256);
blue = rng.nextInt(256);
}
public void freeze() {
isFrozen = true;
}
public boolean containsPoint(double a, double b) {
return a > x - halfLength &&
a < x + halfLength &&
b > y - halfLength &&
b < y + halfLength;
}
public boolean isFrozen() {
return isFrozen;
}
}

Related

Simple perceptron model doesn't give wanted results

I have made a simple perceptron model with java. For visualization I use processing to see results.
Basically, the perceptron gives either negative or positive 1, depending on the location of the given position (float array with 2 values x and y). It is supposed to learn, if a given position is above or below a line.
My code worked fine with a formula of y=x, but has troubles when getting a formula like y=height-x
I tried tweaking the learningStrength, the number of dots it has to guess, changing the bias and line formula.
I can't figure out why it works with the formula y=x, but not with any other...
Class node (Perceptron):
public class Node {
float[] weights;
PApplet pApplet;
float trainingStrength = 0.01f;
public Node(int inputs, PApplet pApplet){
this.weights = new float[inputs + 1];
this.pApplet = pApplet;
for (int i = 0; i < inputs; i++){
this.weights[i] = pApplet.random(1);
}
}
private float activation(float sum){
return Math.signum(sum);
}
public float guess(float[] inputs){
float[] inputsB = addBias(inputs);
if (inputsB.length != weights.length){
return 0;
}
float sum = 0;
for (int i = 0; i < inputsB.length; i++){
sum += inputsB[i] * weights[i];
}
return activation(sum);
}
public void tweak(float[] inputs, int error){
float[] inputsB = addBias(inputs);
for(int i = 0; i < weights.length; i++){
weights[i] += error * inputsB[i] * trainingStrength;
}
}
public float guessY(int x){
float w0 = weights[0];
float w1 = weights[1];
float w2 = weights[2];
return -(w2/w1) - (w0/w1) * x;
}
private float[] addBias(float[] inputs){
float[] inputsB = new float[inputs.length + 1];
for (int i = 0; i < inputs.length; i++) {
inputsB[i] = inputs[i];
if (i + 1 >= inputs.length){
// bias = 1
inputsB[i + 1] = 1;
}
}
return inputsB;
}
}
Class Dot:
public class Dot {
PVector position;
PApplet pApplet;
int answer;
boolean correct;
public Dot(PVector position, PApplet pApplet){
this.position = position;
this.pApplet = pApplet;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public int getAnswer() {
return answer;
}
public PVector getPosition() {
return position;
}
public void guessed(boolean correct) {
this.correct = correct;
}
public void show(){
pApplet.push();
if (!correct) {
pApplet.fill(255, 0, 0);
} else {
pApplet.fill(0,255,0);
}
pApplet.ellipse(position.x,position.y,20,20);
if (answer >= 0){
pApplet.fill(255);
} else {
pApplet.fill(0);
}
pApplet.ellipse(position.x,position.y,10,10);
pApplet.pop();
}
}
Main with processing sketch:
public class NeuralNetworkLibrary extends PApplet{
Node brain;
List<Dot> dots;
int nDots = 1000;
public void settings() {
size(1200,1200);
brain = new Node(2, this);
dots = new ArrayList<>();
for (int i = 0; i < nDots; i++){
dots.add(new Dot(new PVector(random(width), random(height)), this));
dots.get(i).setAnswer((int) Math.signum(formula((int) dots.get(i).getPosition().x) - dots.get(i).getPosition().y));
}
}
public void draw() {
frameRate(1);
background(120);
strokeWeight(1);
for (Dot dot : dots){
int guess = (int) brain.guess(new float[]{dot.getPosition().x,dot.getPosition().y});
int error = dot.getAnswer() - guess;
boolean correct = false;
if (error == 0){
correct = true;
} else {
brain.tweak(new float[]{dot.getPosition().x,dot.getPosition().y}, error);
}
dot.guessed(correct);
dot.show();
}
strokeWeight(5);
line(0, formula(0), width, formula(width));
line(0,brain.guessY(0),width,brain.guessY(width));
}
public int formula(int x){
return x + height / 2;
}
public static void main(String[] args) {
PApplet.main(NeuralNetworkLibrary.class);
}
}

Java - counting the occurrences of a character in a random generator method using a separate method

I'm trying to figure out how to count the number of stars that were printed last time the print() method was used.
I'm confused on how to take the value of starsInLastPrint variable into the starsInLastPrint() method. My understanding is that this isn't possible. I assume there are plenty of things wrong with my current code that isn't helping. Below is my current state as I am stuck.
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
NightSky nightSky = new NightSky(this.density, this.width, this.height);
this.starsInLastPrint = 0;
int i = 0;
while (i < this.height) {
nightSky.printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
You are on the right track. Though, you don't need to instantiate another NightSky object inside the print method. You can just do the following,
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
So, everytime you call print, it will update the stars count for that print method call. Here is the whole code,
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public static void main(String[] args){
NightSky sky = new NightSky(5,5);
sky.print();
System.out.println(sky.starsInLastPrint());
sky.print();
System.out.println(sky.starsInLastPrint());
}
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
Sample run of the above code:
* *
*
*
4
0

Recursive Collision Detection for Rectangles not quite working

I have a bunch of variable sized rectangles, which are laid out randomly in an area.
I am attempting to do (recursive) collision detection that ensures that they do not collide, by shifting their positions.
But, something is still wrong (some of the rectangles still collide), and I can't figure out what.. probably my inability to do recursion right. I would be grateful if someone checked this out.
Here's the code, just copy & paste & run it, and you'll see the result instantly. Requires JavaFx:
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.*;
public class Example extends Application {
public static void main(String[] args) {
launch(args);
}
private Pane root = new Pane();
#Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(root, 800, 600);
stage.setTitle("Collision Problem");
stage.setScene(scene);
stage.setOnCloseRequest(e -> System.exit(0));
stage.show();
run();
}
private static class Node2D {
private double x, y, w, h;
public Node2D() {
w = randInt(40, 80);
h = randInt(20, 40);
}
public void setPosition(double x, double y) {
this.x = x;
this.y = y;
}
public double getWidth() {
return w;
}
public double getHeight() {
return h;
}
}
private static class LayoutEntity {
private Node2D obj = new Node2D();
private double x, y;
public void setLocationInLayout(double x, double y) {
this.x = x;
this.y = y;
}
public double getXInLayout() {
return x;
}
public double getYInLayout() {
return y;
}
}
public void run() {
LayoutEntity[] entitiesToLayout = new LayoutEntity[100];
for (int i = 0; i < entitiesToLayout.length; i++)
entitiesToLayout[i] = new LayoutEntity();
randomizePositions(entitiesToLayout);
collisionDetection(entitiesToLayout);
for (LayoutEntity entity : entitiesToLayout) {
Node2D node = entity.obj;
node.setPosition(entity.getXInLayout(), entity.getYInLayout());
}
// Print possible collisions
displayNodes(entitiesToLayout);
}
private void displayNodes(LayoutEntity[] all) {
for (LayoutEntity entity : all) {
Node2D node = entity.obj;
Rectangle rect = new Rectangle(node.x, node.y, node.w, node.h);
rect.setStroke(Color.BLACK);
rect.setFill(null);
root.getChildren().add(rect);
}
}
private void randomizePositions(LayoutEntity[] entities) {
for (LayoutEntity entity : entities) {
entity.setLocationInLayout(randInt(0, 512), randInt(0, 512));
}
}
private void collisionDetection(LayoutEntity[] entities) {
collisionDetection(Arrays.asList(entities));
}
private void collisionDetection(Collection<LayoutEntity> c) {
List<LayoutEntity> collisions = new ArrayList<>();
for (LayoutEntity e1 : c) {
for (LayoutEntity e2 : c) {
if (e1 == e2)
continue;
boolean collides = checkAndResolveCollision(e1, e2);
if (collides)
collisions.add(e1);
}
}
checkRecursively(collisions, c);
}
private void checkRecursively(List<LayoutEntity> collisions,
Collection<LayoutEntity> all) {
if (collisions.isEmpty())
return;
for (LayoutEntity e1 : all) {
for (int i = 0; i < collisions.size(); i++) {
LayoutEntity e2 = collisions.get(i);
if (e2 == e1)
continue;
boolean collides = checkAndResolveCollision(e1, e2);
if (collides) {
if (collisions.contains(e1))
continue;
collisions.add(e1);
checkRecursively(collisions, all);
}
}
}
}
private boolean checkAndResolveCollision(LayoutEntity e1, LayoutEntity e2) {
Node2D n1 = e1.obj;
// Ideally, I also want to add a gap around the boxes
double extraSpace = 0;
double w1 = n1.getWidth() + extraSpace * 2;
double h1 = n1.getHeight() + extraSpace * 2;
Rectangle2D b1 = new Rectangle2D(e1.getXInLayout() - extraSpace,
e1.getYInLayout() - extraSpace, w1, h1);
Node2D n2 = e2.obj;
double w2 = n2.getWidth() + extraSpace * 2;
double h2 = n2.getHeight() + extraSpace * 2;
Rectangle2D b2 = new Rectangle2D(e2.getXInLayout() - extraSpace,
e2.getYInLayout() - extraSpace, w2, h2);
if (b1.intersects(b2)) {
Point2D trans = getMinimumTranslation(b1, b2);
double x = e1.getXInLayout() + trans.getX();
double y = e1.getYInLayout() + trans.getY();
e1.setLocationInLayout(x, y);
return true;
}
return false;
}
private Point2D getMinimumTranslation(Rectangle2D source, Rectangle2D target) {
double mtdx, mtdy;
Point2D amin = new Point2D(source.getMinX(), source.getMinY());
Point2D amax = new Point2D(source.getMaxX(), source.getMaxY());
Point2D bmin = new Point2D(target.getMinX(), target.getMinY());
Point2D bmax = new Point2D(target.getMaxX(), target.getMaxY());
double left = (bmin.getX() - amax.getX());
double right = (bmax.getX() - amin.getX());
double top = (bmin.getY() - amax.getY());
double bottom = (bmax.getY() - amin.getY());
if (left > 0 || right < 0)
return Point2D.ZERO;
if (top > 0 || bottom < 0)
return Point2D.ZERO;
if (Math.abs(left) < right)
mtdx = left;
else
mtdx = right;
if (Math.abs(top) < bottom)
mtdy = top;
else
mtdy = bottom;
// zero the axis with the largest mtd value.
if (Math.abs(mtdx) < Math.abs(mtdy))
mtdy = 0;
else
mtdx = 0;
return new Point2D(mtdx, mtdy);
}
private static Random rand = new Random();
public static int randInt(int min, int max) {
return rand.nextInt((max - min) + 1) + min;
}
}
From reading your code, what I can predict is, the problem is in the logic. Inside checkAndResolveCollision(), when you assigning a new co-ordinate to the Node using
e1.setLocationInLayout(x, y);
you miss to check whether the new co-ordinate that you have assigned to this node, overlaps any of the other node's which we have already checked against this one.
You will have to generate the logic, as to whenever you are changing the co-ordinate of a Node, it must again be checked against every other Node
Hope it helps

2d array of objects

im really new in Java. I just need to explain how to declare 2D array of objects, i have something like:
package breakout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
public class Breakout extends JPanel {
public class Ball {
private
int x = 400;
int y = 300;
int speed = 2;
int dirx = 1;
int diry = -1;
public
void bounce(int px, int py, int lx, int ly) {
if ((x + 10 >= 800 && dirx == 1) || (x <= 0 && dirx == -1))
dirx *= -1;
if (y <= 0 && diry == -1)
diry *= -1;
if (y + 10 >= py && y <= py + ly && diry == 1 && x + 10 >= px && x <= px + lx)
diry *= -1;
}
int getX() {
return x;
}
int getY() {
return y;
}
void setDirx(){
dirx *= -1;
}
void setDiry(){
diry *= -1;
}
void move() {
x += speed*dirx;
y += speed*diry;
}
void paint(Graphics2D g) {
g.fillOval(x,y,10,10);
}
}
public class Paddle {
private
int x = 400;
int y = 520;
int width = 100;
int height = 6;
int speed = 6;
int dirL = 0;
int dirR = 0;
public
void move() {
x -= speed*dirL;
x += speed*dirR;
}
void stop() {
if (x <= 0)
x = 0;
if (x + width >= 800)
x = 800 - width;
}
int getX() {
return x;
}
int getY() {
return y;
}
int getWidth() {
return width;
}
int getHeight() {
return height;
}
void paint(Graphics2D g) {
g.fillRect(x,y,width,height);
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
dirL = 0;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
dirR = 0;
else {
dirL = 0;
dirR = 0;
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
dirL = 1;
break;
case KeyEvent.VK_RIGHT:
dirR = 1;
break;
}
}
}
public class Brick {
private
int x;
int y;
int width;
int height;
boolean alive;
boolean inX = false,inY = false;
public
void setUpBrick(int px, int py, int w, int h, boolean al) {
x = px;
y = py;
width = w;
height = h;
alive = al;
}
void setAlive(boolean alive) {
this.alive = alive;
}
void paint(Graphics2D g) {
if (alive)
g.fillRect(x,y,width,height);
}
boolean collision(int bx, int by) {
if (alive) {
if (bx + 10 >= x && bx <= x + width && by + 10 >= y && by <= y + height) {
setAlive(false);
return true;
} else return false;
}
else return false;
}
void inAreaX(int bx) {
if (bx + 10 >= x && bx <= x + width) {
System.out.println("inAreaX");
inX = true;
} else {
inX = false;
}
}
void inAreaY(int by) {
if (by + 10 >= y && by <= y + height) {
System.out.println("inAreaY");
inY = true;
} else {
inY = false;
}
}
boolean isInAreaX () {
if (inX)
return true;
else return false;
}
boolean isInAreaY () {
if (inY)
return true;
else return false;
}
}
Ball ball = new Ball();
Paddle paddle = new Paddle();
Brick[][] brick = new Brick[8][4];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 4; j++) {
brick[i][j].setUpBrick(j * 101, i * 51, 100, 50, true);
}
}
void bounce() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 4; j++) {
brick[i][j].inAreaX(ball.getX());
brick[i][j].inAreaY(ball.getY());
if (brick[i][j].collision(ball.getX(), ball.getY())) {
if (brick[i][j].isInAreaX()) {
ball.setDiry();
} else if (brick[i][j].isInAreaY()) {
ball.setDirx();
}
}
}
}
}
void move() {
ball.bounce(paddle.getX(), paddle.getY(), paddle.getWidth(),paddle.getHeight());
ball.move();
paddle.move();
paddle.stop();
bounce();
}
public Breakout() {
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
paddle.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
paddle.keyPressed(e);
}
});
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
ball.paint(g2d);
g2d.setColor(Color.BLACK);
paddle.paint(g2d);
g2d.setColor(Color.ORANGE);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 4; j++)
brick[i][j].paint(g2d);
}
public static void main(String[] args) throws InterruptedException {
JFrame window = new JFrame("Tennis");
Breakout game = new Breakout();
window.add(game);
window.setSize(800,600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
i need to inicialize 2d array of brick, but it says that first for is unexpected token. How to write it? Thank you.
Unless if I have miscounted your opening and closing braces, your for loop is not inside any method, it's directly in your class body. That's why you're getting unexpected token. You will probably want to move it into the Breakout constructor.
In order to create a 2D array in Java, you can create such an array like this:
Object testArray = new Object[10][10];
This array is now a 10*10 array with memory allocated for 100 Object references.
You can create pointers two Objects with a double for-loop:
for (int i = 0; i < testArray.length(); i++) {
for (int j = 0; j < testArray.length; j++) {
testArray[i][j] = Object; //here you input the objects that you want to point to
}
}
Move the logic from setUpBrick to a constructor.
public class Brick {
private int x;
private int y;
private int width;
private int height;
private boolean alive;
private boolean inX = false,inY = false;
public Brick(int px, int py, int w, int h, boolean al) {
x = px;
y = py;
width = w;
height = h;
alive = al;
}
...
}
Then change
brick[i][j].setUpBrick(j * 101, i * 51, 100, 50, true);
to
brick[i][j] = new Brick(j*101, i*51, 100, 50, true);
Also note that access modifiers don't apply to a whole section of your class. In your example,
private
int x;
int y;
int width;
int height;
boolean alive;
boolean inX = false,inY = false;
means that only x is going to be private. The rest of the members will get the default access modifier.
One more tip. A couple of your methods can be simplified.
boolean isInAreaY () {
if (inY)
return true;
else return false;
}
can be written as:
boolean isInAreaY() {
return inY;
}

Java both if() and else if() statements are being called

Okay, so I'm in the progress of making a game, and I need the collisions to work. I have an if() { } else if() {} -statement, but BOTH of them are being called. Here is my code:
Inside my Player Class:
public Rectangle[] tileRect;
public void update() {
tileRect = new Rectangle[Level1.tiles.size()];
for (int w = 0; w < Level1.tiles.size(); w++) {
Tile m = (Tile) Level1.tiles.get(w);
tileRect[w] = m.getBounds();
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
}
x += dx;
y += dy;
}
public Rectangle getRect() {
return new Rectangle(x, y, 32, 32);
}
Here is my Tile class (the Level1.tiles is an arrayList of tiles):
package level;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Tile extends Rectangle {
// Defines the id of the tile, used for setting textures.
private static final long serialVersionUID = 1L;
public int id;
// Location
public int x;
public int y;
// Variables for the terrain sprite sheet.
public BufferedImage image;
public String imageLocation;
public BufferedImage[] sprite;
public int rows = 16;
public int collumns = 16;
public int width = 16;
public int height = 16;
public Tile(int idVal, int xPos, int yPos) {
x = xPos * 32;
y = yPos * 32;
id = idVal;
setBounds(x, y, 32, 32);
createImages();
}
public BufferedImage getImage() {
return sprite[id];
}
public void createImages() {
imageLocation = "res/tile/terrain.png";
try {
image = ImageIO.read(new File(imageLocation));
} catch (IOException e) {
System.out.println("Unable to find file " + imageLocation
+ ", printing stack trace!");
e.printStackTrace();
}
sprite = new BufferedImage[rows * collumns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < collumns; j++) {
sprite[(i * collumns) + j] = image.getSubimage(j * width, i
* height, width, height);
}
}
}
public int getXLoc() {
return x;
}
public int getYLoc() {
return y;
}
public void setX(int xPos) {
x = xPos;
}
public void setY(int yPos) {
y = yPos;
}
}
I'm getting the "intersecting" message in the console, but the player is still falling down (because dy = 4). Please help! I've been trying to solve this all morning...
If and Else cannot both be caught at the same time.
Looks like you are looping through, seeing an intersection, then continuing to loop through regardless.
Try adding a break command to your for loop.
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
break;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
The break will stop your for loop from continuing and you will exit the loop with dy = 0; rather than going onto the next tile and changing it back to dy = 4;
if and else if cannot be called in the same run. If if condition is true, then any else is never run (not even checked). It's probably different runs. Debug or put a log to see the flow.
Also
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
is much simpler written as
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else {
dy = 4;
}

Categories

Resources