Tic Tac Toe game in Java using MouseAdapter - java

I have a CLickableBox class that creates boxes for me and now I need to make it so that when clicked, either an X or an O will be displayed in place. Here is the ClickableBox class.
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.Container;
public class ClickableBox extends MouseAdapter {
private int x, y, width, height;
private Color borderColor, backColor, oldColor;
private boolean drawBorder, clicked;
private Container parent;
public ClickableBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, Container parent) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.borderColor = borderColor;
this.backColor = backColor;
this.drawBorder = drawBorder;
this.parent = parent;
}
public void draw(Graphics g) {
oldColor = g.getColor();
g.setColor(backColor);
g.fillRect(x, y, width, height);
if(drawBorder) {
g.setColor(borderColor);
g.drawRect(x, y, width, height);
}
g.setColor(oldColor);
}
public void mouseReleased(MouseEvent e) {
if(x < e.getX() && e.getX() < x + width &&
y < e.getY() && e.getY() < y + height) {
clicked = true;
parent.repaint();
}
}
public boolean isClicked() {
return clicked;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Color getBorderColor() {
return borderColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public Color getBackColor() {
return backColor;
}
public void setBackColor(Color backColor) {
this.backColor = backColor;
}
public Color getOldColor() {
return oldColor;
}
public void setOldColor(Color oldColor) {
this.oldColor = oldColor;
}
public boolean isDrawBorder() {
return drawBorder;
}
public void setDrawBorder(boolean drawBorder) {
this.drawBorder = drawBorder;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
The TicTacToeBox class should extend ClickableBox, so that each box will be a listener. It needs to be designed so that each Box object will take care of itself- it knows if it's been clicked or not, and if so, whether it's going to be showing an x or an o.
The TicTacToeBox class is what I am having trouble with. This is what I will need for my game board. Any suggestions on how to implement this, simply? Below is my TicTacToeBox so far (not much):
Some direction and/or assistance would be greatly appreciated! Thanks.
import java.awt.Color;
import java.awt.Container;
public class TicTacToeBox extends ClickableBox {
public TicTacToeBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, boolean mask, Container parent)
{
super(x, y, width, height, borderColor, backColor, drawBorder, parent);
}
}

Perhaps you need to override mouseReleased() - something like this:
public void mouseReleased(MouseEvent e) {
if ( this.value == NONE ) {
if ( currentTurn == Turn.X ) {
this.value = X;
}
else {
this.value = O;
}
}
super.mouseReleased();
}
With some global currentTurn variable to keep track of whose turn it is, and a value field to represent what the current value of this box is. You'd also probably want to override draw() to make it render the "X" or "O"...

Related

mouseMoved not called while mousePressed has been called

My mouseMoved simply doesn't get called when mousePressed has been called, but it gets called normally when mousePressed didn't. If I move my mouse while I press a mouse button mouseMoved doesn't get called.
package src.game.main.gui_hud;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.SwingUtilities;
import src.game.main.Game;
public class Slider {
private Color lc,hc,fc;
private int x,y;
private int w,h;
private Runnable change;
private int lineY;
private double value = 100;
private volatile boolean canMove;
public Slider(Color bgColor,Color filledColor,Color handlerColor,Runnable onValueChange,int x,int y,int w,int h,int lineY) {
setLc(bgColor);
setHc(handlerColor);
setFc(filledColor);
change = onValueChange;
this.x = x;
this.y = y;
this.w = w;
this.h = w;
this.lineY = lineY;
}
public void render(Graphics gt) {
Graphics2D g = (Graphics2D) gt.create(x, y, w, h);
g.setColor(getLc());
g.fillRoundRect(10, y/2-lineY, w-10, lineY, 10, 10);
g.setColor(getFc());
g.fillRoundRect(10, y/2-lineY, (int) ((value*w)/100)-10, lineY, 10, 10);
g.setColor(getHc());
g.fillRoundRect((int)((value*w)/100)-6, y/2-20, 5, 30, 10, 10);
}
public void tick() {
value = Game.clamp(value, 0, 100);
System.out.println(canMove);
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if (new RoundRectangle2D.Double(x+ ((int)((value*w)/100)-6), y + (y/2-20), 5, 30, 10, 10).contains(p)) {
canMove = SwingUtilities.isLeftMouseButton(e);
}
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
canMove = false;
}
public void mouseMoved(MouseEvent e) {
System.out.println(e.getX());
Point p = e.getPoint();
if(canMove) System.out.println("LOL");
}
public Color getHc() {
return hc;
}
public Slider setHc(Color hc) {
this.hc = hc;
return this;
}
public Color getLc() {
return lc;
}
public Slider setLc(Color lc) {
this.lc = lc;
return this;
}
public int getX() {
return x;
}
public Slider setX(int x) {
this.x = x;
return this;
}
public int getY() {
return y;
}
public Slider setY(int y) {
this.y = y;
return this;
}
public int getW() {
return w;
}
public Slider setW(int w) {
this.w = w;
return this;
}
public int getH() {
return h;
}
public Slider setH(int h) {
this.h = h;
return this;
}
public double getValue() {
return value;
}
public Slider setValue(double v) {
this.value = v;
return this;
}
public Color getFc() {
return fc;
}
public Slider setFc(Color fc) {
this.fc = fc;
return this;
}
Based on the code in the mousePressed handler, you're trying to do some sort of drag-rectangle, although I can't be certain.
The mouseMoved messages will keep happening until you get a mousePressed event. Once you've received the mousePressed event you will then start to receive mouseDragged events until the receipt of a mouseReleased event. After that point you will start to receive mouseMoved events again.
This is intended to allow differentiating between just moving the mouse and dragging with one of the buttons pressed.

LibGDX Flip 2D Sprite Animation

I have an animation that I want to play flipped if the user presses the 'A' key on their keyboard. The animation is facing right in the file and is played normally until I try to flip the texture.
This is what happens when I try to flip the texture when the player's direction has changed:
As you can see, the animation plays fine in the beginning but when I change the player's direction it alternates between the flipped frame and the unflipped frame.
This is my player class:
package unit22.game;
import java.util.ArrayList;
import unit22.core.Utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Player {
private PlayerState state = PlayerState.STANDING;
private Direction direction = Direction.RIGHT;
private int x = 0, y = 0, width = 0, height = 0;
private PlayerInputProcessor inputProcessor;
private String name;
private ArrayList<Action> actions;
private Animation standingAnim;
private Animation movingAnim;
private float stateTime;
private SpriteBatch spriteBatch;
private Action currentAction;
private Animation currentAnimation;
public Player(String name, int x, int y, int width, int height) {
this.name = name;
setBounds(x, y, width, height);
if(getX() > 400) setDirection(Direction.LEFT);
this.actions = new ArrayList<Action>();
standingAnim = new Animation(0.06f, Utils.loadTextureAtlas("standing", "textures/characters/animations/" + name + "/").getRegions());
//movingAnim = new Animation(0.06f, Utils.loadTextureAtlas("moving", "textures/characters/animations/" + name + "/").getRegions());
stateTime = 0f;
spriteBatch = new SpriteBatch();
}
public void update() {
stateTime += Gdx.graphics.getDeltaTime();
switch(state) {
case STANDING:
if(currentAnimation != standingAnim)
currentAnimation = standingAnim;
break;
case MOVING:
if(currentAnimation != movingAnim)
currentAnimation = movingAnim;
break;
case ACTING:
Animation anim = new Animation(0.06f, Utils.loadTextureAtlas(currentAction.getName(), "textures/characters/animations/" + getName() + "/").getRegions());
if(currentAnimation != anim)
currentAnimation = anim;
break;
}
}
public void render() {
TextureRegion currentFrame = currentAnimation.getKeyFrame(stateTime, true);
if(getDirection() == Direction.LEFT) {
currentFrame.flip(true, false);
}
System.out.println("Direction: " + direction + ", Flipped: " + currentFrame.isFlipX());
spriteBatch.begin();
spriteBatch.draw(currentFrame, x, y, width, height);
spriteBatch.end();
}
public ArrayList<Action> getActions() {
return actions;
}
public void addAction(Action action) {
this.actions.add(action);
}
public void setActions(ArrayList<Action> actions) {
this.actions = actions;
}
public void setInputProcessor(PlayerInputProcessor inputProcessor) {
this.inputProcessor = inputProcessor;
}
public PlayerInputProcessor getInputProcessor() {
return inputProcessor;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Direction getDirection() {
return direction;
}
public PlayerState getState() {
return state;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public void setState(PlayerState state) {
this.state = state;
}
public int[] getBounds() {
return new int[] {x, y, width, height};
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setBounds(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
In my render method I am checking whether the player's direction is left and if it is then to flip the current animation frame.
This is how I am handling my input:
package unit22.screens;
import unit22.core.Game;
import unit22.core.Screen;
import unit22.game.Direction;
import unit22.game.Player;
import unit22.game.PlayerInputProcessor;
import com.badlogic.gdx.Gdx;
public class Playing extends Screen {
Player player;
public Playing(Game game, String name) {
super(game, name);
}
#Override
public void init() {
player = new Player("misaka", 100, 100, 188, 380);
player.setInputProcessor(new PlayerInputProcessor(player) {
#Override
public boolean keyTyped(char character) {
if(getPlayer().getX() <= 14) getPlayer().setX(15);
if(getPlayer().getX() + getPlayer().getWidth() >= 1024 - getPlayer().getWidth()) getPlayer().setX(1024 - getPlayer().getWidth() - 15);
if(character == 'a' || character == 'A') {
getPlayer().setX((int)(getPlayer().getX() - (900 * Gdx.graphics.getDeltaTime())));
if(getPlayer().getDirection() == Direction.RIGHT) {
getPlayer().setDirection(Direction.LEFT);
}
}
if(character == 'd' || character == 'D') {
getPlayer().setX((int)(getPlayer().getX() + (900 * Gdx.graphics.getDeltaTime())));
if(getPlayer().getDirection() == Direction.LEFT) {
getPlayer().setDirection(Direction.RIGHT);
}
}
return super.keyTyped(character);
}
});
getInputHandle().addProcessor(player.getInputProcessor());
}
#Override
public void render(float delta) {
super.render(delta);
player.update();
player.render();
}
}
I've been trying to figure this problem out for a couple of hours now and haven't made any progress. Any idea why this would be happening?
The problem is here:
if(getDirection() == Direction.LEFT) {
currentFrame.flip(true, false);
}
The flip method permanently flips the original TextureRegion that the Animation class is referencing. So you either need to make sure you flip it back each time after flipping and drawing it, or do this, which I think is simpler:
Never flip it but instead use a negative width when drawing it with SpriteBatch, like this:
boolean flip = (getDirection() == Direction.LEFT);
spriteBatch.draw(currentFrame, flip ? x+width : x, y, flip ? -width : width, height);
If somebody is still wondering, negating scaleX or scaleY works like a charm and does not hurt performance (like TextureRegion.flip does for example).
Example:
draw(region, x, y, originX, originY, width, float height,
(flip ? -1 : 1) * scaleX, float scaleY, float rotation);
I know that question is old but I think it is better solution
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
if(!currentFrame.isFlipX())
currentFrame.flip(true, false);
batch.draw(currentFrame, 0,0,0,0);
I flip my animation sequence use the code below(in kotlin):
if (this.position <= 0f)
{
this.speed = - this.speed
this.position = 0.0f
this.runAnimation.keyFrames.forEach {
it.flip(true, false)
}
}else if(this.position >= SCREEN_WIDTH / SCALE - width)
{
this.speed = - this.speed
this.position = SCREEN_WIDTH / SCALE - width
this.runAnimation.keyFrames.forEach {
it.flip(true, false)
}
}
this.game.batch.begin()
this.game.batch.draw(currentFrame, this.position, 40f)
this.game.batch.end()
Every time you flip the sequence, make sure loop every frame and flip every one:
this.runAnimation.keyFrames.forEach { it.flip(true, false) }
and remember that, the frames are flipped forever, so if you want to return back, you should flip again(flip = true)!

Java awt Why can I only remove most recent addition to Linked List

I'm creating a space shooter game in Java awt for my college computer science project.
The enemies that I have spawn every 3 seconds via a timer and are added to a LinkedList, and a for loop renders them all.
In the class I have for my player's bullet object, there are if statements to check whether the laser comes into the bounds of an enemy, and if they are all true it removes the enemy from the LinkedList.
However, only the most recent addition to the LinkedList is being removed; the bullet passes through the others and nothing happens. This is my first time making a game, and the first time I've ever used a LinkedList, so excuse any misunderstandings.
The controller class controls the enemies, the Laser class is the bullet and the Enemy class is the Enemy object. There's also a player, Main and GUI class.
import java.awt.*;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
public class Controller
{
private LinkedList<Enemy> e = new LinkedList<Enemy>();
Enemy tempEnemy, tempEnemy2
;
Main main;
int refreshSpawn = 3000; //move timer refresh rate
int xpos;
int width;
int ypos;
int height;
Timer spawnTimer = new Timer();
public Controller(Main main)
{
this.main = main;
spawn();
}
public void spawn()
{
spawnTimer.schedule(new TimerTask()
{
public void run() //run method and timer
{
addEnemy(new Enemy(main, (int)(Math.random()*4+2)));
}
}, 0, refreshSpawn);
}
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy = e.get(i);
xpos = tempEnemy.getX();
width = tempEnemy.getXsize();
ypos = tempEnemy.getY();
height = tempEnemy.getYsize();
tempEnemy.render(g);
}
}
public void update()
{
for(int i = 0; i < e.size(); i++)
{
tempEnemy2 = e.get(i);
tempEnemy2.move();
}
}
public void addEnemy(Enemy enemy)
{
e.add(enemy);
System.out.println(e.size());
//spawn();
}
public void removeEnemy()
{
e.remove(tempEnemy);
}
public int getX()
{
return xpos;
}
public int getY()
{
return ypos;
}
public int getXsize()
{
return width;
}
public int getYsize()
{
return height;
}
public Enemy getEnemy()
{
return tempEnemy;
}
}
import java.awt.*;
public class Enemy
{
Image ship; //image of enemy ship
int x, y; //ship position
int speed;
public Enemy(Main main, int speed) //constructing enemy
{
this.speed = speed;
ship = main.getImage(main.getDocumentBase(), "enemyShip"+(int)(Math.random()*6+1)+".png"); //picture for enemy ship
x = (int)(Math.random()*900+1); //enemy has a starting position at a random x point
y = -100; //start ship slightly off screen so it doesn't suddenly appear
}
public void move()
{
y += speed;
if(y > 600)
{
y = -100;
x = (int)(Math.random()*900);
}
}
public void render(Graphics g)
{
g.drawImage(ship, x, y, null);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXsize()
{
return ship.getWidth(null);
}
public int getYsize()
{
return ship.getHeight(null);
}
}
import java.awt.*;
public class Laser
{
Image img; //image of laser
int laserSpeed = 10; //speed of laser
int x, y; //position of laser
int xSize, ySize; //size of laser
Controller cont;
GUI gui;
public Laser(Image img, int x, int y, Controller cont, GUI gui) //constructing laser
{
this.cont = cont;
this.img = img; //setting laser image
this.gui = gui;
xSize = x; //size of laser
ySize = y; //size of laser
}
public void shoot(int x, int y, int shipSize)
{
this.x = x + (shipSize/2) - (xSize/2);
this.y = y;
}
public void move()
{
y -= laserSpeed;
if(x <= cont.getX() + cont.getXsize() && x + xSize >= cont.getX() - cont.getXsize())
{
if(y <= cont.getY() + cont.getYsize() && y > 0)
{
remove();
cont.removeEnemy();
gui.scoreUp(5);
}
}
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getXSize()
{
return xSize;
}
public int getYSize()
{
return ySize;
}
public Image getImage()
{
return img;
}
public void remove()
{
y = -ySize;
x = -100;
}
}
From what I can tell, tempEnemy is assigned to the last element in the LinkedList by the render method. This means that when you call removeEnemy it is removing the last rendered object (likely the last object you added).
What you should be doing is telling the Controller which Enemy it should be using, it has absolutely no idea what your intentions are when you call it...

Java swing, need to draw rectangles using the mouse without losing previous rectangles with overlap

in this class that i have extending JLabel I need to be able to use the mouse to left click, then drag down and/or right to create a rectangle and be able to repeat that process to draw multiple rectangles without losing any of the previous ones and drawing boxes for overlap as well as being able to find the rectangle made by the union of all rectangles like this
my current code was adapted as much as i could from the java demo on Performing Custom Painting the program seems to be behaving in odd ways because of how the repaint method is used to update the JLabel but i have no idea how to fix it
JLabel class
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class JLabelx extends JLabel {
private int squareX = 0;
private int squareY = 0;
private int squareW = 0;
private int squareH = 0;
public JLabelx() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
squareX = e.getX();
squareY = e.getY();
//set coordinates of next rectangle
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
newDraw(e.getX(),e.getY());
//find length and width of next rectangle
}
});
}
protected void newDraw(int x, int y) {
int OFFSET = 1;
if ((squareX!=x) || (squareY!=y)) {
// repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
squareW=x-squareX;
squareH=y-squareY;
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
}
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.GREEN);
g.fillRect(squareX,squareY,squareW,squareH);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,squareW,squareH);
}
}
I have also been given a Rectangle class that looks similar to java.awt.Rectangle which has methods that find the rectangles made by overlaps and the rectangles made by the union of all rectangles, but I don't know how to create rectangle objects with mouse movements and then paint them in this JLabel
public class Rectangle {
private int x,y,width,height;
public Rectangle(int x,int y,int width,int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Rectangle(Rectangle a)
{
this.x = a.x;
this.y = a.y;
this.width = a.width;
this.height = a.height;
}
public String toString()
{
return "Start: ("+x+","+y+"), Width: "+width+", Height: "+height+"\n";
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public void setWidth(int width)
{
this.width = width;
}
public void setHeight(int height)
{
this.height = height;
}
public int area()
{
return width*height;
}
public boolean overlaps(Rectangle a)
{
if ((x>a.x+a.width) || (a.x>x+width) || (y>a.y+a.height) || (a.y>y+height))
{
return false;
}
return true;
}
public Rectangle intersect(Rectangle a)
{
if (!overlaps(a))
return null;
int left,right,top,bottom;
if (x<a.x)
left = a.x;
else
left = x;
if (y<a.y)
bottom = a.y;
else
bottom = y;
if ((x+width)<(a.x+a.width))
right = x+width;
else
right = a.x+a.width;
if ((y+height)<(a.y+a.height))
top = y+height;
else
top = a.y+a.height;
return new Rectangle(left,bottom,right-left,top-bottom);
}
public Rectangle union(Rectangle a)
{
int left,right,top,bottom;
if (x<a.x)
left = x;
else
left = a.x;
if (y<a.y)
bottom = y;
else
bottom = a.y;
if ((x+width)<(a.x+a.width))
right = a.x+a.width;
else
right = x+width;
if ((y+height)<(a.y+a.height))
top = a.y+a.height;
else
top = y+height;
return new Rectangle(left,bottom,right-left,top-bottom);
}
}
Not sure why you are extending a JLabel to do custom painting. The tutorial showed you how to use a JPanel.
For the two common ways to do incremental paint, check out Custom Painting Approaches:
Use a List to keep track of the Rectangles (this is probably what you want since you want to be able to test for intersections.
Use a BufferedImage.

How to handle the collision between two arraylist objects?

I have a problem with my code here. I want to make a game with thow ball on each side of screen, on ball being controlled by the user and the other one by the computer. Both ball shoot to each other, and if the bullets intersects one with another, i need to make something happen. I managed to do some thing here, and I have two class, one for the player bullets, and the other one for the enemies bullets, and the bullets are created trough arraylists. All works fin until now, but if I try ti make them collision with each other,it doesnt work at all. I've tried a lot of things but none of it worked, and I would really appreciate if someone could help me.
That is the Player Projectile class:
import java.awt.Rectangle;
public class Projectiles {
private int x, y, speedX;
private boolean visible;
private int width = 10;
private int height = 10;
private Rectangle r;
public Projectiles(){
}
public Projectiles(int startX, int startY) {
x = startX;
y = startY;
speedX = 1;
visible = true;
r = new Rectangle(0, 0, 0, 0);
}
public void update(){
x += speedX;
r.setBounds(x, y, width, height);
if (x > 800){
visible = false;
r = null;
}
if (x < 800){
checkCollision();
}
}
private void checkCollision() {
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public Rectangle getR() {
return r;
}
public void setR(Rectangle r) {
this.r = r;
}
}
And this one is the Enemy_Projectile class:
import java.awt.Rectangle;
public class Enemy_Projectiles {
private int x, y, speedX;
private boolean visible;
private int width = 30;
private int height = 20;
public static Rectangle r;
Projectiles p1;
public Enemy_Projectiles(int startX, int startY) {
x = startX;
y = startY;
speedX = 1;
visible = true;
r = new Rectangle(0, 0, 0, 0);
}
public void update() {
x -= speedX;
r.setBounds(x, y, width, height);
if (x < 0) {
visible = false;
r = null;
}
if (x > 0){
checkCollision();
}
}
private void checkCollision() {
if(r.intersects(p1.getR())){
visible = false;
System.out.println("Coliziune!!");
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
Do not check intersection after the frame has been drawn. Let's say you have a slow computer and your bullets intersect, but they have moved out of intersection in one frame.
You need to apply high school physics/geometry. Calculate where the bullet will be well before you render it. Then, calculate where the ball will be, and construct a line segment for each from where they are now, to where they will be on the next frame. Check if these segments intersect. Then you will have a fool-proof method of checking for intersection.
This method is similar to how physics and intersections between objects are handled inside of a game engine like Unity.

Categories

Resources