Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: check entire list for colision

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default check entire list for colision

    helo,

    i am trying to see if my enemy (the wolf) is intersecting my player in my game. i am trying to reset my player. right now he is set to display an atacking animation if a wolf intersect him so i understand this part; but if he run away i need to stop his atacking animation and put his moving animation so i would need to check the thing is i have multiple enemy (or want to) so i was thinking i could say something like "if wolf !intersecting(player.rectangle) && player.atacking then player.atacking = false (runign away) but the thing is what if their was another wolf intersecting him. i gues the way i am making the game this is imposible because the run speed is all the same and the enemy all spawn from the left but i feel like i might need to know this to be a good coder and maybe put something like that in the video game.

    i was thinking about what if their was a way to say if (mywholelist.isintersecting(player)) i have the list in a while loop and i can set a boolean to say true if any of the element in the list are intersecting then when i run away from the wolf with my player what if one wolf is not intersecting but one is how do i check if all are not intersecting

    in lamen term my code kinda look like:

     
    	// iterate through wolf list to move wolf and animate
    	wolfiterator = wolf.values().iterator();
    	while(wolfiterator.hasNext()) {
    		unit wolfobject = wolfiterator.next();
    		g.setColor(new Color(255, 0, 0, 255));
    		g.fillRect((int)wolfobject.agrovationrectangle.getX(), (int)wolfobject.agrovationrectangle.getY(), (int)wolfobject.agrovationrectangle.getWidth(), 
    		(int)wolfobject.agrovationrectangle.getHeight());
    		g.setColor(new Color(0, 255, 0, 255));
    		g.fillRect((int)wolfobject.unitrectangle.getX(), (int)wolfobject.unitrectangle.getY(), (int)wolfobject.unitrectangle.getWidth(),
    				(int)wolfobject.unitrectangle.getHeight());
    		// wolf run toward wooyl; agrovated
    		if (!wolfobject.unitrectangle.intersects(wooyl.unitrectangle) && wolfobject.agrovated) {
    			wolfobject = detectquadrent(null, wolfobject);
    		}
    		// wolf start atacking
    		if (wolfobject.unitrectangle.intersects(wooyl.colisionrectangle)) {
    			if (!wooyl.atacking) {
    			wooyl.atacking = true;
    			}
    			wolfobject.moving = false;
    			wolfobject.agrovated = false;
    			if (!wolfobject.atacking) {
    			wolfobject.atacking = true;
    			wooylatacking = true;
    			}
    			handleunitmovement(g, wolfobject);
    		}
    		// stop wolf atacking if player ran
    		if (!wolfobject.unitrectangle.intersects(wooyl.colisionrectangle) && wolfobject.atacking) {
    			wolfobject.atacking = false;
    		}
    		// wolf run toward wooyl; agrovate
    		if (wolfobject.agrovationrectangle.intersects(wooyl.colisionrectangle)) {
    			if (!wolfobject.agrovated) {
    			wolfobject.agrovated = true;
    			}
    			if (!wolfobject.atacking) {
    			handleunitmovement(g, wolfobject);
    			}
    		}
    		if (wooyl.moving || wolfobject.agrovated) {
    			wolfobject.unitrectangle.setRect(wolfobject.x.intValue(), wolfobject.y.intValue(), wolfobject.curentanimated.getIconWidth(),
    					wolfobject.curentanimated.getIconHeight());
    		}

    maybe my while loop is confusing me anyone know of a method to check the entire list if these guy are or are not intersecting?

    i was looking online at "containall()" method or am right now
    Last edited by nathaniel victor nelson; February 25th, 2021 at 01:53 PM. Reason: fixed code eror

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: check entire list for colision

    a way to say if (mywholelist.isintersecting(player))
    Something like that. Declare a method: isIntersecting(mywholelist, player) that checks if any objects in the list intersect with player.
    The method would loop through the list and test if there is an intersection with player
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: check entire list for colision

    hmmm i just realized it might not even be a problem because i can turn off atacking if the player move

    now the player wil show standing, moving, atacking image or animation and the enemy the same if they colide or are apart or whatever for eight places... western, western northern, excetera.

  4. #4
    Junior Member
    Join Date
    Jul 2020
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: check entire list for colision

    i had to write my own method - it look kinda like:
    	public boolean wolfcheck() {
    		boolean variable = false;
    		Iterator<unit> wolfiteratorthree = wolf.values().iterator();
    		while(wolfiteratorthree.hasNext()) {
    			unit wolfobjectthree = wolfiteratorthree.next();
    			if (wolfobjectthree.facing.equals("eastern")){
    					variable = true;
    		}
    	}
    		return variable;
    	}

    because i was stil trying to check an entire list if a string was present in a sub variable (string) of my "unit" clas; my own creation

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: check entire list for colision

    I'd suggest using a enum for the facing variable instead of it being a String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. check a list based on the cell if there is a value
    By glprobot in forum Loops & Control Statements
    Replies: 1
    Last Post: July 10th, 2019, 07:59 AM
  2. check if table exists from a list of files in a directory
    By efoikonom in forum JDBC & Databases
    Replies: 1
    Last Post: June 10th, 2013, 08:42 AM
  3. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  4. WHY AM I HAVING TO PUT THE ENTIRE FILE PATH?!?!
    By CameronHussey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2011, 05:32 AM