#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    387
    Rep Power
    21

    Starting from current Y position


    hi all, i have a text area that can be swiped using a touchevent....

    var myText:TextField = new TextField();
    myText.width = 300;
    myText.x = 0;
    myText.y = 50;
    myText.autoSize = TextFieldAutoSize.LEFT;
    myText.border = true;
    myText.textColor = 0xFFFFFF;
    myText.selectable = false;
    myText.multiline = true;
    myText.wordWrap = true;
    addChild(myText);
    myText.htmlText = event.target.data.rep;


    stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);

    function onTouchMove(event:TouchEvent) {
    myText.y = event.stageY;
    }


    this works fine. BUT when I start a swipe again to scroll through the rest of the text, it resets itself rather than holding the current position. Do i need to add a TOUCH_BEGIN and somehow swipe from the current position?
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    Is it any better if you do this:

    myText.y += event.stageY;
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    387
    Rep Power
    21
    hi i tried that but the text field jumps off the stage because i think the += is built up everytime a movement is made. it no longer becomes a smooth scroll
  6. #4
  7. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    Oh, I see what you mean. It sounds like what you need to do is use just the difference value so something like:

    myText.y += (event.stageY - myText.y);
    Quis custodiet ipsos custodes?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    387
    Rep Power
    21
    it slides correctly but it is still relative to the position on the stage, so myText always resets itself to calculate its position from whereabouts on the stage has been pressed and not its current position!

    do i need to incorporate localY? adding the eventListener to myText doesnt work either. I can see the problem, just dont know how to solve it!

    ..edit or maybe somehow use a TOUCH_BEGIN to calculate where MyText currently is and then use that value/
    Last edited by nshack31; September 22nd, 2011 at 10:43 AM.
  10. #6
  11. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    Phew! What a headache touch events are! You motivated me to try them out which led me to realise what a pile of junk Device Central is. So I started testing it out on my Android phone. Here is what works for me:
    Code:
    stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin); 
    stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
    
    var start_y:Number;
    
    function onTouchBegin(event:TouchEvent)
       { 
          start_y = event.stageY;
       }
    
    function onTouchMove(event:TouchEvent)
       { 
          myText.y -= Math.round(start_y - event.stageY);
          start_y = event.stageY;
       }
    I did read that if you are not using multi touch then you should really just be using the normal mouse events as they use less CPU power which could mean it may be more responsive on lower end devices.

    Also, I used Math.round to ensure that the TextField is always moved by whole integer values. This may look jumpier than if you removed the rounding but it should make the text clearer. So a trade-off.

    Finally, if you are scrolling large amounts of text or images then you should checkout the scrollRect property. It will give you a much faster speed boost when scrolling although it takes a while to get your head around. The short version is that you draw a rectangle above your content and then scroll the rectangle instead of the content. The trick is to realise that the axis become reversed. So if you scroll the rectangle down then the content would appear to scroll up and vica versa.
    Quis custodiet ipsos custodes?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    387
    Rep Power
    21
    great advice thankyou! they sure are a headache, i will checkout scrollRect shortly. I gave up in the end and used mouse events like you say...


    stage.addEventListener(MouseEvent.MOUSE_DOWN, checkDirection); stage.addEventListener(MouseEvent.MOUSE_UP, checkstop);

    function checkDirection(event:MouseEvent) {
    //add drag function to sprite, minus original stage size to prevent all text clearing
    mySprite.startDrag(false,new Rectangle(0,0,0,-myText.height+stagesize));


    }


    function checkstop(event:MouseEvent) {
    mySprite.stopDrag();


    }

    ps. to use scrollRect would i simply replace startDrag with scrollRect ?
    Last edited by nshack31; September 23rd, 2011 at 09:18 AM.
  14. #8
  15. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3165
    Hi, no I'm afraid it's not that simple. The scrollRect is one of those things that is more work but in most cases proves worth it. You can read the offical example for it here on the LiveDocs page.

    There's a short explanation of it here with a few basic pictures to help you visualise the rectangle stuff I mentioned earlier.

    Comments on this post

    • delboy31 agrees : thanks
    Quis custodiet ipsos custodes?

IMN logo majestic logo threadwatch logo seochat tools logo