Skip to main content
 首页 » 编程设计

python之在 cocos2d 中显示 Sprite 的问题

2025年05月04日11mq0036

这是我的cocos代码:

class Startbox(Layer): 
 
def __init__(self): 
    Layer.__init__(self) 
 
    self.batch = BatchNode() 
 
    self.add(self.batch) 
     
    img = pyglet.image.load('images/map_sprites.png') 
     
    tileset = pyglet.image.ImageGrid(img, 3, 15, 96, 96) 
 
    x, y = 0, 0 
     
    for i in range(10): 
     
        for j in range(10): 
             
            spr = Sprite(tileset[1]) 
            spr.x = x 
            spr.y = y 
             
            self.batch.add(spr) 
             
            x += 96 
        y += 96 
        x = 0 

我正在尝试获取 Sprite 并并排显示以覆盖窗口。 该代码会产生错误结果,图 block 之间有一个空格,如下所示:

我不明白为什么会这样,也不知道如何解决。下面的 pyglet 代码基本上做同样的事情,但是 Sprite 正确排列并且没有创建任何黑线:

class screen(pyglet.window.Window): 
 
def __init__(self, w, h): 
    super(screen, self).__init__(w, h) 
     
    sprite_sheet = grid(pyglet.image.load("images/map_sprites.png"), 3, 15) 
     
    self.batch = pyglet.graphics.Batch() 
     
    self.all_sprites = [] 
 
    x, y = 0, 0 
     
    for i in range(10): 
 
        for j in range(10):                 
             
            sprite = pyglet.sprite.Sprite(sprite_sheet[1], batch=self.batch) 
            sprite.x = x 
            sprite.y = y 
 
            self.all_sprites.append(sprite) 
             
            x += 96 
        y += 96 
        x = 0 


编辑:我找到了解决方案,为了清楚起见,我想将其作为答案发布,以防将来有人偶然发现这个问题,但我想编辑我的 OP 就足够了。

来自 Claudio Canepa 在 Cocos2d Google groups列表:

You can try passing do_not_scale=True in the director.init call , this will use ortographic projection which is better suited for tiles.

You can look at examples for cocos tilemaps in the scripts
test_tiles.py
test_tmx.py
test_platformer.py

实现非常简单:

if __name__ == '__main__': 
    director.init(width, height, do_not_scale=True) 
    director.run(Scene(Startbox())) 

请您参考如下方法:

在 python 版本的 cocos2d 中不起作用,但在 iPhone 版本的 cocos2d 中观察到同样的情况。所以这可能会有所帮助:-

« 如果 SpriteSheet 使用 Zwoptex 创建,则将 Sprite 间距添加到 2px +。

« 编辑 ccConfig.h 文件并定义它

#ifndef CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL 
#define CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL 1 
#endif 

引用我的回答:Thread in Stackoverflow