tags:

views:

38

answers:

2

Whats the best way to create a dart board game on Android. Is it possible to determine where a dart hit and in which area it is using a canvas or do I need to use a diffenrent approach?

Edit

Sorry I dont think I have explained my problem very well.

In svg in html5 I can create a dart board using shapes and assign an id to each shape. When a dart lands on a shape the code knows which shape it has landed on, gets its id and calculates the score. e.g id="20" id="60" id="40" for all 20 areas on a dart board.

How would I do this on the Android?

+1  A: 

You've got the right idea. A simple approach would be to display a custom DartBoardView in an activity. The view would use an overridden onDraw to draw a dart board image and any darts the user has "thrown." The view would then use View.onTouchEvent to handle user touch events and translate those into new darts.

Josh
Thanks for the answer, its half way where I need to be. Please see my edit to the question
skyfoot
try using math! distance formula and a loop
schwiz
+1  A: 

New answer based on your update:

You're going to have to do the hit detection manually. A touch event will give you an x-y coordinate with the upper left of the view as origin. Calculating points would go as follows:

  1. Offset the given coords so they line up with the center of your board (i.e. touching the middle of your board would give (100, 150), translate that to (0,0)).
  2. Get the angle of the touch to find which numbered section of the board was hit. Something like double touchAngle = Math.atan2(y,x). Might need to convert from radians to degrees here.
  3. Map the angle to a base point value (if angle > 9 && angle < 27, base points = 7)
  4. Multiply the base point value by 2 or 3 if the distance of the touch (distance = Math.sqrt(x^2 + y^2)) from center is a particular length away to account for the multiplier rings.
Josh