Lightning in Corona

My partner and I are working on putting together a new educational game project for the Department of Education at a University here. We are going to be creating a bunch of mini games each with a small academic objective. We needed a way to create cool lightning and we didn’t want to do it with textures.

After some googling I found this article.  It was easy enough to implement the algorithm in Corona and I’m pretty happy with the effect.  If you’d like to have the Cocos2D implementation, you can find that here on the Cocos2D forums.

 local curDetail = 1

function drawLightning(x1, y1, x2, y2, displace)
	if displace < curDetail then

		line = display.newLine(x1, y1, x2, y2)
		line:setColor(255, 255, 255)
		line.width = 25
		line.alpha = 0.05
		
		line = display.newLine(x1, y1, x2, y2)
		line:setColor(255, 255, 255)
		line.width = 9
		line.alpha = 0.1
		
		line = display.newLine(x1, y1, x2, y2)
		line:setColor(255, 255, 255)
		line.width = 5
		line.alpha = 0.2
		
		line = display.newLine(x1, y1, x2, y2)
		line:setColor(255, 255, 255)
		line.width = 2
		
	else
		local midx = (x2+x1)/2
		local midy = (y2+y1)/2
		midx = midx + (math.random(0, 1) - 0.5)*displace
		midy = midy + (math.random(0, 1) - 0.5)*displace
		drawLightning(x1, y1, midx, midy, displace/2)
		drawLightning(x2, y2, midx, midy, displace/2)
	end
end

drawLightning(0, 0, 280, 400, 100)

Here's what it looks like:

Leave a Reply