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: Controlling a platform game character with the mouse.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Controlling a platform game character with the mouse.

    Hello there, I am a beginner to Java and this site (not sure if this post is in the right place either, apologies if it isn't

    Anyway I have been trying to make a simple platform game which currently consists of an image/sprite moving round on an image via Key events. The character currently jumps using the space bar.

    However what i would like to do is make my character jump by mousePressing on the character, dragging the mouse downwards and letting go, like pulling back a catapult.

    This has unfortunately caused me nothing but headaches.

    Currently i cannot get the game to recognise when the character is being clicked, is there an easy way to do this?

    Currently my character is just an ImageIcon, but im not sure if it has to be an object or a shape or something, I'm pretty confused. Mouse Listener is so much more complicated than using a keyboard!

    Cheers.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Controlling a platform game character with the mouse.

    recognise when the character is being clicked
    The mouse listener can determine where the click was made and see if it is on a character.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Controlling a platform game character with the mouse.

    Thanks for the quick reply!

    I can get the mouse listener to give me it's it's MX and MY on click, i also have the X and Y of my character...i made 2 new integers CX and CY which add 90 pixels (the size of the image) to X and Y so the image is easy to click on.

    Then iv set if (mx == x && mx <= cx && my == y && my <= cy){ then for example do a system.out message, but nothing, no errors either.

    I'm pretty sure im trying to do this the wrong way and that there is an easier method.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Controlling a platform game character with the mouse.

    Try debugging by adding some println statements to the code that print out the x,y location of the mouse click and the x,y location of the character so you can see it the click is inside the rectangle surrounding the character.

    Testing with == is unusual when testing locations. Use >= for the left hand side and <= for the right hand side.
    Same logic for top: >= and <= for the bottom


    Another way would be to use the Rectangle class's contains() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    subsidian (March 18th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Controlling a platform game character with the mouse.

    Wow thanks! The first == were all that was wrong.

Similar Threads

  1. Prints an extra character for this game.
    By TCDave in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 06:35 PM
  2. Video Game Character ArrayList Help?
    By MagicTricksKill in forum Object Oriented Programming
    Replies: 0
    Last Post: October 15th, 2012, 03:44 PM
  3. Cross-Platform game
    By ggcon2 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 02:33 PM
  4. Adding a mouse listener to my Draughts game
    By MXA92 in forum Object Oriented Programming
    Replies: 10
    Last Post: March 22nd, 2012, 06:43 PM
  5. [SOLVED] Platform game player doesn't respond to keyboard
    By jesamjasam in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 09:44 AM