Display objects not displaying at x, y specified

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]

just a quick note, do you think that the left and higher up could be exactly half the width and half the height of your image?

if it is, then it is resolvable by using the referencePoint

?:slight_smile: [import]uid: 3826 topic_id: 11352 reply_id: 41196[/import]

Not sure what you mean by “…using the referencePoint”, but I shall look it up and try.

Thanks :slight_smile: [import]uid: 26769 topic_id: 11352 reply_id: 41199[/import]

Ok, so clearly I’m doing something wrong. Probably to do with the reference point settings, but I don’t understand them.

I just want my class’s display object to display to the same coordinate scheme as display objects created directly in my maingame.lua file.

Can somebody help me here? Please? I’ll be your best friend and everything. [import]uid: 26769 topic_id: 11352 reply_id: 41202[/import]

@lordmooch,
I thought you would have got some hint, well anyways…

[lua]local myImg = display.newImage(“myimage”,0,0)
myImg:setReferencePoint(display.TopLeftReferencePoint)
myImg.x=100
myImg.y=100[/lua]

if you do not have the second line, comment it out by ading – in front of the line and see what happens.

hope this solves your issue :slight_smile:

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11352 reply_id: 41203[/import]

Oh, I did try altering the reference point settings, but it didn’t fix my problem. No change whether I set the reference point or not.

Thanks for the help though :slight_smile: [import]uid: 26769 topic_id: 11352 reply_id: 41227[/import]

Ah, so I think I may have solved the issue. It was, as was suggested, to do with the Reference Point. However, as the details below explain, I couldn’t make it work.

For example, the use of setReferencePoint works in this example:

-- 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:setReferencePoint(display.TopLeftReferencePoint)  
 mDisplay.x = 30  
 mDisplay.y = 100  
 mDisplay.isVisible = false  
  
 pGroup:insert(mDisplay)  
  
 -- 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()  
 end  
 return {  
 updateDisplay = updateDisplay,  
 reset = reset  
 }  
  
end  

But in this example, setReferencePoint does not work. Notice that in this example it’s used inside a function that lives within the main rod function. The error given is “attempt to index upvalue ‘display’ (a function value)”.

-- 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.x = 30  
 mDisplay.y = 100  
 mDisplay.isVisible = false  
  
 pGroup:insert(mDisplay)  
  
 -- Update the Display object  
 local function updateDisplay(x, y, width, height, fillColour)  
  
 mDisplay:setReferencePoint(display.TopLeftReferencePoint)  
  
 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()  
 end  
 return {  
 updateDisplay = updateDisplay,  
 reset = reset  
 }  
  
end  

I figured out that it meant the “display” object (if that’s the right term?) could not be “seen” inside the updateDisplay function. So, to make it work I changed the code as below. Basically, setting a new local variable’s value to the “display.TopLeftReferencePoint” value, and then using the local variable to set the object’s reference point.

Is this a good/bad/acceptable way of doing this?

[code]
– Rod
function rod(pGroup, pColour, pRequiredColour, pQuantity)

– Properties
local mColour = pColour
local mRequiredColour = pRequiredColour
local mQuantity = pQuantity
local mDisplay = nil
local mRefPoint = display.TopLeftReferencePoint

mDisplay = display.newRect(10, 10, 10, 10)
mDisplay.x = 30
mDisplay.y = 100
mDisplay.isVisible = false

pGroup:insert(mDisplay)

– Update the Display object
local function updateDisplay(x, y, width, height, fillColour)

mDisplay:setReferencePoint(mRefPoint)

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()
end
return {
updateDisplay = updateDisplay,
reset = reset
}

end
[/code] [import]uid: 26769 topic_id: 11352 reply_id: 42704[/import]