Top down view unit atack

helo,

i am trying to have my player atack a unit facing western in this game i am working on and also facing southern western but he just stands their. if you try to atack all the other directions work fine.

the file i am working on is caled “Roleplayinggamehorn.java”

i am runing it in 1366 x 768 resolution

the project file is at:

anyone ever seen this?

nathan

Hi

You should actually post the code here which has the issue as it will give you more of a chance for people to help out. I doubt people are going to download an unknown zip file and then install your code and try and understand where your issue is with the small amount of information you have provided.

1 Like

do you guys do the code tags here? System.out.println("testing");

ah yes it worked

i want to try to put in a fix

the declaration is a good read, let me see if i can put some code on here

i started by making this:

package test;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.concurrent.ConcurrentHashMap;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

/** shoot a bal from 200, 200 in any direction. the system wil print one of eight quadrent which can be used for 
* animation purpose - example: player click quadrent one then display western moving animation. plug in the 
* center of the scren for a top down view two dimensional game and reverse the negative / positive symbol tied 
* to a background. */

public class direction extends JFrame implements MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Integer x, y, reckey;
	Double mx, my;
	Boolean once;
	Line2D line, line2, line3, line4, line5, line6, line7;
	Rectangle rectangle[];
	ConcurrentHashMap<Integer, movingObject> molist;
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stubfloat f = 102.236569f; 
		new direction();
		//System.out.print(5.0 / 2.0);
	}
	public class movingObject{
		Rectangle rec;
		Double spe, quad, x2, y2;
		public movingObject(double speed, int quadrent) {
			spe = new Double(speed);
			quad = new Double(quadrent);
			x2 = new Double(200);
			y2 = new Double(200);
			rec = new Rectangle(x2.intValue(), y2.intValue(), 8, 8);
		}
	}
	public direction() throws IOException {
		reckey = new Integer(0);
		molist = new ConcurrentHashMap<Integer, movingObject>();
		line = new Line2D.Double(100, 100, 200, 122.5);
		once = false;
		setSize(700, 500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addMouseListener(this);
		x = getWidth() / 2;
		y = getHeight() / 2; 
		Double one = line.getX2() - line.getX1();
		Double two = line.getY2() - line.getY1();
		//System.out.print(two / one);
	}
	public void paint(Graphics g) {
		super.paint(g);
		if (once.equals(false)) {
			once = true;
		//	System.out.println(((45) / 100.0));
		}
		Iterator<movingObject> iterator = molist.values().iterator();
		Integer removekey = new Integer(0);
		while (iterator.hasNext()) {
			movingObject mo = iterator.next();
			g.drawRect(mo.rec.x, mo.rec.y, (int)mo.rec.getWidth(), (int)mo.rec.getHeight());
			//System.out.println(mo.quad);
			if (mo.quad == 1) {
				mo.x2--;
				mo.y2 = mo.y2 - mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 2) {
				mo.y2--;
				mo.x2 = mo.x2 - mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 3) {
				mo.y2--;
				mo.x2 = mo.x2 + mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 4) {
				mo.x2++;
				mo.y2 = mo.y2 - mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 5) {
				mo.x2++;
				mo.y2 = mo.y2 + mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 6) {
				mo.y2++;
				mo.x2 = mo.x2 + mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 7) {
				mo.y2++;
				mo.x2 = mo.x2 - mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.quad == 8) {
				mo.x2--;
				mo.y2 = mo.y2 + mo.spe;
				mo.rec.x = mo.x2.intValue();
				mo.rec.y = mo.y2.intValue();
			}
			if (mo.rec.x <= 0) {
				iterator.remove();
			}
		}
		//g = getGraphics();
		//g.drawImage(bufferedimage, x, y, this);
		g.drawLine(200, 200, 100, (int)155);
		g.drawLine(200, (int)200, (int)155, 100);
		g.drawLine(200, 200, (int)245, 100);
		g.drawLine(200, 200, (int)300, (int)155);
		g.drawLine(200, 200, (int)300, (int)245);
		g.drawLine(200, 200, (int)245, 300);
		g.drawLine(200, 200, (int)155, 300);
		g.drawLine(200, 200, (int)100, (int)245);
		//g.draw(line);
		repaint();
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == 3) {
			/*
			System.out.println("molist size: " + molist.size());
			System.out.println("molist key: " + reckey);
			*/
			mx = e.getLocationOnScreen().getX();
			my = e.getLocationOnScreen().getY(); 
			double local = 200 - mx;
			double local2 = 200 - my;
			if (local > 0 && local2 > 0) {
				if (local >= local2) {
					molist.put(reckey, new movingObject(local2 / local, 1));
					reckey++;
					//System.out.print(local2 / local);
				}
				if (local < local2) {
					molist.put(reckey, new movingObject(local / local2, 2));
					reckey++;
					//System.out.print(local / local2);
				}
				//System.out.println(local / local2);
				if (local / local2 >= 2.5) {
					System.out.println("quadrent one");
				}
				if (local / local2 < 2.5) {
					 if (local / local2 < .45) {
						System.out.println("quadrent three");
					 }
					 if (local / local2 > .45) {
					System.out.println("quadrent two");
					 }
				}
			}
			if (local < 0 && local2 > 0) {
				local = mx - 200; // local is mouseclicked x - 200
				if (local <= local2) { // local2 is 200 - mouseclicked y
					// shoots from 200, 200
					molist.put(reckey, new movingObject(local / local2, 3));
					reckey++;
					//System.out.print(local2 / local);
				}
				if (local > local2) {
					molist.put(reckey, new movingObject(local2 / local, 4));
					reckey++;
				}
				//System.out.println(local / local2);
				if (local / local2 <= .45) {
				System.out.println("quadrent three");
				}
				if (local / local2 > .45) {
					 if (local / local2 <= 2.5) {
						System.out.println("quadrent four");
					 }
					 if (local / local2 > 2.5) {
					System.out.println("quadrent five");
					 }
				}
			}

			if (local < 0 && local2 < 0) {
				local = mx - 200;
				local2 = my - 200;
				if (local > local2) {
					molist.put(reckey, new movingObject(local2 / local, 5));
					reckey++;
				}
				if (local <= local2) {
					molist.put(reckey, new movingObject(local / local2, 6));
					reckey++;
				}
				if (local / local2 > 2) {
				System.out.println("quadrent five");
				}
				if (local / local2 <= 2) {
					if (local / local2 >= .45) {
						System.out.println("quadrent six");
						
					}
					if (local / local2 < .45) {
					System.out.println("quadrent seven");
					}
				}
				//System.out.println(local / local2);
				//System.out.println("botom left working");
			}
			if (local > 0 && local2 < 0) {
				local2 = my - 200;
				if (local < local2) {
					molist.put(reckey, new movingObject(local / local2, 7));
					reckey++;
				}
				if (local >= local2) {
					molist.put(reckey, new movingObject(local2 / local, 8));
					reckey++;
				}
				if (local / local2 > 2) {
					System.out.println("quadrent one");
				}
				if (local / local2 <= 2) {
					if (local / local2 < .45) {
						System.out.println("quadrent seven");
						
					}
					if (local / local2 > .45) {
					System.out.println("quadrent eight");
					}
				}
				//System.out.println(local / local2);
				//System.out.println("botom left working");
			}
			//e.getLocationOnScreen().getX() <= x + bufferedimage.getWidth() / 2 && e.getLocationOnScreen().getY() <= y + bufferedimage.getHeight() / 2 && 
			//if (my / mx ) {

				//System.out.print("test");
			//}
		}
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}

