Positioning group/container of Entities


(Scott Thiessen) #1

New to FlashPunk, and I’m trying to get my head around the best way to position Entities in cases where I’d normally be nesting Sprites within containers.

In this case, I’m working on a Tetris game, and I’d like the game to be offset from the left and top edges some number of pixels. The game is already working, and so I naturally don’t want to update every positioning line of code for every entity and add offsetX and offsetY values.

What I want to do is just put the game as a whole inside a container and then position the container. I can’t seem to find anything in the documentation about grouping entities, or entity containers, or setting x,y values for a World, though.

Any thoughts? Thanks!


(Jacob Albano) #2

One thing you could do is just put the entire Flashpunk engine instance into a Sprite and position it that way, instead of having the game class be your document class and adding it to the stage directly. I don’t know if that approach would be flexible enough for you though.


(Scott Thiessen) #3

Well, I’d potentially like to be able to do FlashPunk stuff behind/outside the game as well. Just want to offset the positioning of the main portion of the game.

If I am understanding right, x and y of entities values are always global x and y values? There is no nesting?


(Scott Thiessen) #4

I’m noticing FP.anchorTo(), and wondering if that will help, but I’m not totally getting how that works. Documentation is pretty vague on that one.


(Sparrow) #5

Why not use FP.camera?


(Scott Thiessen) #6

That’s a global setting, so, again, it feels pretty hack-y.

I only want to affect the position the game area (i.e., the blocks that are dropping within a certain rectangular area). I’m going to have a background and some ui elements outside of the game area, and I’d rather not have to undo the camera offset for every entity not inside the game.


(Scott Thiessen) #7

It doesn’t seem like there’s any idea of nested entities or containers in FlashPunk, so (at least for now) I suppose I will add an offset to all the code lines that position the blocks.


(azrafe7) #8

Well, I can think of multiple solutions for your problem:

  • simply define your offset as a constant and use it in your blocks drawing methods (or pass the offset in their constructor)
  • you can draw all your sub-entities to a buffer and then blit the latter on FP.buffer (setting your blocks visible property to false etc…)
  • create an EntityContainer class exposing an addEntity(...) method etc., and make so that it updates and renders its children at the needed offset
  • simply define a base-class with the needed offset and make the blocks extend from it
  • use punk.fx FXLayer
  • use a normal Entity and add all the blocks to its Graphiclist, and then write ad-hoc logic to update each Graphic

(David Williams) #9

If you use FP.camera, you can set your GUI’s scrollX and scrollY properties to 0 to avoid the camera positioning them.


(der_r) #10

In your world class, you could cycle through all entities and set the x/y-offsets of their graphic instances. You can accomplish the same via adding a GameManager entity that looks after all your game-related entities.


(Scott Thiessen) #11

Thanks everyone for the responses.

What seemed like the cleanest solution in my case was to simply use an offset constant any time I was repositioning entities (e.g., x = Config.GAME_AREA.x + (_pos.x * cellSize); ), similar to what @azrafe7 was suggesting I think.

Seems like creating an EntityContainer would also makes a lot of sense, and I’m surprised something like that isn’t built in to FlashPunk. Coming from a website & app dev background, I’m used to nesting objects within objects many layers deep pretty frequently.

Anyway, much appreciated all.