
Share Your Code
Browse Code
- All (915)
-
(95)
-
(87)
-
(45)
-
(4)
-
(42)
-
(136)
-
(22)
-
(41)
-
(13)
-
(66)
-
(153)
-
(211)
TileBG - Tile and manage a image on the display background
I'm still on my demo time, trying to get the hang of this Corona thing... loving every minute!
I was trying to do what I thought would be simple...tile an image on the background of the display. And thats what this does: It detects your display content height and attempts to calculate the minimum number of tiles needed (based on the size of the provided background tile.)
This is simple to use:
1) Include the function in your code (probably main.lua)
require( "tilebg" )
2) Now you can begin to use the function in your code.
local bg = tileBG() --add a new background, use the default settings
The function takes 3 optional arguments:
bgImageFile -> the filename of the background image to tile
bgWidth -> the width of the bgImagefile
bgHeight -> the height of the bgImagefile
Default values
If the function is run without any arguments then it will use these values:
1 2 3 | bgImageFile = tileBG.jpg bgWidth = 100 bgHeight = 100 |
In other words, you can quickly use this code if you have an image file called tileBG.jpg in your project folder, which is 100x100 pixels in size.
Its best to store the background in a local object, as demoed above:
local bg = tileBG()
Doing this, you can then use an build in method to self-destruct the tiles:
bg.removeBG()
Thats all there is to it. There is a full demo on the github repo. To make things easier, the function logs everything it does to the console...
Here is the include file:
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | ------------------------------------------------------------------------ -- ================== -- tilebg.lua v1.0 -- ================== -- -- This creates a function tileBG() which will tile an image on the -- background of the display. It detects your display content height -- and attempts to calculate the minimum number of tiles needed (based -- on the size of the provided background tile.) ------------------------------------------------------------------------ -- --------- -- USAGE -- --------- -- -- 1) Include the function in your code (probably main.lua) -- -- require( "tilebg" ) -- -- -- 2) Now you can begin to use the function in your code. -- -- local bg = tileBG() --add a new backgroud, use the default settings -- -- The function takes 3 optional arguments: -- -- bgImageFile - the filename of the background image to tile -- bgWidth - the width of the bgImagefile -- bgHeight - the height of the bgImagefile -- -- Default values -- If the function is run without any arguments then it will use these values: -- -- bgImageFile = tileBG.jpg -- bgWidth = 100 -- bgHeight = 100 -- -- In other words, you can quickly use this code if you have an image file -- called tileBG.jpg in your project folder, which is 100x100 pixels in size. -- ------------------------------------------------------------------------ -- --------- -- CHANGELOG -- --------- -- -- 1.0 - First release -- ------------------------------------------------------------------------ function tileBG(bgImageFile,bgWidth,bgHeight) print ("TileBG started.") --Set the defaults, for rapid calls and just in case bgImageFile = bgImageFile or "tileBG.jpg" bgWidth = bgWidth or 100 bgHeight = bgHeight or 100 --Calculate how manytimes to repeat the image based on display size, math.ceil roundup for loops local bgRepeatX=math.ceil(display.contentWidth/bgWidth) --number of times to repeat bg on x axis local bgRepeatY=math.ceil(display.contentHeight/bgHeight) --number of times to repeat bg on y axis local bgCount = bgRepeatX*bgRepeatY --total bg images placed --Useful debug info print ("TileBG: display.contentWidth: " .. display.contentWidth) print ("TileBG: display.contentHeight: " .. display.contentHeight) print ("TileBG: bgImageFile: " .. bgImageFile) print ("TileBG: bgWidth: " .. bgWidth) print ("TileBG: bgHeight: " .. bgHeight) print ("TileBG: bgRepeatX: " .. bgRepeatX .. "(number of times to repeat bg on X axis)") print ("TileBG: bgRepeatY: " .. bgRepeatY .. "(number of times to repeat bg on Y axis)") --Loop through and place the bg image local bgImages = {} --images table local bgLocationY = 0 --start at origin local bgLocationX = 0 --start at origin local tiledBG=display.newGroup() for bgY=1,bgRepeatY do local bgLocationX = 0 --reset x position each time we go to next row for bgX=1,bgRepeatX do tiledBG:insert(display.newImage( bgImageFile, bgLocationX, bgLocationY )) print ("TileBG: Placed tile at x,y: " .. bgLocationX .. "," .. bgLocationY) bgLocationX=bgLocationX+bgWidth end bgLocationY=bgLocationY+bgHeight end tiledBG:toBack() --make sure its on the background print ("TileBG: Placed " .. bgCount .. " images on background.") print ( "textureMemoryUsed: " .. system.getInfo( "textureMemoryUsed" ) ) --a function to cleanup the bg function tiledBG.removeBG () if tiledBG.numChildren then print ("TileBG: Removing " .. tiledBG.numChildren .. " images from background.") while tiledBG.numChildren > 0 do -- we have have tiles left in the tiledbg group, so first clean them out tiledBG:remove(tiledBG.numChildren) -- clean out the last member of the group (work from the top down!) end end tiledBG:removeSelf() --whats left is now an empty group. remove it. print ("TileBG: Removed background.") return end --Send everything back to mama return tiledBG end |
- Type:
- Tags:
