Pixel Vision 8 BackgroundColor() Lua Tutorial
The background color is used to fill the screen when clearing the display. You can use this method to read or update the background color at runtime. When calling BackgroundColor()
without an argument, it returns the current background color offset as an int
. You can pass in an optional int
to shift the background color by calling BackgroundColor(offset),
where offset
is any valid between 0 and 255.
Passing in a value such as -1
, or one that is out of range will default to the first system color unless the ColorChip
’sdebugColor
property is set to true. In debug color mode, any out of bounds color IDs will display Magenta (#ff00ff
), which is the engine's default transparent color.
Usage
BackgroundColor ( id )
Arguments
Name | Value | Description |
id | int | This argument is optional. Supply an int to offset the default background color value by. |
Returns
Value | Description |
int | This method returns the current background color offset. If no color exists, it returns 0 which is the default background color offset. |
BackgroundColor Example
In this example, we will display the default background color on the display, then change it, and redraw the new value below it.
Running this code will output the following:
Learn more about making Pixel Vision 8 games by checking out the docs.
Step 1
Create a new file called code.lua.{1}
in your project folder.
Step 2
Create a new function
called Init()
:
01 function Init()
02
03 end
Step 3
Add the following code to the script
:
02 DrawText("BackgroundColor()", 1, 1, DrawMode.Tile, "large", 15)
03 DrawText("Lua Example", 8, 16, DrawMode.TilemapCache, "medium", 15, -4)
Example Title
Step 4
Create a new local
variable called defaultColor
inside the script
:
04 local defaultColor = BackgroundColor()
Get the current background color
Step 5
Add the following code to the script
:
05 DrawText("Default Color " .. defaultColor, 1, 4, DrawMode.Tile, "large", 15)
Draw the default background color ID to the display
Step 6
Create a new local
variable called newColor
inside the script
:
06 local newColor = BackgroundColor(1)
Here we are manually changing the background color
Step 7
Add the following code to the script
:
07 DrawText("New Color " .. newColor, 1, 6, DrawMode.Tile, "large", 15)
Draw the new color ID to the display
Step 8
Create a new function
called Draw()
:
09 function Draw()
10
11 end
Step 9
Add the following code to the script
:
10 RedrawDisplay()
Redraw the display
Final Code
When you are done, you should have the following code in the code.lua
file:
01 function Init()
02 DrawText("BackgroundColor()", 1, 1, DrawMode.Tile, "large", 15)
03 DrawText("Lua Example", 8, 16, DrawMode.TilemapCache, "medium", 15, -4)
04 local defaultColor = BackgroundColor()
05 DrawText("Default Color " .. defaultColor, 1, 4, DrawMode.Tile, "large", 15)
06 local newColor = BackgroundColor(1)
07 DrawText("New Color " .. newColor, 1, 6, DrawMode.Tile, "large", 15)
08 end
09 function Draw()
10 RedrawDisplay()
11 end