Problem with Slider

Hi,

I am trying to use the slider to change the volume of audio playing.

I use this code:
local release = function( event )
print("Clicked ")
audio.setVolume(mySlider.value)
end

local mySlider = slider.newSlider(
{
track = "track.jpg",
thumbDefault = "thumb.png",
thumbOver = "thumbDrag.png",
minValue = 0,
maxValue = 1,
value = 0.5,
onPress = press,
onRelease = release,
}
)

When the "release" function gets called I get this error:

attempt to index global 'mySlider' (a nil value)

I tried to declare mySlider as local in the beginning of the lua file but I get pretty much the same error, about indexing mySlider.

Does someone have a clue about what it is not working?

Thanks

Pierre

"pretty much the same error"?

Can you post your full code in < lua > tags?

Peach

I get the same error. Instead of complaining about the global variable it complains about the local variable but the problem remains that I am trying to index a nil value.

I don't understand what in < lua > tags is but here is the code:

Thanks

---------------------
local ui = require("ui")
require "slider"

local pausebutton
local playbutton
local rewindbutton

local playbuttonPress
local pausebuttonPress
local rewindbuttonPress
--local mySlider

display.setStatusBar( display.HiddenStatusBar )

local background = display.newImage( "background.jpg" )

local Betty = audio.loadStream("bettyreis.mp3")

local playbuttonPress = function( event )
-- playbutton.isVisible=false;
-- pausebutton.isVisible=true;
audio.play(Betty)
print("play")
end

local pausebuttonPress = function( event )
-- playbutton.isVisible=true,
-- pausebutton.isVisible=false,
audio.stop()
print("pause")
end

local rewindbuttonPress = function( event )
-- playbutton.isVisible=true;
-- pausebutton.isVisible=false;
audio.rewind()
audio.stop()
end

local rewindbutton = ui.newButton
{
default = "rewindupsmall.png",
over = "rewinddownsmall.png",
onPress = rewindbuttonPress
}

local playbutton = ui.newButton
{
default = "playup.png",
over = "playdown.png",
onPress = playbuttonPress
}

local pausebutton = ui.newButton
{
default = "pauseupsmall.png",
over = "pausedownsmall.png",
onPress = pausebuttonPress
}

local release = function( event )
print("Clicked ")
audio.setVolume(mySlider.value)
end

local mySlider = slider.newSlider(
{
track = "track.jpg",
thumbDefault = "thumb.png",
thumbOver = "thumbDrag.png",
minValue = 0,
maxValue = 1,
value = 0.5,
onPress = press,
onRelease = release,
}
)

mySlider.x = display.contentWidth / 2;
mySlider.y = 410;

pausebutton.x = 243;
pausebutton.y = 290;
playbutton.x = 160;
playbutton.y = 290;
rewindbutton.x = 215;
rewindbutton.y = 353;

I'm curious about this line:

require "slider"

Are you trying to use the new widget library? If so, it would be:

require "widget"

And then a new slider would be declared by:

widget.newSlider()

That is an older slider implementation. The sliders from the widget library are the only ones that are officially supported, and should be able to be implemented with very minimal code changes on your part:

http://developer.anscamobile.com/reference/index/widgetnewslider

Jon,
Concerning the widget's slider, do you know anything about this? When I added a require("widget")to the settings page of my project to get the slider, the text rendering of my labels changed. Coming back to any previous scene via director (v1.2a), all text was blown up by a factor of 2x. I was using crawlspacelib's display.newText(), so I experimented by switching to your newRetinaText() and this fixed the problem for iPhone and iPad, but not for iPhone4. So I'm now running without the widget library without a problem, but no slider.

Jon, Peach, Ansca!!
Bruce brings up a really good point...if Jonathan is overriding native corona behavior (as he's shown great examples in other posts) within the widget module , this needs to be extensively documented or even better, done privately. It should not be done GLOBALLY for all of the obvious reasons...the most important being that no module should have global side-effects. Please confirm that we don't have to watch out for this when using the widget library.

It turns out that the widget scaling bug I'm seeing is still present in a very simple test program I wrote that includes widget_ios without either the crawlspacelib or beebegames libraries included. My post above unfairly implied that the problem might have been associated with them. Run this code in a project with widget_ios and cycle through the simulator changing window view to iPhone, iPhone4, then iPad. Then uncomment require "widget" and repeat.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--config.lua
 
application = {content = {width = 320, height = 480, scale = "letterbox",},}
 
--main.lua
 
    display.setStatusBar(display.HiddenStatusBar);
 
--      require ("widget")
 
    local mainGroup = display.newGroup();
 
    local bug1 = display.newText("|--Full Size--|", 0, 0, native.systemFont, 30);
    bug1:setReferencePoint(display.CenterReferencePoint);
    bug1.x = display.contentWidth * 0.5 + 2; bug1.y = 50;
    mainGroup:insert(bug1)
 
    local rect1 = display.newRect(0, 0, 200, 40)
    rect1:setFillColor(242, 108, 79)
    rect1:setReferencePoint(display.CenterReferencePoint)
    rect1.x = display.contentWidth * 0.5; rect1.y = 100
    mainGroup:insert(rect1)
 
 
    local bug2 = display.newText("|--Half Size--|", 0, 0, native.systemFont, 30);
    bug2:setReferencePoint(display.CenterReferencePoint);
    bug2.x = display.contentWidth * 0.5; bug2.y = 200;
    mainGroup:insert(bug2)
    bug2.xScale, bug2.yScale = .5, .5
 
    local rect2 = display.newRect(0, 0, 210, 40)
    rect2:setFillColor(242, 108, 79)
    rect2:setReferencePoint(display.CenterReferencePoint)
    rect2.x = display.contentWidth * 0.5; rect2.y = 230
    mainGroup:insert(rect2)
    rect2.xScale, rect2.yScale = .5, .5
 
    local p1 = display.newText("Cycle through simulator,", 0, 0, native.systemFont, 16);
    p1:setReferencePoint(display.CenterReferencePoint);
    p1.x = display.contentWidth * 0.5; p1.y = 300;
    mainGroup:insert(p1)
 
    local p2 = display.newText("changing window view to", 0, 0, native.systemFont, 16);
    p2:setReferencePoint(display.CenterReferencePoint);
    p2.x = display.contentWidth * 0.5; p2.y = 320;
    mainGroup:insert(p2)
 
     local p3 = display.newText("iPhone, iPhone4, then iPad.", 0, 0, native.systemFont, 16);
     p3:setReferencePoint(display.CenterReferencePoint);
     p3.x = display.contentWidth * 0.5; p3.y = 340;
     mainGroup:insert(p3)
 
     local p4 = display.newText('Uncomment require "widget"', 0, 0, native.systemFont, 16);
     p4:setReferencePoint(display.CenterReferencePoint);
     p4.x = display.contentWidth * 0.5; p4.y = 380;
     mainGroup:insert(p4)
 
     local p5 = display.newText('and repeat.', 0, 0, native.systemFont, 16);
     p5:setReferencePoint(display.CenterReferencePoint);
     p5.x = display.contentWidth * 0.5; p5.y = 400;
     mainGroup:insert(p5)

@Bruce: Thank you very much for the test case. I believe I've identified the bug and squashed it. I ran your code you pasted above with my new fixes and everything looks consistent with the commented-out 'require "widget"' line.

The version of widgetlib that's pushed to the core will have the fix.

Thanks again for identifying this bug!

views:2211 update:2011/9/26 8:07:09
corona forums © 2003-2011