How to find move direction on image

155 views
Skip to first unread message

Omer Meshy

unread,
Jun 24, 2020, 1:08:59 AM6/24/20
to DroidScript
Hello
How can I get the direction of the move with img.setontouchmove?

ECEisLife

unread,
Jun 24, 2020, 2:04:33 AM6/24/20
to DroidScript
Hi Omer.

Maybe you can track the position of the touch. It should be included in the returned event. Store X and Y of the initial touchmove and check for the succeeding X and Y's of the event. Just remember that 0,0 is at the topleft corner of the image object. So when X increases therefore you probably going to the right horizontally. And when Y increases, the touch is going down vertically. And from that in mind you can even get the angle of displacement.

Alan Hendry

unread,
Jun 24, 2020, 4:04:13 AM6/24/20
to DroidScript
Omer,
The reference on the app shows that img.SetOnTouchMove passes an event
The event has
object: {source, action, count, x:[x1,x2,x3], y:[y1,y2,y3]}
the appobject, string Move, integer, six fractions 0 ... 1
not clear what x1/2/3 and y1/2/3 are
Regards, ah

Symbroson

unread,
Jun 24, 2020, 4:21:44 AM6/24/20
to DroidScript
I've added this popup to the next docs version - hope this makes things clear

Steve Garman

unread,
Jun 24, 2020, 4:57:02 AM6/24/20
to DroidScript
I would use OnTouch rather than OnTouchMove

In the code below you can use xMove and yMove to calculate the direction the finger has moved

function OnStart()
{
var lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
var img = app.CreateImage( null, 0.8, 0.8 );
img.SetColor( "red" );
img.SetOnTouch( img_OnTouch );
lay.AddChild( img );
app.AddLayout( lay);
}

function img_OnTouch( ev )
{
if(ev.action=="Down")
{
this.data.lastX = ev.X;
this.data.lastY = ev.Y;
}
else if(ev.action=="Move")
{
// negative xMove is left
var xMove = ev.X - this.data.lastX;
// negative yMove is up
var yMove = ev.Y - this.data.lastY;
this.data.lastX = ev.X;
this.data.lastY = ev.Y;
}
}

Steve Garman

unread,
Jun 24, 2020, 5:16:59 AM6/24/20
to androi...@googlegroups.com
And if you just want to know about movement from down to up

function img_OnTouch( ev )
{
if(ev.action=="Down")
{

this.data.firstX = ev.X;
this.data.firstY = ev.Y;


}
else if(ev.action=="Move")
{
// negative xMove is left
var xMove = ev.X - this.data.lastX;
// negative yMove is up
var yMove = ev.Y - this.data.lastY;
}

else if(ev.action=="Up")
{
var xOverall = ev.X - this.data.firstX;
var yOverall = ev.Y - this.data.firstY;
var msg = "whole touch moved\nX: "+
xOverall+"\nY: "+yOverall
//app.ShowPopup( msg );

Omer Meshy

unread,
Jun 24, 2020, 7:02:47 AM6/24/20
to DroidScript
Thank you.
I thought about that but I didnt know if there is an easy way.

Omer Meshy

unread,
Jun 24, 2020, 8:34:26 AM6/24/20
to DroidScript
It works! Thanks a lot.
Here is my code:

controlImg = app.CreateImage( null, 1, 1 );
controlImg.SetPosition(0, 0);
controlImg.SetOnTouch(ControlOnTouch);
controlImg.direction = "";
lay.AddChild(controlImg);

function ControlOnTouch(ev)
{    
    if(ev.action=="Down")
    {
        this.data.lastX = ev.X;
        this.data.lastY = ev.Y;
    }
    
    else if(ev.action=="Move")
    {
        // negative xMove is left
        var xMove = ev.X - this.data.lastX;
        // negative yMove is up
        var yMove = ev.Y - this.data.lastY;
        
    //txt.SetText(xMove + "\n" +  yMove + "\n" + controlImg.direction);
   
    if(xMove < 0 && (yMove < 0.001 && yMove > -0.001))
        {
            this.direction = "Left";
        }
        
        else if(xMove > 0 && (yMove < 0.001 && yMove > -0.001))
        {
            this.direction = "Right";
        }
        
        else if(yMove < 0 && (xMove < 0.005 && xMove > -0.005))
        {
            this.direction = "Up";
        }
        
        else if(yMove > 0 && (xMove < 0.005 && xMove > -0.005))
        {
            this.direction = "Down";
        }   
   
        this.data.lastX = ev.X;
        this.data.lastY = ev.Y;
    }
}

ב-יום רביעי, 24 ביוני 2020 בשעה 14:02:47 UTC+3, Omer Meshy כתב/ה:

Risky Idrus

unread,
Jun 24, 2020, 9:58:04 AM6/24/20
to DroidScript
Hello, is there anyone here who can help me? I have contacted the developer of DroidScript a few days ago but there has been no response to date even though I have subscribed to premium.

I use firebase. Can anyone give me a sample code or plugin to make "Sign in with Google" on DroidScript?

Anyone who can help me, please contact me via: riski...@gmail.com

Symbroson

unread,
Jun 24, 2020, 10:09:10 AM6/24/20
to DroidScript
Please start a new thead in this or the premium forum if your issue is not directly related to the topic in this thread

Alan Hendry

unread,
Jun 24, 2020, 10:22:05 AM6/24/20
to DroidScript
Idrus,
I presume that you want the user to type in a Google email and password.
Add Google Sign-In to Your Android App - https://developers.google.com/identity/sign-in/android/
(It looks like some-one would have to write a plugin in Android Studio to use this)
Regards

Omer Meshy

unread,
Jun 24, 2020, 2:51:16 PM6/24/20
to DroidScript
Hey again
I worked more on the code and know it works even better:

function ControlOnTouch(ev)
{    
    if(ev.action=="Down")
    {
        this.data.lastX = ev.X;
        this.data.lastY = ev.Y;
    }
    
    else if(ev.action=="Move")
    {
        // negative xMove is left
        var xMove = ev.X - this.data.lastX;
        // negative yMove is up
        var yMove = ev.Y - this.data.lastY;
   
    if(xMove < 0 && Math.abs(xMove) > Math.abs(yMove))
        {
            this.direction = "Left";
        }
        
        else if(xMove > 0 && Math.abs(xMove) > Math.abs(yMove))
        {
            this.direction = "Right";
        }
        
        else if(yMove < 0 && Math.abs(xMove) < Math.abs(yMove))
        {
            this.direction = "Up";
        }
        
        else if(yMove > 0 && Math.abs(xMove) < Math.abs(yMove))
        {
            this.direction = "Down";
        }
   
        this.data.lastX = ev.X;
        this.data.lastY = ev.Y;
    }
}

ב-יום רביעי, 24 ביוני 2020 בשעה 17:22:05 UTC+3, Alan Hendry כתב/ה:
Reply all
Reply to author
Forward
0 new messages