I’ve got a handle on my game, and this concept is driving me crazy.
Lets say I have a stereo with a volume dial. It rotates left and right. I want to play a single clicking sound for every degree (or every couple degrees, if it’s too noisy considering there are 360 degrees I may settle on every 5 or 10)
I dove into the Lua 5.1 reference guide and pulled up the “relational operators” since I am still kind of new to programming (all programming never touched code before June 2011).
<
>
<=
>=
~=
I was trying to do something in a function like this (full code below, but here is the snippet):
local function clicker ()
print ("clickity click")
if testobject.rotation ~= 0 then print ("1 more degree")
-- So I read ~= equals "negation of equality" OK what the hell does that mean? lol @ me.
--audio.play(click)
print (testobject.rotation)
end
end
So I’m printing out the rotation and it’s not in round numbers as expected but I want to take action based on whole rounded numbers such as 1,2,3 for the number of whole degrees and not decimal places. In my head and paper I know what I want, I just can’t seem to code it worth a damn haha.
So if the volume dial rotates left or right by say 5 degrees, every 5 degrees it would click. The problem I see is the knob turns from 0 starting position gets to 5 clicks and then stops clicking. So I have to figure out a way to…uhh I don’t know something like add 5 to the degrees or take away 5 to the degrees or something that way it plays every 5 degrees.
That was probably completely off, but I don’t know what I’m talking about most of the time 
My mind says “table, or array” or something. What do you think? Any help is greatly appreciated, spent a few hours on this so far and I think this beat me haha.
Here is the sample code, simply drag the circle left or right and it spins around using a simple joint.
-Nick
[code]
local physics = require “physics”
physics.start(true)
physics.setDrawMode(“debug”)
–DECLARE WIDTH AND HEIGHT SHORTCUT VARIABLES
local _W = display.contentWidth
local _H = display.contentHeight
local refpoint
local testobject
–local click = audio.loadSound (“click.wav”)
print (“wtfbbq kyle this code is pissing me off and I want to die”)
local refpoint = display.newCircle (_W/2, _H/2, 5)
physics.addBody(refpoint, “static”, {isSensor = true, radius = 5})
refpoint:setFillColor (255,0,0)
refpoint.alpha = 1
–======================================================================================
–testobject - ATTACHES TO REFERENCE POINT VIA “PIVOT”
local testobject = display.newCircle (refpoint.x, refpoint.y, 150)
physics.addBody( testobject, {radius = 150})
testobject.isSleepingAllowed = false
testobject.alpha = 1
testobject.x = refpoint.x
testobject.y = refpoint.y
–Some stuff I was messing around with, trying to capture numbers
–and to get degrees or something…grr
local av = testobject.angularVelocity
local ad = testobject.angularDamping
local lv = testobject.LinearVelocity
local r = testobject.rotation
–======================================================================================
–PIVOT JOINT THAT CONNECTS THE REFERENCE POINT TO testobject
local testobjectJoint = physics.newJoint ( “pivot”, refpoint, testobject, refpoint.x, refpoint.y)
–X and Y touch accurate – no “snapping”
local function dragBody( event )
–print (“testobject drag started”)
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true
–Change body to dynamic while we are moving the testobject around
event.target.bodyType = “dynamic”
– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )
elseif body.isFocus then
if “moved” == phase then
– Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
–After the event ends, change body back to static.
event.target.bodyType = “dynamic”
– Remove the joint when the touch ends
body.tempJoint:removeSelf()
–print (“testobject drag ended”)
end
end
– Stop further propagation of touch event
return true
end
local function clicker ()
print (“clickity click”)
if testobject.rotation ~= 0 then print (“1 more degree”)
–audio.play(click)
end
end
testobject:addEventListener( “touch”, clicker )
testobject:addEventListener( “touch”, dragBody )
[/code] [import]uid: 61600 topic_id: 27557 reply_id: 327557[/import]
