Advice and tip for assignment new parent Sprites?

Discussion in 'Learning Javascript' started by Jonforum, Jul 19, 2017.

  1. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,583
    Likes Received:
    1,361
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    hi
    What are your advices or tricks for new parent assignment to a Sprites and the opportunity to return to the original parent.
    example:
    to change parent i do this
    PHP:
    Game_Picture.prototype.spine = function(id){
            var 
    newParent SceneManager._scene._spriteset._pictureContainer.children[id-1];
            var 
    targetSpriteSceneManager._scene._spriteset._pictureContainer.children[this._PID-1];
            
    targetSprite.parent=newParent;
            return 
    this
        
    };
    For the moment I do as like this for keep the original parent sprites.
    But I am not sure of the validity of this procedure.
    PHP:
    Game_Picture.prototype.spine = function(id){
            var 
    newParent SceneManager._scene._spriteset._pictureContainer.children[id-1];
            var 
    targetSpriteSceneManager._scene._spriteset._pictureContainer.children[this._PID-1];
            
    targetSprite.originalParent targetSprite.parent// store Original parent in originalParent , for comeBack if need !
            
    targetSprite.parent=newParent;
            return 
    this
        
    };
    so when delete picture
    PHP:
    Game_Screen.prototype.erasePicture = function(pId) {
        
    this._pictures[pId] = undefined;

        var 
    pictureChild SceneManager._scene._spriteset._pictureContainer.children[pId-1];
        if(
    pictureChild.originalParent){// reset to original parent // TODO: check other way if possible
             
    pictureChild.parent pictureChild.originalParent;
        }
    };
    Is there a way to go back (set Back) to the original parent, without making a total copy, for performance reasons?
    Or can you confirm that this is the right ways to do?
    thanks in advance for your suggestions
     
    Last edited: Jul 19, 2017
    #1
  2. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Why not just store the id of the parent instead of the whole object? That way should be easy to go back to the original parent without having to store everything about it. :)
    Also why are you returning this in the spine function? Basically you are returning the entire Game_Picture object, shouldn't it just be the targetSprite?
     
    #2
  3. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,583
    Likes Received:
    1,361
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    The parent assign by default are not all different?
    What class do they come from?
    I do not use id because original parent they seem to be all different.
    Or I'm wrong.
    this is example original parent from
    SceneManager._scene._spriteset._pictureContainer.children[141]
    And all picture seem to have diferent parent !?
    Where i can find the containe of those original parent ?
    44tt4t.jpg
    This is the only technique I have found for chaining
    awf3gt.jpg
    Do you have another suggestion?
    Ideally I allowed myself to add additional method chain, for some picture that will need it
    PHP:
    $gameScreen.showPicture('pid''fn', [origin], 'x''y').spine(id).setAngle(aN).setRot(sP).bindToMap(true).addProp({i:i})
    is the good way ?
    thank for your help

    this is the entir proto i add.
    PHP:
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    Game_Picture.prototype.spine = function(spineID){
            
    this._parentSpinePID spineID// parent Reference info
            
    var parentSpine SceneManager._scene._spriteset._pictureContainer.children[spineID-1];
            var 
    childSpine SceneManager._scene._spriteset._pictureContainer.children[this._PID-1];
            
    childSpine.originalParent childSpine.parent;
            
    childSpine.parent=parentSpine;
            return 
    this
        
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    Game_Picture.prototype.setAngle = function(a){
            
    this._angle a; return this
        
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    Game_Picture.prototype.setRot = function(r){
        
    this._rotationSpeed r; return this
        
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    Game_Picture.prototype.bindToMap = function(){
        
    // voir si on peut ajouter le child a spritmap plutot TODO:
        
    this._BindToMap=true; return this
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    Game_Picture.prototype.addProp = function(prop){
        
    this._prop prop; return this
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
    // ERASE PICTURE AND PIXI PICTURE BINDED TO IM
    Game_Screen.prototype.erasePicture = function(pId,trigger) { // trigger true , also deletter trigger
    this._pictures[pId] = undefined;
    if(
    trigger){delete this._lePictureTriggers[pId]}; // TODO: check if bug
    var pictureChild SceneManager._scene._spriteset._pictureContainer.children[pId-1];
    if(
    pictureChild.children.length){ // removePixiText child
    $gamePixiBank.removePixi(pId);
    pictureChild.removeChildren();
    };
    // reset to original parent // TODO: voir les parent si bug pour reatribustion
    if(pictureChild.originalParent){
    pictureChild.parent pictureChild.originalParent;
    }
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
     
    Last edited: Jul 19, 2017
    #3
  4. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    I haven't worked much with sprites myself so I am not completely certain about the parent part but:
    The _pictureContainer is a Sprite object this is all of your pictures top level parent. The next parent in the list will be either Spriteset_Map or Spriteset_Battle (both are in turn children of Spriteset_Base which is a child of Sprite) in order to check which one you are currently using you could check to see if the party is in battle or by using the instanceof method.

    The Game_Screen.prototype.showPicture function creates a new Game_Picture object meaning you should be able to access all Game_Picture functions from the $gameScreen._pictures array. Instead of chaining your functions, I suggest putting the arguments for those other functions inside your spine function then have your spine function call the appropriate functions. Then all you would have to do is something like:
    Code:
    $gameScreen.showPicture('pid', 'fn', [origin], 'x', 'y');
    $gameScreen._pictures[pid].spine(id, aN, sP, true, {i:i});
    Basically by doing this you won't have to retype every function name every time you want to reuse your code.
     
    #4
  5. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,583
    Likes Received:
    1,361
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    I was using something similar before.
    The problem is that duplicating code lines, and it quickly becomes hard to read.
    I have to do some testing with performance.now();
    To check if we have a very big difference of performance between return picture; and direct afectacion.

    But logically only the sprite is returned.
    because I return picture; at the time of creation.
    PHP:
    //□▼↓▼□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↓↓↓
        //QUAND AFFICHE UNE PICTURE AJOUTE AUSSI UN CONTENEUR EASING [DANS LE BON PID ARRAY]
        
    Game_Screen.prototype.showPicture = function(pictureIdnameoriginxyscaleXscaleYopacityblendMode) {
            var 
    picture = new Game_Picture();
            
    picture.show(pictureIdnameoriginxyscaleXscaleYopacityblendMode);
            
    this._pictures[pictureId] = picture;
            
    this.iniEasingIAVRA(this._pictures[pictureId]); // create IAVRA to _picturesEasing[]
            
    return picture// or this._pictures[pictureId]: TODO:need check performance.now();
        
    };
    //□▲↑▲□═══════════════════════════════════════════════════□□═══════════════════════════════════════════════════□↑↑↑
    And if I only want to activate the Bind, for example, without going through the other properties, the code becomes dificil to read.
    PHP:
    $gameScreen._pictures[pid].spine(''''''true); // hard to read. // hard read
    /////
    $gameScreen.showPicture(pidfn, [], x,y).bindToMap();
    In fact after verification, it would seem that it works.
    Instead of creating a copy of the parent to assign it when delete.
    Simply add line at the time of suppressing the image.

    I think this is the best way.
    Or if i see other better suggestions.
    PHP:
        // FAST WAY TO REASIGN PARENT ?
        
    if(this._parentSpinePID){
            
    pictureChild.parent SceneManager._scene._spriteset._pictureContainer;
        }
     
    #5
  6. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Alternatively instead of having more arguments, you could use a single object as the argument in the spine function. So something like this for instance:
    Code:
    //an example of using an object for arguments
    Game_Picture.prototype.spine = function (object) {
        object.id ? console.log('id', object.id) : false;
        object.aN ? console.log('aN', object.aN) : false;
        object.sP ? console.log('sp', object.sP) : false;
        object.bind ? console.log('bind', object.bind) : false;
    };
    Then you can have a call like so without the other possible arguments:
    Code:
    $gameScreen._pictures[id].spine({bind: true}); // logs bind
    $gameScreen._pictures[id].spine({id: 4, sP: 15}) // logs id and sP
    Anyway I hope these suggestions help. :)
     
    #6

Share This Page