system.vibrates on and off?

So now I have a vibration checkbox button, how to make it such that when the button is checked and in game when my health decrease it vibrates and when the checkbox is not checked, it does not vibrates in game?

Anyone has any suggestion?

Simply call system.vibrate() to vibrate the device.

http://docs.coronalabs.com/api/library/system/vibrate.html

Ya I am using system.vibrate to vibrate the device but my problem is that in my game, I have a setting menu where I can turn vibration on and off, so what am I suppose to do so that if the vibration button is not checked the device will not vibrate in the game and similar for if I check the vivration button

And Thanks for the reply

Bumps

Any ideas???

surround your vibrate command with an if then end statement that checks the state of your option

Oh right! Thanks for the reply

but I met with some problems when adding the code

I have a settingmenu.lua where the checkbox is at and a game scene.lua where the device will vibrate will my health reduce

--Handles the press event for the checkbox local function onSwitchPress( event ) -- body local switch = event.target print("Switch with ID '"..switch.id.."' is on:" ..tostring(switch.isOn)) --Enable vibration when the check box for Vibrates is checked. if (switch.isOn) then -- If the checkbox is checked, then run this code. print( "Switch is on, Vibrate the device" ) system.vibrate() -- Vibrate the device end end

For this it works but it does not works in the gamescene

Game Scene.lua:

 elseif event.other.IsPit then if player.invul == false then local disableInvul = function() player.invul = false pauseBtn.alpha = 1 end decreaseHealth(1) -- Health -1 if (switch.isOn) then -- Check if the checkbox is on print( "Vibrate the device" ) system.vibrate() -- Vibrate the device end

It gives an error like this “attempt to call “switch” a nil value” (It does not run the if statement, it gives an error when entering the statement)

Am I missing out something??

Sorry, just started Corona 2 weeks ago.

your switch variable is local to the settings file. read this it may help you solve the problem.
http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Thanks Man!!!

Helped me alot! 

Cheers 

welcome

Came across this post and decided to test it out but I met some problem.

Here’s my gamedata.lua:

M = {}; M.switch = {} 

Game scene.lua:

GetHit(1); -- Minus the hp by 1 if (M.switch.isOn) then print("Vibrate") system.vibrate( ) end

No errors but it did not get into the if statement.

Any advice?

need to return M at end of game data file

Yes I did return M at the end of my game data file but every time my hp minus 1 it does not print out the “Vibrate”

how are you requiring the gamedata file

it should be something like this

local gamedata = require’gamedata’

  if gamedata.switch.isOn then …

So its "if(gameData.switch.isOn) then …

So the way i wrote the code in the gamedata is correct?

OK will try it out.

Strange enough… it does not work also.
Any ideas? There’s no error but when I drop into a pit and my health decrease, it does not print out the debug log for vibrate. It seems that it does not know if the vibration button is being checked or not? Any ideas how to check if the checkbox is checked?

So now the funny part…
Before the if statement I wrote these : <print (gameData.switch.isOn)>
And it gives me nil?! Shldnt it gives me true or false???

Its okay now! Solved the problem
Just add
M.switch.isOn in the gamedata.lua.
But now it does not check whether is the vibration button is checked or not. It just vibrate.
Any idea?

Anyone has any suggestion?

Simply call system.vibrate() to vibrate the device.

http://docs.coronalabs.com/api/library/system/vibrate.html