5 Replies - 2813 Views - Last Post: 16 April 2011 - 04:05 PM

#1 taylor.alison   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Custom Event Handling

Posted 16 April 2011 - 12:11 PM

I'm currently programming a game where flowers fall from the top of the screen and you have to catch them at the bottom. I know how to do collision detection, I know the math behind it, but I'm using threads to control my game so it is not as simple. The thing is, I am actually programming an Android application, but for all intents and purposes I believe that I would get more responses here for the custom event handling. I have tried and tried and tried but I cannot get my head around it at all. I have a class for the blossoms, and I want each blossom to be able to handle its own collision. There are an infinite number of blossoms that fall. Any thoughts?

Basically, I'm wondering if anybody knows of any good tutorials online. I've looked and looked and have yet to find one that helps me :(

This post has been edited by taylor.alison: 16 April 2011 - 01:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Custom Event Handling

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Custom Event Handling

Posted 16 April 2011 - 03:19 PM

You shouldn't use Thread and a Swing application
Use Sing.Timer to have your actionPerformed() called every whatever milliseconds
In this actionPerformed() update your flowers positions and call repaint()
That should do it :^:
Was This Post Helpful? 1
  • +
  • -

#3 taylor.alison   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Custom Event Handling

Posted 16 April 2011 - 03:21 PM

View Postpbl, on 16 April 2011 - 03:19 PM, said:

You shouldn't use Thread and a Swing application
Use Sing.Timer to have your actionPerformed() called every whatever milliseconds
In this actionPerformed() update your flowers positions and call repaint()
That should do it :^:



I'm using something called a handler. My problem is creating an event handler for collision detection. I need my blossom class to sit and wait until the blossom collides with a basket which will be catching it at the bottom of the screen.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Custom Event Handling

Posted 16 April 2011 - 03:25 PM

That should all be done in your actionPerformed()
when the method is called it compute the "state" what should be shown or not
store all that in instance variables (your actual animation "state")
and calls repaint() which will call paint() and paintComponent() stat should react accordingly to your animation "state"
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,830
  • Joined: 27-December 08

Re: Custom Event Handling

Posted 16 April 2011 - 04:04 PM

Since you are developing an Android app, I'll move this to Android. The Android members will know more about the specifics of Android graphics. Please post in the appropriate forum in the future.

As for your question, this is why you separate your program state/data from your GUI. I would design a custom StateManager class to encapsulate your grid using something like an int[][]. A value other than 0 will indicate an Object occupies that location. When you go to move the non-0's down a slot (until they hit the bottom), check to see if there is a non-0 value below your current 0 value. If there is, a collision has occured when you go to move it. That's the tetris way of doing things at least.

Also, check out the Observer pattern. Java has tools that are in the subset contained in the Android API for you to use, including the Observer and Observable classes.
Was This Post Helpful? 0
  • +
  • -

#6 taylor.alison   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: Custom Event Handling

Posted 16 April 2011 - 04:05 PM

Thank you - I posted here simply because I felt my problem was more Java based, not really pertaining to Android.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1