Jump to content

How to move the sprite to the top?


moszis
 Share

Recommended Posts

Hi Guys,

 

   Is there a way to specify that sprite should go on top (similar to the way its done in phaser)?   I need to have multiple draggable sprites.   Currently the last one drawn is always on top.  So when you are dragging sprites, they will be dragged under the sprites that have been drawn later.

 

  I know it can be easily done by just clearing and redrawing the sprite every tick but it would make it much easier if there was an API to bring it to top layer on mousedown.

 

 

PS:  I know its two dimensional pixel grid(this seems to be common answer to these type of questions).. just talking about APIs here.

 

Link to comment
Share on other sites

An alternative form.. without prototyping

function bringToFront(sprite, parent) {var sprite = (typeof(sprite) != "undefined") ? sprite.target || sprite : this;var parent = parent || sprite.parent || {"children": false};if (parent.children) {    for (var keyIndex in sprite.parent.children) {         if (sprite.parent.children[keyIndex] === sprite) {            sprite.parent.children.splice(keyIndex, 1);            break;        }    }    parent.children.push(sprite);}}
function sendToBack(sprite, parent) {var sprite = (typeof(sprite) != "undefined") ? sprite.target || sprite : this;var parent = parent || sprite.parent || {"children": false};if (parent.children) {    for (var keyIndex in sprite.parent.children) {          if (sprite.parent.children[keyIndex] === sprite) {            sprite.parent.children.splice(keyIndex, 1);            break;        }    }    parent.children.splice(0,0,sprite);    }}

uses..

 

mySprite.mousedown = bringToFrontbringToFront(mySprite);bringToFront(mySprite, newDisplayObjectContainer); //pops it out of its original container/parent

and of course..

mySprite.bringToFront = bringToFront;mySprite.bringToFront();mySprite.bringToFront(null, newDisplayObjectContainer);

EDIT: opps... I was using pop incorrectly.. it doesn't take a parameter, so I am using a cross browser legacy compatible implementation. Reference: http://stackoverflow.com/questions/5767325/remove-specific-element-from-an-array/15762329#15762329 

Edited by mrBRC
Link to comment
Share on other sites

  • 1 year later...
  • 1 month later...
  • 3 years later...
  • 1 year later...

I've just stored the zIndex on each of the display objects and then when you want to move the display objects to the top you just find the highest zIndex, add 1 and set the zDepth on the display objects to that. Works well and pretty straight forward.  You need to put the display objects in a container and set that as sortable too.

Edited by Dougi
Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...