Learn Solar2d - 10 Code Examples & CST Typing Practice Test
Solar2D (formerly Corona SDK) is a free, open-source, cross-platform 2D game and app development framework using Lua scripting. It focuses on rapid development and performance for mobile, desktop, and HTML5 applications.
View all 10 Solar2d code examples →
Learn SOLAR2D with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Solar2D Lua Example - Simple Counter
-- main.lua
local count = 0
local counterText = display.newText("Count: 0", display.contentCenterX, 100, native.systemFont, 40)
local function updateCount(delta)
count = count + delta
counterText.text = "Count: " .. count
end
local incButton = display.newText("+", display.contentCenterX - 50, 200, native.systemFont, 50)
incButton:addEventListener("tap", function() updateCount(1) end)
local decButton = display.newText("-", display.contentCenterX + 50, 200, native.systemFont, 50)
decButton:addEventListener("tap", function() updateCount(-1) end)
A simple Solar2D Lua script to increment and decrement a counter displayed on the screen.
Solar2D Lua Example - Moving Object
-- main.lua
local rect = display.newRect(display.contentCenterX, display.contentCenterY, 100, 100)
rect:setFillColor(1, 0, 0)
local function moveRect()
rect.x = rect.x + 2
if rect.x > display.contentWidth then
rect.x = 0
end
end
Runtime:addEventListener("enterFrame", moveRect)
Moves a display object across the screen continuously.
Solar2D Lua Example - Touch Drag
-- main.lua
local rect = display.newRect(display.contentCenterX, display.contentCenterY, 100, 100)
rect:setFillColor(0, 0, 1)
local function onTouch(event)
if event.phase == "moved" then
rect.x = event.x
rect.y = event.y
end
return true
end
rect:addEventListener("touch", onTouch)
Allows a rectangle to be dragged around the screen using touch input.
Solar2D Lua Example - Simple Button
-- main.lua
local button = display.newRect(display.contentCenterX, display.contentCenterY, 150, 80)
button:setFillColor(0, 1, 0)
local function onTap()
button:setFillColor(math.random(), math.random(), math.random())
end
button:addEventListener("tap", onTap)
Creates a button that changes color when tapped.
Solar2D Lua Example - Fade In/Out
-- main.lua
local circle = display.newCircle(display.contentCenterX, display.contentCenterY, 50)
circle:setFillColor(1, 0, 1)
local function fade()
transition.to(circle, {time=1000, alpha=0, onComplete=function()
transition.to(circle, {time=1000, alpha=1})
end})
end
fade()
Fades a circle in and out continuously.
Solar2D Lua Example - Simple Physics
-- main.lua
local physics = require("physics")
physics.start()
local ball = display.newCircle(display.contentCenterX, 50, 30)
physics.addBody(ball, "dynamic", {radius=30, bounce=0.8})
local ground = display.newRect(display.contentCenterX, display.contentHeight - 50, display.contentWidth, 50)
physics.addBody(ground, "static")
Applies physics to a falling object.
Solar2D Lua Example - Sprite Animation
-- main.lua
local sheetOptions = { width=64, height=64, numFrames=4 }
local sheet = graphics.newImageSheet("sprite.png", sheetOptions)
local sequence = { name="walk", start=1, count=4, time=400, loopCount=0 }
local sprite = display.newSprite(sheet, sequence)
sprite.x, sprite.y = display.contentCenterX, display.contentCenterY
sprite:play()
Animates a sprite using an image sheet.
Solar2D Lua Example - Timer Event
-- main.lua
local function sayHello()
print("Hello Solar2D!")
end
timer.performWithDelay(2000, sayHello, 0)
Displays a message every 2 seconds using a timer.
Solar2D Lua Example - Simple Scene
-- main.lua
local bg = display.newRect(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)
bg:setFillColor(0.5, 0.7, 1)
local title = display.newText("Welcome!", display.contentCenterX, 100, native.systemFontBold, 50)
Creates a basic scene with background and text.
Solar2D Lua Example - Audio Play
-- main.lua
local sound = audio.loadSound("click.wav")
local rect = display.newRect(display.contentCenterX, display.contentCenterY, 150, 80)
rect:setFillColor(1, 0.5, 0)
local function playSound()
audio.play(sound)
end
rect:addEventListener("tap", playSound)
Plays a sound effect when a rectangle is tapped.
Frequently Asked Questions about Solar2d
What is Solar2d?
Solar2D (formerly Corona SDK) is a free, open-source, cross-platform 2D game and app development framework using Lua scripting. It focuses on rapid development and performance for mobile, desktop, and HTML5 applications.
What are the primary use cases for Solar2d?
2D mobile games. Educational apps and interactive media. Prototyping and game jams. Casual games for iOS/Android. HTML5 browser-based apps
What are the strengths of Solar2d?
Lightweight and fast for 2D. Free and open-source. Rapid prototyping and development. Strong mobile deployment support. Large plugin ecosystem for monetization and analytics
What are the limitations of Solar2d?
Primarily 2D; no 3D support. Smaller community than Unity or Godot. Limited advanced GUI customization. Performance can degrade with very complex scenes. Lua scripting required, not visual scripting
How can I practice Solar2d typing speed?
CodeSpeedTest offers 10+ real Solar2d code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.