Cocos2d场景仅占屏幕的四分之一,如何解决?

时间:2019-08-10 09:33:58

标签: cocos2d-python

我正在尝试创建一个非常基本的cocos2d程序,但是从左下角到窗口中间,场景只占据了1/4的窗口。

我尝试使用scene.position = x,y更改场景的位置,但cocos似乎将窗口的中间视作其右上角。

import cocos
from cocos.director import director

if __name__ == '__main__':
    director.init(
        fullscreen=False, width=1280, height=800)
    hello_layer = HelloCocos() #Grey ColorLayer and a "Hello" label on it.
    test_scene = cocos.scene.Scene(hello_layer)
    test_scene.position = 0, 600

    director.run(test_scene)

You can see here,场景没有越过窗口中间。 在全屏模式下没有问题,场景占据了整个屏幕。 如何使场景占据整个窗口?增大它也无济于事,它只是从底部和左侧边缘移出。

1 个答案:

答案 0 :(得分:1)

在cocos中,场景的锚点默认为(0.5,0.5)。这意味着场景的(0,0)位于其中心。

将场景定位在(0,600)时,就是说场景的中心在窗口(可见区域)的(0,600)处。

现在,我假设您的场景大小和窗口大小均为width = 1280,height = 800。这将使场景的一部分离开窗口。特别是在左上方。

Here is a reference image to explain the same

要使场景居中,您可能需要将场景放置在(640,400)的可见区域(窗口)的中心。或者将其锚点明确设置为(0,0),并将位置设置为(0,0)。