Skip to main content
 首页 » 编程设计

python之如何使用 cocos python 查找图层宽度/高度

2025年02月15日16Renyi-Fan

我目前正在摆弄 python 和 cocos2D

我想做一个像这样的缩进空间的简单显示

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() ) 

我想要做的是在第一个标签下方显示第二个标签,并将第一个标签的三分之一向右移动。

有谁知道我如何获得第一个标签的宽度?

编辑:这是开发人员关于为什么没有明确访问权限的答案的问题。

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

请您参考如下方法:

最合适的似乎是

self.basic_text.element.content_width 

从图书馆的快速浏览来看,这似乎被遗忘了公开。也在 their API根本没有记录此参数。