Jump to content

Bug in my ordering code?


Recommended Posts

I'm trying to implement a simple "move to back" / "move to front" without relying on the heavy lifting of something like https://github.com/pixijs/pixi-display

Here's my ordering code, but it doesn't seem to always work... though it does work some of the time... I think it looks like for some of the items it works but in reverse? What am I missing? :)

//moves an item in an array to either the front or back
export const PopTo = (isFront:boolean) => items => item => {
    const copy = items.concat();
    const ret = copy.splice(copy.indexOf(item), 1);

    return (isFront) ? ret.concat(copy) : copy.concat(ret);
}
export const PopToFront = PopTo(true);
export const PopToBack = PopTo(false);


//move item to back
this.children = PopToBack (this.children) (target)
//move item to front
this.children = PopToFront (this.children) (target)

 

Link to post
Share on other sites

I'm still having some weird stuff happening where the same code run over two different objects is having the opposite effect.

In other words, for the more recent object (starting at top layer) things work correctly. For the first object (starting at bottom layer) the action is refersed ("send to back == send to front and vice-versa")

It's almost like the ordering is affected by setting children(), but it's somehow affected by the original placement too? Not sure... will revisit on Monday probably. Have a good weekend!

Link to post
Share on other sites

Yeah that's basically the idea, trying to get more into functional stuff and keep things pure... in this case we're mutating the "this.children" anyway so there's no point really, but still - trying to force myself into new habits :D

Not sure why the action isn't working for me yet, maybe I'll add a fiddle here tomm...

Link to post
Share on other sites

The idea of having immutable data for games always seemed crazy to me. In games things are moving around a lot and changing their data all the time. So if you have to throw away objects and arrays each time this happens you are going to generate masses of garbage for the GC and have hopelessly bad performance.

Link to post
Share on other sites

It's a debatable topic, that's for sure.

On the one hand - you're of course right, GC sucks for games and creating a bunch of objects is a no-no.

On the other hand - code that's easy to reason about is a huge win for lots of reasons.

I'm working on something which I'll hopefully share in the next week or two, it's really just pong but using some functional programming and object-copying stuff. The neat thing is I'm pushing all of the game logic off to a web-worker. Theoretically, browsers are allowed to compartmentalize GC per thread, and those that do (if they do) would get the best of both worlds from that (imho).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...