Game Development Reference
In-Depth Information
To draw the line, we call the function as follows:
local line = newline(10,10,250,50)
This creates a line from 10, 10 to 250, 50 with a width of 2 pixels. We can set the line width and the
color with the setLineStyle function. We could extend that function to also include a width and color,
which could default to the standard values of 1 for the width and 0x000000 for the color.
function newLine(x1, y1, x2, y2, lineWidth, lineColour)
local lineWidth = lineWidth or 1
local lineColour = lineColour or 0x000000
local obj = Shape.new()
obj:setLineStyle(lineWidth, lineColour)
obj:beginPath()
obj:moveTo(x1, y1)
obj:lineTo(x2, y2)
obj:endPath()
stage:addChild(obj)
return obj
end
Now we can draw a line in pretty much the same way as earlier, but also choose our own line width
and color, like so:
local line = newLine(10, 10, 250, 50, 4, 0x00FFFF)
Rectangles
A rectangle is a slightly modified form of a line. It is made up of four lines, and also includes a fill
color. If we don't pass a parameter for the fill color, the rectangle will have an outline; otherwise, it
will be filled in with the color passed. Figure 9-13 shows an example of the each.
function newRectangle(x1, y1, x2, y2, lw, lc, fc)
local obj = Shape.new()
local lw = lw or 1
local lc = lc or 0x000000
obj:beginPath()
obj:setLineStyle(lw, lc)
if fc then
obj:setFillStyle(Shape.SOLID, fc)
end
obj:moveTo(x1,y1)
obj:lineTo(x2, y1)
obj:lineTo(x2, y2)
obj:lineTo(x1, y2)
obj:lineTo(x1, y1)
obj:closePath()
obj:endPath()
 
Search WWH ::




Custom Search