Game Development Reference
In-Depth Information
function getMonthName(theMonth)
local months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"}
return months[theMonth]
end
Accessing Strings Like an Array
In some other programming languages, string can be accessed using square brackets. However, in
Lua, we use the string.sub function. The flexibility of Lua allows for a few alterations and provides
us this functionality such as
getmetatable('').__index = function(str,i)
return string.sub(str,i,i)
end
After we have declared this function, we can test it out quickly, as follows:
theString = "abcdef"
print(theString[4])
Finding the Distance Between Two Points in One or Two Dimensions
This is almost a no-brainer. When dealing with two points on the same axis, we can use simple
subtraction to find the distance between them, like so:
function getDistance(pos1, pos2)
return pos1 - pos2
end
However, if we're trying to find the distance between two points, we have to resort to trigonometry.
Here's an example:
function getDistanceBetween(ptA, ptB)
return math.sqrt((ptA.x - ptB.x)^2 + (ptA.y - ptB.y)^2)
end
Determining the Angle Between Two Points
If you're creating a tower defense-style game, for example, you might need to turn a player, gun, or
other object to point in the direction of an approaching enemy or rotate a the object to a point where
the screen has been touched. To do this, you need to determine the angle between two points: the
center point where the turret or player is, and the point of touch or where the enemy currently is. The
angle between two points can be determined again using trigonometry, as follows:
 
Search WWH ::




Custom Search