Flash game on ActionScript 3.0. There is a rotating drum, which stops when clicked, and you need to find out which sector has fallen out. I do not want to reinvent the curve bike. How is this usually done?

The object of the drum is drawn, the sectors are all different in size and not in order. There is an idea to tie the control points to the intersection of the sectors, but there is a problem. For each point, you need to specify the value of the sector for which it, roughly speaking, is responsible. These points can probably be added from the library after the drum is drawn - in the right places. Is there any possibility for an object added from a library to specify a property?

It only comes to mind to give the instance name with the desired parameter at the end (for example, item_1, item_5). Is this a very rude approach? Plus, two points can have the same value, because the sectors are repeated.

How I see it:

  1. Drum drawn
  2. In the right places come across control points (objects of the same class, taken from the library). Each point is given a name in which the parameter is specified.
  3. When you add the drum itself to the scene, somehow all the control points are collected in the array. Now they have coordinates, which can now be used to calculate the position of the drum and determine the fallen sector.
  4. Get the value of this sector (from the name of the object). How to get access to it from the code?

I'm already confused myself) The main problem is how to get the value of the sector, if the drum is drawn and all sectors are different?

P.S. The position of the drum can also be calculated by rotation, probably, but it can spin for a long time and the meaning is wild there.

P.P.S. Found hitTestPoint () method. If he really calculates the intersection of the form, and not on the frame, then find out which sector fell even easier. But the question of how to convey the value of the drawn sector remains in force. If you put it in a text field inside a sector, can it be considered?

Please any tips)

    1 answer 1

    A drum is drawn, transformed into a MovieClip with a transformation point (registration) in the center of the drum. Rotation of the drum occurs through the property rotation.

    The final angle of rotation will be in the range from -180 to 180, so there will be no large values. Spin the drum:

    this.addEventListener(Event.Event.ENTER_FRAME, function(){ Circle.rotation += 1; }) 

    The hitTestPoint method calculates collisions as far as I remember from the frame. In order to calculate the shape of a circle, you can use the formula for finding the distance between points and compare with the radius.

    enter image description here

    Total, collision on a round contour:

     Math.sqrt( Math.pow((Mouse.X - Circle.X), 2) + Math.pow((Mouse.Y - Circle.Y), 2) ) < Circle.Radius 

    That is, the distance from the center of the circle to the click point should be less than the radius.


    UPD 1.

    It is best to store the range of degrees for each sector, measuring it initially. For example, var sectors = [-180,-80,20,60,180] . Then it remains only to understand the range in which the angle lies.

     for(var i=0; i<sectors.length - 1; i++){ if(angle >= sectors[i] && angle < sectors[i+1]) trace("сектор "+i+" на барабане"); } 

    UPD 2. You can also try to take corners from the workpiece, optimizing a bit the code above.

    We draw our drum for the application

    enter image description here

    Next, I chose lines to manipulate them as a direction.

    enter image description here

    Well, then it remains to impose them on the drum (then, you can of course hide them under it by sending it to the layer below or by running Arrange-> Send To Back). Having set the necessary angles with the help of the rotation tool and naming somehow rotate1, rotate2, rotate3 [...] .

    enter image description here

    Well, then get the corners of the matter of technology

     var arr = []; for(var i = 1; i <= 3; i++){ arr.push(this["rotate" + i].rotation); } 
    • Here is the final position found, now how can I find out which sector has been chosen?) How to fasten their value to the sectors? Or just immediately calculate and write to the array? Click by the way will not be on the drum itself, but somewhere outside. - Furry Cat
    • Just wanted to avoid manual calculation of the angles) - Furry Cat
    • Fine! Then everything else is generally understandable) I just could not decide on this decision :) Thank you very much) - Furry Cat
    • @FurryCat, you can also fasten the values ​​to the drum through control points, but then you will calculate the angles programmatically through sine-cosine along the point on the arc. Or it seems like there you can rotate a point and get its rotation too later. You can also look to the future by Joba Makar’s book, The Secrets of Game Development in Macromedia Flash MX, there’s a lot of useful code for different games (rotation, collision, physics), but you’ll have to transfer from AS 2.0 to AS 3.0. - Alex Krass
    • @ AlexKrass I still do not understand, if you add an object from the library directly to the frame, and not from the code, can you not define any properties for it? - Furry Cat