1

Currently, each level has 3 layers :

  • background
  • collisions(walls, invisible walls, ground, etc)
  • foreground

My character sprite has 1 pixel to detect the collision. In this character class, there is a Color[] contains every pixels of the collision layer.

I calculate the position of the Character's pixel detection, and obtain an int that i can use with the Color[] ("CharactersPixel")

if ( Color[CharactersPixel].A != 0 )
    Then collision.

It works perfectly.

But the collision layer has to be used by every others objects like particles, monsters etc.

The collision layer has about 3.800.000 pixels. so the Color[] has the same.

If I add this Color[] to every objects, this will use too much RAM, right ?

4

1 回答 1

2

您只需向对象添加对碰撞层的引用。您不会将整个内容复制到每个对象。添加引用不会为您的对象增加太多内存。

但是,我建议通过在任何对象可访问的地方公开一些关卡数据,以更一般的方式使碰撞可用。也许您可以将数据作为可访问属性添加到您的类Game中。CurrentLevelCollisionData

通常,您将碰撞数据与实际渲染颜色分开,并使用框等执行更简单的碰撞,而不是近似匹配碰撞数据的形状。这最终将使计算诸如影响环境的角度和计算适当的物理场之类的事情变得更容易。像素到像素的冲突很少见,从性能角度来看也可能存在问题。

于 2012-08-29T17:36:23.130 回答