Custom Touch Move Node

Discussion in 'BBNodes' started by Codifie, Feb 13, 2019.

  1. Codifie

    Codifie Avid Boxer

    Joined:
    Apr 17, 2018
    Messages:
    234
    Likes Received:
    100
    This is a Custom Touch Move node. You will find it in the Actor nodes.
    This node allows you to input a left and right constraint to your movement. Stopping character at the point you designate. This prevents the character from moving off screen.
    If you want to make the changes to the code manually you can do so by copying and pasting the below code or just copy and paste the code I have Labeled as "//New Code". Only 4 lines of code need to be added.
    Make sure to add and setup the new Attributes, BorderLeft and BorderRight.


    var _speed;
    var _enabled = false;
    var _prev;

    //----Standart
    function init(){
    this.enableTouch();
    _speed = this.attribute('Speed');
    _borderleft = this.attribute('BorderLeft'); //New Code
    _borderright = this.attribute('BorderRight'); //New Code
    }

    function update( dt ){
    }
    function signal( name, value){
    _enabled = value;
    }
    //----Move
    function move( point ){
    if(_enabled){
    let ep = this.entity().position();
    let wp = this.scene().screenToWorld(point.x, point.y);

    wp.x = Math.min(wp.x, _borderright); //New Code
    wp.x = Math.max(wp.x, _borderleft); //New Code

    // log('X World Position = ' + wp.x);
    let delta = new Vec3(wp.x-ep.x, wp.y-ep.y, wp.z-ep.z);
    this.entity().setPosition(ep.x+delta.x*_speed.x,
    ep.y+delta.y*_speed.y,
    ep.z+delta.z*_speed.z);

    }
    }
    component.move = move;

    //----Touches
    function touchBegan( point ){
    this.move( point );
    _prev = point;
    }
    component.touchBegan = touchBegan;
    function touchMove( point){
    this.move( point );
    _prev = point;
    }
    component.touchMove = touchMove;
    function touchEnded(){
    }
    component.touchEnded = touchEnded;



    Link to file
    https://www.dropbox.com/s/vrbfm1o9r6upvoj/CustomeTouchMoveNode.bbasset?dl=0
     
    Last edited: Feb 13, 2019
  2. Rickwhy

    Rickwhy Boxer

    Joined:
    Feb 10, 2019
    Messages:
    1
    Likes Received:
    0
    Thank you very much!
     
  3. imonedesign

    imonedesign Boxer

    Joined:
    Oct 25, 2015
    Messages:
    29
    Likes Received:
    23
    Thank you! that's awesome.
     

Share This Page