Alright, so at the moment I have a couple audio files - Tank_idle and Tank_move - and depending on the current phase of the joystick’s touch event, one of them will be looping.
For example, on the “began” phase (the user has their finger on the joystick but has not yet initiated the moved phase) the idle sound will be looping.
Now my question is how can I code it to play say, the idle sound, as long as the joystick-thumb-knob is a certain distance from the center? and same for when the joystick-thumb-knob is at maximum distance from the center, play the ‘Tank_move’ sound?
I’m not too familiar with how the math functions/calls work, so I’m not quite sure what to add&or change to achieve what I want.
Here is some code from the joystick module I use (you’ll notice the lines I’ve added to play the sounds)
--------------------------------------------- -- METHOD: MOVE AN OBJECT --------------------------------------------- function Group:move(Obj, maxSpeed, rotate) if rotate == true then Obj.rotation = self.angle + 90 end Obj.x = Obj.x + Cos( Rad(self.angle-90) ) \* (maxSpeed \* self.percent) Obj.y = Obj.y + Sin( Rad(self.angle-90) ) \* (maxSpeed \* self.percent) end --------------------------------------------- -- GETTER METHODS --------------------------------------------- function Group:getDistance() return self.distance end function Group:getPercent () return self.percent end function Group:getAngle () return Ceil(self.angle) end --------------------------------------------- -- HANDLER: ON DRAG --------------------------------------------- Group.onDrag = function ( event ) local T = event.target -- THUMB local S = T.parent -- STICK local phase = event.phase local ex,ey = S:contentToLocal(event.x, event.y) ex = ex - T.x0 ey = ey - T.y0 if "began" == phase then audio.setVolume(1.0, {channel = 2}) Tank\_idleChannel = audio.play( Tank\_idle, { channel = 2, loops = -1, fadein = 1300 }); Particles.StartEmitter("PTEmitter1") Particles.StartEmitter("PTEmitter2") if S.Timer ~= nil then timer.cancel(S.Timer); S.Timer = nil end display.getCurrentStage():setFocus( T, event.id ) T.isFocus = true -- STORE INITIAL POSITION T.x0 = ex - T.x T.y0 = ey - T.y elseif T.isFocus then if "moved" == phase then audio.fadeOut( { channel = 2, time = 800 } ) audio.setVolume(1.0, {channel = 3}) Tank\_moveChannel = audio.play( Tank\_move, { channel = 3, loops = -1, fadein = 1300 }); ----------- S.distance = Sqr (ex\*ex + ey\*ey) if S.distance \> S.maxDist then S.distance = S.maxDist end S.angle = ( (Atan2( ex-0,ey-0 )\*180 / Pi) - 180 ) \* -1 S.percent = S.distance / S.maxDist ----------- T.x = Cos( Rad(S.angle-90) ) \* (S.maxDist \* S.percent) T.y = Sin( Rad(S.angle-90) ) \* (S.maxDist \* S.percent) elseif "ended"== phase or "cancelled" == phase then audio.fadeOut( { channel = 3, time = 800 } ) --audio.fadeOut( { channel = 2, time = 800 } ) Particles.StopEmitter("PTEmitter1") Particles.StopEmitter("PTEmitter2") T.x0 = 0 T.y0 = 0 display.getCurrentStage():setFocus( T, nil) T.isFocus = false S.Timer = timer.performWithDelay( 33, S.onRelease, 0 ) S.Timer.MyStick = S end end -- STOP FURTHER PROPAGATION OF TOUCH EVENT! return true end
This isn’t all the code, just the parts that I thought were relevant. If you want, I can post the entire module.
My other question is regarding what I like to call “dynamic audio.”
Like, in games with cars, tanks, other vehicles etc… how the sound changes depending on how long the user holds down the action-button – e.g. when you’re driving a car and you hear the gears shift and the tone of the engine change.
How is this achieved, and is it possible with Corona?
If it is, I’d like to incorporate this into my own game so as the user drags the joystick the sound will change in accordance with the speed.
All comments and suggestions are welcome.
-Saer
Edit: - in reference to my dynamic audio question -
Is it just a bunch of ‘if, then’ statements?
e.g. If joystick_distance <= 30º then play idle sound. if joystick_distance > 30 then play medium speed sound etc… etc…
Is that the general idea? or am I way off…