Hello,
I am trying to make an object I have created in my project play audio only when the user rotates it. I do not want the audio to continue playing if the user removes their finger from the object, or if they pause in the middle of rotating it.
I have put together something that almost works, except sometimes the audio continues to play after the user removes their finger from the object; sometimes too while the user pauses in the middle of rotating it.
Initially, I set the scene with the audio playing, and have the volume for this channel set to 0.
Here’s my code:
local circle1 = display.newCircle( 0, 0, 66 ) circle1:setFillColor( 0, 0, 255 ) circle1.x = display.contentCenterX - 166 circle1.y = display.contentCenterY - 82 circle1.alpha = 0.6 group:insert(circle1) physics.addBody( circle1, "static" ) local function rotateCircle1(event) --store initial position of finger local t = event.target local phase = event.phase if (phase == "began") then t.x1 = event.x t.y1 = event.y t:toFront(); display.getCurrentStage():setFocus( t, id ); t.isFocus = true elseif (phase == "moved") then t.x2 = event.x t.y2 = event.y angle1 = 180/math.pi \* math.atan2(t.y1 - t.y , t.x1 - t.x) angle2 = 180/math.pi \* math.atan2(t.y2 - t.y , t.x2 - t.x) print("angle1 = "..angle1) rotationAmt = angle1 - angle2 --rotate it t.rotation = t.rotation - rotationAmt print ("t.rotation = "..t.rotation) t.x1 = t.x2 t.y1 = t.y2 if rotationAmt == 0 then audio.fade( { channel=1, time=150, volume=0 } ) end if rotationAmt \> 2.5 or rotationAmt \< -2.5 then audio.fade( { channel=1, time=150, volume=1 } ) else audio.fade( { channel=1, time=150, volume=0 } ) end elseif (phase == "cancelled" or phase == "ended") then audio.fade( { channel=1, time=150, volume=0 } ) display.getCurrentStage():setFocus( t, nil ) t.isFocus = false end return true end circle1:addEventListener("touch", rotateCircle1)
That is the best I have come up with, and it just doesn’t quite work.
I have been banging my head against these forums and all the documentation trying to figure this out!
Can anyone help me!?
–I would also like to add that this is my first post, and I am so appreciative of all the tutorials, documentation, and help around the website and forums! Corona SDK is incredible! I’m kind of a newb, but I have learned so much in just a couple of months, and I’m almost ready to release my first app. I’m so excited, and I’m having tons of fun! Thanks for everything Corona team and community!
-Daniel