Hey all,
I wrote some code to handle making TV-like static.
I stat by drawing a white background and then create a series of black squares over the white. When I refresh I randomly set the black squares to be either visible or not visible.
I pass in a size to be used for the black squares. If I use a width of 2 in a static window that’s 320x320 then it runs fine but doesn’t look very real. If I set it to 1 pixel wide then it runs very slowly, but it looks like it will be a great (and realistic) effect!
Anyways I’m wondering if anyone has any suggestions on how to improve the performance of my static class?
One thing I’ve thought about is not trying iterate through every square but randomly select a few and toggle them, but I have doubts that this will look as good.
Here is the class “Graphics_StaticWindow.lua”:
[lua]local static = {}
local staticWindow = display.newGroup()
local getRand = math.random
local background
local blackDots = {}
local dotCountHorizontal
local dotCountVertical
local function newBlackSquare( blackDotSize )
local blackDot = display.newRect( 0,0,blackDotSize,blackDotSize )
blackDot:setFillColor( 0,0,0 )
return blackDot
end
static.newStaticWindow = function( width,height,dotSize )
staticWindow.windowWidth = width or 480
staticWindow.windowHeight = height or 320
staticWindow.blackDotSize = dotSize or 2
dotCountHorizontal = math.floor( staticWindow.windowWidth/staticWindow.blackDotSize )
dotCountVertical = math.floor( staticWindow.windowHeight/staticWindow.blackDotSize )
local currentDotX, currentDotY = 0,0
background = display.newRect( 0, 0, staticWindow.windowWidth, staticWindow.windowHeight )
background:setFillColor( 255,255,255 )
staticWindow:insert( background )
for row = 1, dotCountVertical do
blackDots[row] = {}
for col = 1, dotCountHorizontal do
blackDots[row][col] = newBlackSquare( staticWindow.blackDotSize )
blackDots[row][col].x = currentDotX
blackDots[row][col].y = currentDotY
currentDotX = currentDotX+staticWindow.blackDotSize
staticWindow:insert( blackDots[row][col] )
end
currentDotX = 0
currentDotY = currentDotY+staticWindow.blackDotSize
end
return staticWindow
end
static.refresh = function( )
local rand
for row = 1, dotCountVertical do
for col = 1, dotCountHorizontal do
rand = getRand( 1,100 )
blackDots[row][col].isVisible = (rand>50)
end
end
end
return static[/lua]
And how it’s used in the game:
[lua]local static = require( “Graphics_StaticWindow” )
local staticWindow = static.newStaticWindow( 320,320, 2)
group:insert( staticWindow )[/lua]
Then I refresh it by calling this in an “enterFrame” listener
[lua]static.refresh( )[/lua] [import]uid: 181948 topic_id: 35590 reply_id: 335590[/import]