[英]Cocos2d/Python: viewable portion of layer shifts in opposite direction of focus
新手在這里,嘗試使用Cocos2d 0.6.0在Python中制作游戲:)
我正在使用ScrollingManager的set_focus(x, y)
方法來使用鼠標在2D瓦片地圖上滾動。 涉及的各層似乎可以正確滾動,但是似乎存在一個裁剪層,其大小與沿相反方向移動的窗口(512x512)相同,部分遮擋了我的層。 我該如何消除?
我可以這樣最好地形象化:
作為參考,我將Cocos2d 0.6.0 ScrollingManager類用於Python。
[編輯]我創建了一個可測試的獨立腳本。
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()
感謝您的回答!
通過更改每個單獨層的視圖來解決此問題,如下所示:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.