Reputation: 9251
I have relatively simple but stupid question, but surprisingly could not find solution :(
I am new to Corona SDK and trying to display an image. All images are place in directory images
directory relative to main.lua
.
Presently i am doing like this
local IMAGE_DIR = "images";
local bg = display.newImageRect( IMAGE_DIR.. "/bg.png", 570, 360 )
mainScreenGroup:insert(bg)
and it is working perfectly. But according to documentation, here is the syntax
display.newImageRect( [parentGroup,] filename, [baseDirectory] width, height )
i have found that instead of concatenate IMAGE_DIR
, i can specify [baseDirectory]
and instead of manually inserting into group mainScreenGroup
, i can specify [parentGroup,]
all in one line. But i am not being able to make it work. I have tried following.
local bg = display.newImageRect( [mainScreenGroup,] "bg.png", [IMAGE_DIR] 570, 360 )
local bg = display.newImageRect( [mainScreenGroup], "bg.png", [IMAGE_DIR], 570, 360 )
local bg = display.newImageRect( [mainScreenGroup], "/bg.png", [IMAGE_DIR], 570, 360 )
local bg = display.newImageRect( mainScreenGroup, "/bg.png", IMAGE_DIR, 570, 360 )
local bg = display.newImageRect( mainScreenGroup, "bg.png", IMAGE_DIR, 570, 360 )
But nothing of the above 5 statements working. Am i doing wrong? Any help is appreciated.
Upvotes: 1
Views: 1638
Reputation: 8598
You can't use IMAGE_DIR as your baseDirectory, it has to be one of 4 mentioned in the docs here and here:
In your case, you will still need to append IMAGE_DIR, but you can get rid of inserting bg to group, so your code:
local IMAGE_DIR = "images/";
local bg = display.newImageRect( IMAGE_DIR.. "/bg.png", 570, 360 )
mainScreenGroup:insert(bg)
can be changed to:
local IMAGE_DIR = "images/";
local bg = display.newImageRect( mainScreenGroup, IMAGE_DIR.."/bg.png", 570, 360 )
As mentioned by Teddy Engel, you need to be careful if you want to use system.ResourceDirectory on Android - see docs.
Upvotes: 0
Reputation: 1006
The baseDirectory parameter cannot be just any value, you can only fill it with values mentionned here: http://docs.coronalabs.com/api/library/system/pathForFile.html
From the newImageRect doc:
baseDirectory (optional)
Constant. Path to load the image data from filename. Default is system.ResourceDirectory (project folder; same location as main.lua). See system.pathForFile() for valid values).
So in your initial example, what the project would do is look for the system.ResourceDirectory/images/bg.png (system.ResourceDirectory being the same folder as the one where your main.lua is).
If it still doesn't show, check the gotchas on newImageRect to make sure your image can load.
Also, for Android you cannot rely on using system.ResourceDirectory:
Android
File access in Corona is based on the underlining operating system which varies by platform. On iOS devices, you can access files in all of the directories described above. On Android, however, there is no literal system.ResourceDirectory because all resource files reside inside a compressed APK file.
Corona allows direct loading of images and audio files using the appropriate APIs, but it has limited access to resource files on Android using the file I/O APIs. Specifically, the following types can not be read from the resources directory: .html, .htm., .3gp, .m4v, .mp4, .png, .jpg, and .ttf.
Because of this limitation, if you have files of these types bundled in the core directory that you need to copy to another directory, you must change the file name so it can be accessed by the file I/O APIs. For example, if you want to move cat.png from the resource directory to the documents directory, it must be renamed cat.png.txt to be copied. See http://docs.coronalabs.com/guide/data/readWriteFiles/index.html for details.
Upvotes: 4
Reputation: 776
In your last example you should not have "/" before the file name bg.png
.
local bg = display.newImageRect( mainScreenGroup, "bg.png", IMAGE_DIR, 570, 360 )
Upvotes: 2