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 2 of 2

Thread: code is right but i dont understant how its working

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default code is right but i dont understant how its working

    this code works find. all its doing is setting collsion around enemy. and if player touch player from right than player move to right. if player touch enemy from left than player move to left.

    playerX = player x postion
    playerY = player y postion
    playerW = player width
    playerH = player height
    x = enemy x postion
    y - enemy y postion
    width - enemy width
    height - enemy height
    p.setX = set player x postion
    ...



    if(playerX + playerW >= x && playerX <= x + width)
    		{
    		    if(playerY+playerH >= y && playerY <= y+height)
    		    {
    		        if (playerX <= x)  //player on left
    		        {
    		        	System.out.println(playerX +"--"+x);
    		        	p.setX(x - width);
    		        }
    		        else if(playerX >= x)//player on right
    		        {
    		        	p.setX(x + width);
    		        }
    		    }
    		}


    this code below i understand how its working
    if(playerX + playerW >= x && playerX <= x + width)
    		{
    		    if(playerY+playerH >= y && playerY <= y+height)
    		    {

    but this code is like magic!
    ok so if player x postion is on left of enemy x, than move player to right
     if (playerX <= x)  //player on left
    p.setX(x - width);

    but i dont get it. bc if want to to see if player is on left of enemy than i should take playerX+playerW so collsion point is on right of player head. for ex.
     if (playerX+playerW <= x)       //thsi i understand but its not right why??
    and next line shoud be enemy x - player width.
    p.setX(x - playerW) //this i understand but its not right. why??


    i was thinking if playerx+playerW will get me a point at right of player head. and
    this will test if player right side of head is <= enemy x(which is left of enemy head)
    if (playerX+playerW <= x) //this shoudl work.........


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: code is right but i dont understant how its working

    if (playerX+playerW <= x) //this shoudl work.........

    Time for some trouble shooting then.
    You make multiple assumptions and something does not work the way you wanted, odds are something is not quite right.
    Do some printlns on the variables each step. Follow what the code does. This is a great learning opportunity. Figure out why it is working the way it does when you use: if (playerX+playerW <= x) and figure out what would have to be done to make it work the way you expected it to work. If you dig deep enough there is a treasure chest.

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. dunno how to test this code so i dont know if it works help please
    By jonathanfox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 24th, 2012, 04:43 AM
  3. Replies: 4
    Last Post: July 25th, 2011, 06:12 PM
  4. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM
  5. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM