Best method to search a children id? very fast ?[SOLVED]

Discussion in 'Learning Javascript' started by Jonforum, Jun 30, 2017.

  1. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    Hi
    What would be the most effective way, for find a child or (sprite) in the _pictureContainer ?
    PHP:
    SceneManager._scene._spriteset._pictureContainer.children
    The problem is that I have a lot of picture and their CHILD id are messy.!

    I use the filter() method, but it is very heavy in resource, especially that can not BREAK; after the first result. !

    PHP:
    var pictureChild SceneManager._scene._spriteset._pictureContainer.children.filter(function(pic) { return pic._pictureId === id; });
    pictureChild.children.removeChildren(); // remove all children inside this sprite picture
    Would there be? a method to quickly get the child's position ? or a way to stored Childposition in the Game Screen constructor with is INDEX ???

    I would have liked, use the find () method, but it seem only ECMAScript 6 :frown:
    What are your tips and advice?

    and do existe something in a class , to do example
    .getChildAt() ?

    thank a lot for help
     
    Last edited: Jun 30, 2017
    #1
  2. Quxios

    Quxios Veteran Veteran

    Messages:
    1,055
    Likes Received:
    779
    First Language:
    English
    Primarily Uses:
    RMMV
    Just loop through the children with a normal for loop and break once you have what you need.
    Code:
    var child;
    for (var i = 0; i < children.length; i++) {
      if (children[i].someId === IDIWANT) {
        child = children[i];
        break;
      }
    }
    And as for the es6 functions, you can find polyfills for most of them. You can find them in external libraries or just at the mdn site. example the polyfill for Array.find can be found at the bottom of:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
     
    #2
  3. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    Thank you, I did not know this term !.
    i was use https://www.w3schools.com/js/ to learn & study javascript.
    and not have the Polyfill !

    So if I understood correctly to stay clean, I can do a js file
    with all Polyfill i need
    example Polyfill.js
    and add im to index.html , before load my plugin.js

    PHP:
    <body style="background-color: black">
            <
    script type="text/javascript" src="js/libs/pixi.js"></script>
            <
    script type="text/javascript" src="js/libs/pixi-tilemap.js"></script>
            <
    script type="text/javascript" src="js/libs/pixi-picture.js"></script>
            <
    script type="text/javascript" src="js/libs/fpsmeter.js"></script>
            <
    script type="text/javascript" src="js/libs/lz-string.js"></script>
            <
    script type="text/javascript" src="js/libs/iphone-inline-video.browser.js"></script>
            <
    script type="text/javascript" src="js/rpg_core.js"></script>
            <
    script type="text/javascript" src="js/rpg_managers.js"></script>
            <
    script type="text/javascript" src="js/rpg_objects.js"></script>
            <
    script type="text/javascript" src="js/rpg_scenes.js"></script>
            <
    script type="text/javascript" src="js/rpg_sprites.js"></script>
            <
    script type="text/javascript" src="js/rpg_windows.js"></script>
    <
    script type="text/javascript" src="js/Polyfill.js"></script>
            <
    script type="text/javascript" src="js/plugins.js"></script>
            <
    script type="text/javascript" src="js/main.js"></script>
        </
    body>
     
    #3
  4. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Just a thought but if you know the id and since all ids for the children should be unique could you perhaps use Array.prototype.indexOf?
    Code:
    var child;
    var id = children.indexOf(IDIWANT);
    child = children[id];
     
    #4
  5. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    In fact when we are tired we makes us stupid and incompetent.
    All the pictures are well in order !!
    I confused the pictureID with the spriteID !, we must admit that if we read quickly. We can confused :dizzy:
    aeafdf.jpg
    So calling a filter is completely stupid.
    so i simply do this now and is work !

    PHP:
        var pictureChild SceneManager._scene._spriteset._pictureContainer.children[pictureId-1];
        if(
    pictureChild.children.length){pictureChild.removeChildren()}; // removePixiText child
     
    Last edited: Jul 1, 2017
    #5
    Sarlecc likes this.
  6. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    I know the feeling lol. There are normally a ton of ways to do something and 99% of them are overly complicated (figures out an easier way which in turn allows you to find an even simpler way). :p
     
    #6
    Jonforum likes this.

Share This Page