Graphics.newimagesheet Fails To Find Image

What am I doing wrong?

My code is as follow:

local widget = require “widget”

function PlayButtonDisp()

local ix,iy,iz = 50,110,1

local filename = “C:/PlayButton.png”

local options =

{

    width = ix,

    height = iy,

    numFrames = iz

}

local PlayButtonSheet = graphics.newImageSheet( filename, options )

local PlayButton = widget.newButton{

    id = “PlayBtn”,

    sheet = PlayButtonSheet,

    left = 250 * SFx + SOSx,

    top = 10 * SFx + SOSx,

    label = “Play”,

    width = 50, height = 110,

    cornerRadius = 8,

    onEvent = onPlayButtonEvent

}

end

I get a message: WARNING: Failed to find image(C:/PlayButton.png)

I have tried every combination of file names and locations without success!

Your images have to be in the same folder as your main.lua or a sub-folder in the folder with your main.lua.  You cannot reference things in Windows terms on other drives.  By saying C:/PlayButton.png, you’re telling the app to look at the top level of your C drive for the image.  Mobile apps cannot work outside their “sandbox”.  Put PlayButton.png in the same folder as your main.lua and take off the C:/

Hi Rob,

I made sure the file was in the same folder as the main.lua, I had tried this previously

Changed code to :

local widget = require “widget”

function PlayButtonDisp()

local ix,iy,iz = 50,110,1

local filename = “PlayButton.png”

local options =

{

    width = ix,

    height = iy,

    numFrames = iz

}

local PlayButtonSheet = graphics.newImageSheet( filename, options )

local PlayButton = widget.newButton{

    id = “PlayBtn”,

    sheet = PlayButtonSheet,

    left = 250 * SFx + SOSx,

    top = 10 * SFx + SOSx,

    label = “Play”,

    width = 50, height = 110,

    cornerRadius = 8,

    onEvent = onPlayButtonEvent

}

end

I then get :

ERROR: bad argument #2 to display.newImageRect(): filename or image sheet expected, but got nil.

Runtime error

               ?:0: attempt to index filed ‘default’ (a nil value)

plus a whole lot more.

any suggestions?

I don’t know if this has anything to do with the error or not, but you are missing some required parameters for widget.newButton when using an imageSheet.  The docs say:

ImageSheet. Specify the image sheet object for the widget button (instead of graphics files). Requires defaultIndex, overIndex, width, and height to also be set.

So you need to provide defaultIndex, overIndex and the width and height of each button.   Try providing those and see if that fixes it.  Is PlayButton.png in the same folder as your main.lua?

Sometimes it can be as simple as case-sensitivity.  If the filename does not match ‘case’ or is spelled wrong it will fail to find the image and thus nil, and that may result in the message 'bad argument #2 … ’    double check the filename as it is spelled and case-sensitivity.  I know that is basic, but sometimes thinking about all the other things that can be wrong, it is easy to miss those things.  I know I have.

Not saying for sure this is the issue, it is one of the first things I always check, I am prone to serious tying errors.

Good luck! 

Your images have to be in the same folder as your main.lua or a sub-folder in the folder with your main.lua.  You cannot reference things in Windows terms on other drives.  By saying C:/PlayButton.png, you’re telling the app to look at the top level of your C drive for the image.  Mobile apps cannot work outside their “sandbox”.  Put PlayButton.png in the same folder as your main.lua and take off the C:/

Hi Rob,

I made sure the file was in the same folder as the main.lua, I had tried this previously

Changed code to :

local widget = require “widget”

function PlayButtonDisp()

local ix,iy,iz = 50,110,1

local filename = “PlayButton.png”

local options =

{

    width = ix,

    height = iy,

    numFrames = iz

}

local PlayButtonSheet = graphics.newImageSheet( filename, options )

local PlayButton = widget.newButton{

    id = “PlayBtn”,

    sheet = PlayButtonSheet,

    left = 250 * SFx + SOSx,

    top = 10 * SFx + SOSx,

    label = “Play”,

    width = 50, height = 110,

    cornerRadius = 8,

    onEvent = onPlayButtonEvent

}

end

I then get :

ERROR: bad argument #2 to display.newImageRect(): filename or image sheet expected, but got nil.

Runtime error

               ?:0: attempt to index filed ‘default’ (a nil value)

plus a whole lot more.

any suggestions?

I don’t know if this has anything to do with the error or not, but you are missing some required parameters for widget.newButton when using an imageSheet.  The docs say:

ImageSheet. Specify the image sheet object for the widget button (instead of graphics files). Requires defaultIndex, overIndex, width, and height to also be set.

So you need to provide defaultIndex, overIndex and the width and height of each button.   Try providing those and see if that fixes it.  Is PlayButton.png in the same folder as your main.lua?

Sometimes it can be as simple as case-sensitivity.  If the filename does not match ‘case’ or is spelled wrong it will fail to find the image and thus nil, and that may result in the message 'bad argument #2 … ’    double check the filename as it is spelled and case-sensitivity.  I know that is basic, but sometimes thinking about all the other things that can be wrong, it is easy to miss those things.  I know I have.

Not saying for sure this is the issue, it is one of the first things I always check, I am prone to serious tying errors.

Good luck!