for the background / player / enemy movement but then i wanted to use paintComponent instead of paint but the shooting was all buggy i think that might be the problem

Wow, that’s some code right there. Godspeed to you.

@Gjallar - Do you have any helpful suggestions to add to your comment? We are all here to learn, hoping to benefit from those willing to share their knowledge.

switching to paint did not fix it… well it quit flashing i think

ii was thinking about working on a simpler project maybe like a sidescroler because my programing skils are not that good

maybe even working on a book because i do not feel like drawing much

basicly i got that hooked up to this


			// northern western western quadrent
			//System.out.println(type.agrovated);
			if (type.quadrent == 1) {
				// if unit wooyl and not atacking
				if (type.name.equals("wooyl") && !type.atacking){
					// reset wooyl colision rectangle
					colisionrectangle.setFrame((wooyl.colisionrectangle.getX() - 1), (wooyl.colisionrectangle.getY() - wooyl.speed), 1, 1);
					// if wooyl next step intesect mater, stop unit and unit footstep sound DO NOT LET WOOYL WALK THROUGH HEITA FOREST MATER
					if (one.border.intersects(colisionrectangle) && type.name.equals("wooyl") || two.heitaforestbotomcolisionpolygon.intersects(colisionrectangle) && 
							type.name.equals("wooyl") || two.heitaforesttopcolisionpolygon.intersects(colisionrectangle) && 
							type.name.equals("wooyl") || two.heitaforesthousepolygon.intersects(colisionrectangle) && 
							type.name.equals("wooyl")) {
						stopwooyl();
					}
					// move all wolf in list as the background move and destination point
					
					// move background backward relative to player to point of click
					
				one.x++;
				one.y = one.y + wooyl.speed;
				// move destionation point the same as background
				wooyl.destinationpoint.x++;
				wooyl.destinationpoint.y = wooyl.destinationpoint.y + wooyl.speed;
				movewolf(1);
					
				}
				
				
				// make wolf run toward player if agrovated
				if (type.name.equals("wolf") && !type.atacking && !type.dying && type.agrovated) {
					type.x--;
					type.y = type.y - type.speed;
				}
			}

