Stopping Enemy Sprites From Overlapping

Oct 1, 2014

I am currently in the process of making a game in java. it's going fairly well, however, one problem that i am having is that i cant seem to get my enemy sprites not stack up onto one another. What i want to happen is if two enemy sprites come in contact with one another i want them to touch but not to overlap. However, i cant seem to get this to work.

This is the enemy class:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Enemy extends Base {
int x;
int y;

[Code] ....

View Replies


ADVERTISEMENT

Game Combat / Fighting One Enemy At A Time?

Mar 1, 2014

So I'm making a combat system for a game I'm working on.The combat is basically like old-school Runescape's, where the 2 participating combatants take turns hitting each other.Except, in this game, I'm trying to make it so you can only fight one enemy at a time, no matter what. I'm trying to make so if you're fighting a mob and you try to attack another, your action is ignored and you continue your current fight.

I'd post the code I have, but it is such a mess with all the booleans and other variables being in other classes that I'd have to post the whole project itself.

Here's the sequence I have:

1. I try to fight an enemy. (Once I'm in their "fight radius" and press space)

2. They fight me back. (Executes their combat-style method, in this case, non-hostile)

3. I walk away and try to fight another enemy. (I'm still in the other enemy's "chase radius" and they follow me as I try to attack another enemy)

4. I'm able to fight the other enemy, resulting in a 1 vs 2 fight. (The second enemy executes the same non-hostile combat method to fight back)

I don't want that. This is my goal:

1. I try to fight an enemy. (Once I'm in their "fight radius" and press space)

2. They fight me back. (Executes their combat-style method, in this case, non-hostile)

3. I walk away and try to fight another enemy. (I'm still in the other enemy's "chase radius" and they follow me as I try to attack another enemy)

4. I'm unable to start another fight because I'm already fighting a different mob. (The second enemy just ignores as the enemy I attacked first walks up to me and continues to attack until I kill it or run away)

Is this because of static/non-static methods and values? Instances?

I tried everything but I cannot get this 1-enemy-per-fight limit to work.

View Replies View Related

Handling Sprite Overlapping?

Sep 16, 2014

The game I am working on now is an overhead game. Anyways I have a visual issues with the sprites where they overlap. I mean its normal to have sprites overlap but I do not want one to appear like it is walking on top of the other rather than the ground. The below is an image illustration the issue I want to address. (I am really good at drawing)

On the right you see what I want my sprites to do, but on left you see overlap in the way I am trying to avoid.

Now my approach to this problem is taking my array of monsters (a class that extends JLabels) and reordering them based on which monster is higher I make this occur at every run of my thread. It seems to me that if a monster is higher it should be added to the screen before the one that is lower. So I try to keep resorting the array of monsters at every iteration of my main game loop based on who is higher to account for the overlap

if (e.getSource() == follower){
c =0;
for (monster m: monsters) {
//Arrays.sort(monsters, new whoHigherComparator()); //I tried having the sort happen here
m.movingToPlayer(joe);//1
Arrays.sort(monsters, new whoHigherComparator());//2
layeredScene1.add(m, new Integer(3));//3

[code]...

The above is code that shows the majority of my games flow I will describe my flow with references to the commented lines above with [num] for better understanding of where I am coming from. Basically, there is a set number of monsters, the monster all locate and move to the player [1], then I attempt to sort the monster array based on height [2], then add my re-sorted monsters back onto the pane in order [3], then bumpers (a type that handle collision and make sure monsters don't overlap too much) attach the current monster [4], if bumpers are touching monsters arae moved away from each other until their bumpers are no longer touching [5], if the monster is moving leftwards set the animation to a leftwards walk [6], and likewise for rightwards walking [7]

By the way this is my comparator

public class whoHigherComparator implements Comparator<monster>
{
@Override
public int compare(monster m1, monster m2) {
System.out.println("comping?");
int h1 = m1.getY();
int h2 = m2.getY();
if (h1 < h2)

[code]...

At this point everything in my game flow works just fine, its just the annoying "walking on top of each other" effect that I am having trouble handling. And my attempt at this is line [2] & [3] from my game flow which seems like it would be an effective way to handle the "walking on top of another" effect but its surely not working. I also swapped the returns from my comparator but still no changes. It seems that maybe the array isn't being sorted and/or these changes are just not being reflected.

View Replies View Related

Background Overlapping Images

Jan 7, 2014

Java Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class sample{
public static void main(String args[]){
JFrame frame = new JFrame();
frame.setSize(300,300);

[code]....

To limit the amount of drawing done i want unmoving objects to be painted once. So i set a boolean. When it first load the boolean is false so it draws then after first draw i never want it to draw the objects agian but leave them on the screen. Now in this if you run it you get a black screen.

If you didnt have the "getContentPane().setBackground(Color.black)" it would work fine and show a blue brick. Is there something I don't understand about the setBackgound Function that makes it automatically redone even when not called? and if so how do i overwrite this?

View Replies View Related

Swing/AWT/SWT :: BorderLayout - Adding Multiple Video Game Oriented Objects Such As Sprites To JFrame?

May 24, 2014

I decided to write a small Java program to experiment around with BorderLayout, because I'm developing a Java game and I need to have 2 objects placed in a single JFrame at the same time, and everyone I asked said I need BorderLayout to do that.Before you answer: Also, is using BorderLayout the best option for adding multiple video game oriented objects such a sprites to a JFrame?

So the Java program I wrote is supposed to place a JButton on the JFrame and ALSO place a graphic component (a rectangle in this case). The problem is, only the button shows up, as can be seen in the image link below: URL....

Here is the code:

**main.java** --> The main method class + JFrame/JPanel/JButton constructor
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main {

[code]....

View Replies View Related

Loop Not Stopping Where It Should

Sep 14, 2014

I am doing a simple program, just to find multiples of 7 all the way to 125. But the loop its not stopping at 125 and is going to 126.

public class Assingment2B {
public static void main(String[] args) {
int number = 0;
while (number <= 125) {
number += 7;
if (number % 7 == 0) {
System.out.println(number);

[code]....

View Replies View Related

Ball Stopping At The Edge Of Maze

Mar 7, 2015

I am doing an assignment which involves creating a maze and a ball that needs to go through the maze to an end tile then stop the scenario. The maze is made up of 208 buttons in a Jbutton Grid layout. I am replacing certain buttons with a brown tile icon to make up the maze. The ball needs to run along those brown tiles as its path and not be able to move off the brown tile icon images.

I have built the maze and have got quite far with this but now I am stumped on the concept of keeping the ball within the boundary edges and on the brown tiles. I have been told to use if statements, but not had the process explained to me in a way I can understand.

View Replies View Related

Playing And Stopping Music File With Same JButton

Oct 8, 2014

This is my whole Code

import java.awt.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

[Code] .....

I'm not getting any errors, just i can't seem to stop the music from playing after clicking the jbutton "Sound On" / "Sound Off" again. I've tried a few methods but it just isn't working for me.

View Replies View Related

Playing And Stopping A Music File With Same JButton

Oct 8, 2014

This is my whole Code

import java.awt.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

[Code] ....

I'm not getting any errors, just i can't seem to stop the music from playing after clicking the jbutton "Sound On" / "Sound Off" again. I've tried a few methods but it just isn't working for me.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved