The code below is the class I use for displaying a rod - basically a coloured rectangle - in my game. The class definition is stored in a lua file called applib, which is used by all my other lua files.
However, although the code below works, i.e. it displays the rod etc, it doesn’t display the rod in the position that I specify. Rather, it seems that the rod is displayed further to the left and higher up the screen.
-- Rod
function rod(pGroup, pColour, pRequiredColour, pQuantity)
-- Properties
local mColour = pColour
local mRequiredColour = pRequiredColour
local mQuantity = pQuantity
local mDisplay = nil
mDisplay = display.newRect(10, 10, 10, 10)
mDisplay.isVisible = false
pGroup:insert(mDisplay)
-- Properties
local function colour(newValue)
-- If not nil then update class value
if (newValue ~= nil) then
mColour = newValue
end
-- Return value
return mColour
end
local function requiredColour(newValue)
-- If not nil then update class value
if (newValue ~= nil) then
mRequiredColour = newValue
end
-- Return value
return mRequiredColour
end
local function quantity(newValue)
-- If not nil then update class value
if (newValue ~= nil) then
mQuantity = newValue
end
-- Return value
return mQuantity
end
local function display(newValue)
-- If not nil then update class value
if (newValue ~= nil) then
mDisplay = newValue
end
-- Return value
return mDisplay
end
-- Update Stroke properties
local function updateStroke(strokeWidth, strokeColour)
mDisplay.setStrokeWidth = strokeWidth
mDisplay:setStrokeColor(strokeColour[1], strokeColour[2], strokeColour[3])
end
-- Update the Display object
local function updateDisplay(x, y, width, height, fillColour)
mDisplay.x = x
mDisplay.y = y
mDisplay.width = width
mDisplay.height = height
mDisplay:setFillColor(fillColour[1], fillColour[2], fillColour[3])
if (mDisplay.isVisible == false) then
mDisplay.isVisible = true
end
end
-- Reset Rod
local function reset()
mColour = 7 -- Default value on reset
mQuantity = 0 -- Quantity is zero on reset
mDisplay.isVisible = false
end
return {
updateDisplay = updateDisplay,
updateStroke = updateStroke,
reset = reset,
colour = colour,
requiredColour = requiredColour,
quantity = quantity,
display = display
}
end
In my maingame.lua file, I use the rod class like this:
local mLevel = applib.level(1) -- Just load level 1 to test
local mRods = {}
-- Add 3 Rods to the collection
local rod1 = applib.rod(localGroup, 7, mLevel.bucket1Colour(), 0)
mRods[1] = rod1
local rod2 = applib.rod(localGroup, 7, mLevel.bucket2Colour(), 0)
mRods[2] = rod2
local rod3 = applib.rod(localGroup, 7, mLevel.bucket3Colour(), 0)
mRods[3] = rod3
This creates 3 rods and assigns them to my mRods collection. “localGroup” is the main display group as used by Director. When I need to display the rods, I use code such as:
local rod = mRods[1]
rod.updateDisplay(10, 10, 75, 200, {255, 0, 0} )
And that would display a red rod for me. However, the problem is that the rod isn’t drawn to the coordinates specified. I have tested this by using the following code:
local mooch = display.newRect(10, 10, 75, 200)
You’d expect that code to overwrite the rod’s display as it should be drawn on top. However, as mentioned, the rod rectangle is actually drawn further up the screen and to the left, than the rectangle above.
I have tested the values being passed to the rod class and they are correct, i.e. it’s receiving the x, y as expected, but still not drawing them at that position.
Any ideas why this is happening?
Thanks. [import]uid: 26769 topic_id: 11352 reply_id: 311352[/import]
[import]uid: 3826 topic_id: 11352 reply_id: 41196[/import]