I have been able to implement a simple wheel spin and get the angle of rotation as you suggest, i found some code example of this to calculate my angle and display a value which i can spin up or down.
However one of the main issues i want to overcome is to count the numbers of spins and keep on increasing my value (or decreasing). i managed to get some work arounds to mostly count my spins by a simple method, where i add 1 to my count if the angle > 359, ie so i am passing through the start point of my spin. This largely works but if can miss a count if you are not precise enough with your touching and moving of the image.
I was hoping someone out there had already over come this issue and come up with a function to allow multiple spins of a wheel to keep increasing, or decreasing a value.
my function so far is as below. as i display the value of my spin in a text field (myValue), which also acts as an external memory of the last value.
Inside my local touchEvent function i have:
local source = 0
local spinScale = 10 – to allow the spin wheel to have more or less resolution in terms of scale, so here 1 spin = 360 deg = 3600 in user value terms.
– In the began phase, capture some original locations and calculate the original adjustment.
if(event.phase == “began”) then
local dx = event.x - (spinWheel.x)
local dy = event.y - (spinWheel.y)
adjustment = math.atan2(dy,dx) * 180 / math.pi - spinWheel.rotation
– In the moved phase, the player is dragging their finger or stylus around the screen.
– We’ll grab the new event location and rotate the spin wheel accordingly.
elseif(event.phase == “moved”) then
local dx = event.x - spinWheel.x
local dy = event.y - spinWheel.y
spinWheel.rotation = (math.atan2(dy,dx) * 180 / math.pi) - adjustment
source = tonumber(myValue.text)
local numRotations = math.floor(source/spinScale / 359.9)
local numRotations1 = math.ceil(source/spinScale / 360)
if(source/spinScale - dest > 355) then
countspins = countspins + 1
end
if((countspins > numRotations) and (numRotations > 0)) then countspins = numRotations end
if(countspins > math.floor(someMaxValue/(360*spinScale))) then countspins = math.floor(someMaxValue/(360*spinScale)) - 1 end
local spinValue = (360*countspins*spinScale) + dest * spinScale
if(spinValue > someMaxValue) then spinValue = someMaxValue end
– display value of spin in a text field
myValue.text = string.format("%.0f", spinValue)
elseif(event.phase == “ended” or event.phase == “cancelled”) then
– release the focus on the spin wheel
display.getCurrentStage():setFocus( event.target,
As mentioned this largely works, but it can miss count the spins depending on how accurate the touch event is with regards to the image.
Regards
Bruce