Cocos2d / Python:图层的可视部分在焦点的相反方向上移动

时间:2014-06-05 22:01:48

标签: python python-2.7 scroll cocos2d-python

新手在这里尝试使用Cocos2d 0.6.0在Python中制作游戏:)

我使用ScrollingManager的set_focus(x, y)方法使用鼠标滚动2d图块地图。所涉及的图层似乎正确滚动,但似乎有一个裁剪图层,与窗口(512x512)相反的方向移动,部分遮挡我的图层。我怎么能让它消失?

我最好用这种方式来形象化:

Screenshot 1: initial focus at startup, all layers visible

Screenshot 2: layers seem cropped after scrolling down

作为参考,我使用Python的Cocos2d 0.6.0 ScrollingManager类。

[编辑]我已经创建了一个应该可以测试的独立脚本。

import pyglet
from pyglet.window import key, mouse
import cocos
from cocos import tiles, actions, layer
import random

def main():
  global mouse, scroller
  global fx, fy, winx, winy, offsetx, offsety

  # map size (cells)
  hsize = 128
  vsize = 128

  # window size (pixels)
  winx = 960
  winy = 640

  # coordinates of screen focus
  fx = 0
  fy = 0

  from cocos.director import director
  director.init(width=winx, height=winy, do_not_scale=True, resizable=True)

  offsetx = hsize * 16 - winx
  offsety = vsize * 16 - winy

  ##################################
  ### creation of randomized map ###
  ##################################

  tileset = {}
  # a 16x16 pixel tile (blue, represents water)
  tileset['water']  = pyglet.image.ImageGrid(pyglet.image.load('resources/water.png'), 4, 4)
  # a 16x16 pixel tile (green, represents land)
  tileset['land']   = pyglet.image.ImageGrid(pyglet.image.load('resources/land.png'), 4, 4)

  # empty 2D array holding RectCells
  cellMatrix = [ [None]*hsize for i in range(vsize) ]

  # filling of array
  for v in range(vsize):
    for h in range(hsize):
      if random.random() < 0.5:
        image = tileset['water'][1]
      else:
        image = tileset['land'][1]

      tile = cocos.tiles.Tile('test', {}, image, None)

      cellMatrix[v][h] = cocos.tiles.RectCell(h*16, v*16, 1, 1, None, tile)

  ##################################
  ### end of creating random map ###
  ##################################

  # creating rectMapLayer from cell array
  rectMapLayer = cocos.tiles.RectMapLayer('test', 16, 16, cellMatrix, None, {})

  scroller = cocos.layer.ScrollingManager()
  scroller.add(rectMapLayer, 0, 'test')
  main_scene = cocos.scene.Scene(scroller)

  def on_mouse_motion(x, y, dx, dy):

    global fx, fy, offsetx, offsety, winx, winy

    if x > winx - 16:
      fx = min(offsetx, fx + 16)

    if x < 16:
      fx = max(0, fx - 16)

    if y > winy - 16:
      fy = min(offsety, fy + 16)

    if y < 16:
      fy = max(0, fy - 16)

    scroller.set_focus(fx + winx/2, fy + winy/2, force=True)

  director.window.push_handlers(on_mouse_motion)

  director.run(main_scene)

# run game
if __name__ == '__main__':
  main()

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

通过更改每个单独图层上的视图来解决问题:

rectMapLayer = cocos.tiles.RectMapLayer('test', 16, 16, cellMatrix, None, {})

scroller = cocos.layer.ScrollingManager()

scroller.add(rectMapLayer, 0, 'test_layer')

# focus coordinates
x = 50
y = 50

scroller.get('test_layer').set_view(0, 0, 1024, 1024, -x, -y)

这将导致测试图层从原点(0,0)设置其视图,宽度/高度为1024x1024,偏移量为-x,-y。