Search code examples
pythoncocos2d-python

How to find layer width/height with cocos python


I'm currently fiddling around with python and cocos2D

and i wanted to make a simple display with indented space much like this

import cocos
from cocos.director import director

class MyLayer(cocos.layer.Layer):

    def __init__(self):
        #Setting the coordinate for the base layer
        self.x = 200
        self.y = 100
        #A first layer of text
        x_basic_text = 0
        y_basic_text = 0
        self.basic_text = cocos.text.Label("Unindented text", x=x_basic_text, y=y_basic_text )
        #A second layer of text
        y_indent_text = -20 #So that it doesn't squeeze with the basic text layer
        x_indent_text = self.basic_text.width/3 #which doesn't work since layers have no width attribute
        self.indent_text = cocos.text.Label("Indented text", x=x_indent_text, y=y_indent_text )

director.init(resizable=True)
director.run( MyLayer() )

What i want to do is display the second label below the first, and moved one third of the first label to the right.

Does anyone know how i could get the width of the first label to do so ?

Edit : Here's the issue with the answer from the devs as to why there's no clear access.

https://github.com/los-cocos/cocos/issues/282


Solution

  • The best fit seems to be

    self.basic_text.element.content_width
    

    From a quick view at the library it seems that this was forgotten to be exposed. Also in their API this parameter is not documented at all.