dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Thread: Detect a mouse click on specific entity in an arrayList

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    17

    Detect a mouse click on specific entity in an arrayList

    Hi,

    Im a beginner in Java and I'm creating a simple game. I have an arrayList of entities (cars in my case), I need to be able to detect a mouse click on an individual object (car) in the array. I need to do this so I can change the speed of the car if it has been pressed. Im not sure how to go about doing this, hope somebody can help me out here.

    Thanks.

  2. #2
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Detect a mouse click on specific entity in an arrayList

    Hello Gnasher,

    Your cars each need a "collision region". It can be a rectangular area or a circular area. Once you have that, it is quite easy to compare the mouse coordinate with a car "collision region" to determine if the click was inside the region or outside of it. I would say it's easier with a rectangular region as you simply need some comparison.

    Hope it helps

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Detect a mouse click on specific entity in an arrayList

    JeffB is correct.
    If you want more help please provide more details of your Car class eg does it extend a Swing component.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Dec 2010
    Posts
    17

    Re: Detect a mouse click on specific entity in an arrayList

    Thanks for the help both.

    So far I have a base entity class, that is used for collisions and movement. Then, I have a car entity class that extends the base entity. In the base entity class I already have a collision region because the aim of the game is to stop the cars colliding, which is working. I just dont understand how to go about getting it to work with mouse position and that, because I've just started with Java and dont understand that much.

    Im not sure what my next step is...

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Detect a mouse click on specific entity in an arrayList

    How are you drawing your cars on the screen. For example: Are these car objects Swing/AWT objects which you are manually positioning or are you overriding a JPanel's paintComponent method to draw them yourself?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Dec 2010
    Posts
    17

    Re: Detect a mouse click on specific entity in an arrayList

    Oh sorry. Im using AWT to draw the images.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)