wooyl is the main character

then the mouseclicked part of direction for my game look like:

private unit detectquadrent(MouseEvent e, unit type) {
		Double local = new Double(0);
		Double localtwo = new Double(0);
		// if wooyl was pased in
		if (type.name.equals("wooyl")) {
			// get mouse right click point
		mousex = e.getLocationOnScreen().getX();
		mousey = e.getLocationOnScreen().getY();
		// set destinationpoint rectangle to right click location
		wooyl.destinationpoint.setFrame(mousex.intValue() - 2, mousey.intValue() - 2, 5, 5);
		// subtract leser number from greater number (500 - 250 (example)) to get slope relative to one below
		if (!wooyl.moving) {
		local = (wooyl.x + (wooyl.curentimage.getWidth() / 2)) - mousex;
		localtwo = (wooyl.y + (wooyl.curentimage.getHeight() / 2)) - mousey;
		}	
		if (wooyl.moving) {
			local = (wooyl.x + (wooyl.curentanimated.getIconWidth() / 2)) - mousex;
			localtwo = (wooyl.y + (wooyl.curentanimated.getIconHeight() / 2)) - mousey;
		}
		}
		// get slope wolf to wooyl so wolf can run toward him
		if (type.name.equals("wolf") && type.moving) {
			local = (type.x + type.curentanimated.getIconWidth() / 2) - (wooyl.x + (wooyl.curentanimated.getIconWidth() / 2));
			localtwo = (type.y + type.curentanimated.getIconHeight() / 2) - (wooyl.y + (wooyl.curentanimated.getIconHeight() / 2));
		}		
		if (type.name.equals("wolf") && !type.moving) {
			local = (type.x + type.curentimage.getWidth() / 2) - (wooyl.x + (wooyl.curentimage.getWidth() / 2));
			localtwo = (type.y + type.curentimage.getHeight() / 2) - (wooyl.y + (wooyl.curentimage.getHeight() / 2));
		}
		/*
		System.out.println("local " + type.name + " " + local);
		System.out.println("local two " + type.name + " " + localtwo);
		*/
		// northern western quadrent
		if (local > 0 && localtwo > 0) {
			// first subquadrent (left sub-quadrent). known because x greater than y.
			if (local >= localtwo) {
				type.quadrent = 1;
				// divide smaler number by biger number to get slope relative to one for x
				type.speed = localtwo / local; 
			}
			// right subquadrent
			if (local < localtwo) {
				type.quadrent = 2;
				type.speed = local / localtwo;
			}
			if (local / localtwo >= 2.5) {
				// animation quadrent one
				type.moving = true;
				type.animationquadrent = 1;
				type.facing = "western";
				type.oposite = "eastern";
				type.opositeinteger = 5;
			}
			if (local / localtwo < 2.5) {
				 if (local / localtwo <= .45) {
					// animation quadrent three
					type.moving = true;
					type.animationquadrent = 3;
					type.opositeinteger = 7;
					type.facing = "northern";
					type.oposite = "southern";
				 }
				 if (local / localtwo > .45) {
						// animation quadrent two
						type.moving = true;
						type.animationquadrent = 2;
						type.opositeinteger = 6;
						type.facing = "westernnorthern";
						type.oposite = "easternsouthern";
				 }
			}
		}

for the western quadrent or half; yeah half

and i say if the unit intersect in the enemy iterator, have them start atacking

[code]

	// wolf start atacking WOLF UNIT RECTANGLE INTERSECT WOOYL COLISION RECTANGLE
	if (wolfobject.unitrectangle.intersects(wooyl.unitrectangle)) {
		// unit go into mele batle
			if (wooyl.moving) {
			stopwooyl();
			}
		}
		if (!wooyl.atacking) {
			/*
			System.out.println(wolf.contains("eastern"));
			System.out.println(wolfobject.facing);
			*/
			// start wooyl atacking and damage timer
		wooyl.atacking = true;
		wooyl.atacktimer.start();
		}
		//System.out.println("wooyl atacking: " + wooyl.atacking);
		// wolf is no longer moving
		wolfobject.moving = false;
		wolfobject.agrovated = false;
		// start wolf atacking and damage
		if (!wolfobject.atacking && !wolfobject.dying) {
		wolfobject.atacking = true;
		wolfobject.atacktimer.start();
		//wooylatacking = true;
		}
		// stop wolf walking sound
		if (wolfobject.wolffootstep.isRunning()) {
		wolfobject.wolffootstep.stop();
		}
		// display wolf atacking animation
		//wolfobject.atacking = true;
	}

here we go! i forgot to export one of the file and one file name was wrong but it did not give me a null pointer exception

both the animation are working now and it is dealing damage from the hero and the enemy

i had just updated the hero file… i put horn on the guy so i had changed the image, moving animation, and atacking file