Sound Effects, Background Music, Events & Channels

Hi Everyone,

So I’ve built TWO widget.newSlider() as toggle buttons to adjust the volume up and down.
Individually, they work fine.

The first is to control the background music (Channel = 1)
The other to control all sound effects volume (Channel = 2)

The problem is they both fire an event, which is then effecting both widgets.
As they are widgets, the event() is not able to event.target.name.
I’ve tried if statements to identify which channel is which event, using this…
audio.getVolume( channel=1)
but no luck.

How do I separate the logic of the event, so I can determine which widgets function responds?

		btnSliderMusic = widget.newSlider( {
			sheet = graphics.newImageSheet( "Buttons/widgetBtnSlider.png", modAdmin.btnSliderOptions ),
			leftFrame = 1,
			middleFrame = 2,
			rightFrame = 3,
			fillFrame = 4,
			frameWidth = 18,
			frameHeight = 32,
			handleFrame = 5,
			handleWidth = 32,
			handleHeight = 32,
			x = display.contentCenterX,
			y = display.contentCenterY,
			orientation = "horizontal",
			width = 250,
			value = musicVolumeIn,
			listener = adjustMusicVolume
			} 
		)
		btnSliderMusic.name = "btnSliderMusic"
		btnSliderSFX = widget.newSlider( {
			sheet = graphics.newImageSheet( "Buttons/widgetBtnSlider.png", modAdmin.btnSliderOptions ),
			leftFrame = 1,
			middleFrame = 2,
			rightFrame = 3,
			fillFrame = 4,
			frameWidth = 18,
			frameHeight = 32,
			handleFrame = 5,
			handleWidth = 32,
			handleHeight = 32,
			x = display.contentCenterX,
			y = display.contentCenterY,
			orientation = "horizontal",
			width = 250,
			value = musicSFXIn,
			listener = adjustSFXVolume
			} 
		)
local function adjustMusicVolume( event )
	print("adjustMusicVolume event.name: "..event.name)
	if ( event.phase == "began" ) then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
		lblMusicVolume.text = "Music Volume: " .. event.value .. "%"
		setVolumeMusic = event.value/100 -- devide value by 100 to get a 0. number
		audio.setVolume( setVolumeMusic, { channel=1 } ) -- dedicated channel control
	elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
		display.getCurrentStage():setFocus( nil )
		event.target.isFocus = false
	end
end

local function adjustSFXVolume( event )
	print("adjustSFXVolume event.name: "..event.name)
	if ( event.phase == "began" ) then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
		lblMusicSFX.text = "SFX Volume: " .. event.value .. "%"
		setVolumeSFX = event.value/100 -- devide value by 100 to get a 0. number
		audio.setVolume( setVolumeSFX, { channel=2 } ) -- all channels remaining
	elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
		display.getCurrentStage():setFocus( nil )
		event.target.isFocus = false
	end
end

Thanks

Instead of event.target.name have you tried adding id value when creating the widgets? I’m looking at the Widget sample from the simulator and that’s how it’s handled there. Sample code here:

local function sliderListener( event )
		statusText.text = event.target.id .. "\nslider.value = " .. event.value .. " (%)"
	end
	
	-- Create a horizontal slider
	local horizontalSlider = widget.newSlider {
		left = 80,
		top = 285,
		width = 140,
		id = "Horizontal Slider",
		listener = sliderListener
	}
	sceneGroup:insert( horizontalSlider )

	-- Create a vertical slider
	local verticalSlider = widget.newSlider {
		left = 20,
		top = 275,
		height = 140,
		value = 80,
		id = "Vertical Slider",
		orientation = "vertical",
		listener = sliderListener,
	}
	sceneGroup:insert( verticalSlider )

no i had not - thanks will try

Both widgets have separate listeners so they will not affect each other - unless you have other code not listed here?

Also your code will not work. You update/set values on ended not began.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.