android - Virtual Joystick - multiple actions instead of one action -
i'm using code virtual joystick website. works fine not application. have problem implementation of action. in joystick.class can control range of every direction if range between specific angle , distance returns int value of each direction.
public int get4direction() { if(distance > min_distance && touch_state) { if(angle >= 225 && angle < 315 ) { return stick_up; } else if(angle >= 315 || angle < 45 ) { return stick_right; } else if(angle >= 45 && angle < 135 ) { return stick_down; } else if(angle >= 135 && angle < 225 ) { return stick_left; } } else if(distance <= min_distance && touch_state) { return stick_none; } return 0; }
i used system.out.println show not right application. when put stick in specific direction example , left, generates output that:
08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.017 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.037 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.117 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.137 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.157 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.167 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.187 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.207 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.217 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.317 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.337 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.357 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.377 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.387 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.407 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.427 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.437 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.457 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.477 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.487 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.507 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.527 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.537 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.557 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.577 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ 08-18 14:02:10.587 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.607 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.627 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.637 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.657 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.677 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.687 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left 08-18 14:02:10.707 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left
multiple output same other directions. it's problem me because more complicated actions crashing app or app works slow , warning: the application may doing work on main thread
there way output once? example if stick in range of left direction, getenerating 1 line:
08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕ left
if etc.
08-18 14:02:10.007 11806-11806/com.example.aidan.joysticktest2 i/system.out﹕
it's checking angle , distance if/else statement, im looking solution allow me check range once every direction or similar. tried everything, using radiobuttons, checkbuttons, variables wasn't helpfull , couldn't 1 line of output.
full code:
joystick.class
mainactivity:
public class mainactivity extends activity { relativelayout layout_joystick; imageview image_joystick, image_border; textview textview1, textview2, textview3, textview4, textview5; joystickclass js; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview1 = (textview)findviewbyid(r.id.textview1); textview2 = (textview)findviewbyid(r.id.textview2); textview3 = (textview)findviewbyid(r.id.textview3); textview4 = (textview)findviewbyid(r.id.textview4); textview5 = (textview)findviewbyid(r.id.textview5); layout_joystick = (relativelayout)findviewbyid(r.id.layout_joystick); js = new joystickclass(getapplicationcontext(), layout_joystick, r.drawable.redball2); js.setsticksize(150, 150); js.setlayoutsize(500, 500); js.setlayoutalpha(150); js.setstickalpha(100); js.setoffset(90); js.setminimumdistance(50); layout_joystick.setontouchlistener(new ontouchlistener() { public boolean ontouch(view arg0, motionevent arg1) { js.drawstick(arg1); if(arg1.getaction() == motionevent.action_down || arg1.getaction() == motionevent.action_move) { textview1.settext("x : " + string.valueof(js.getx())); textview2.settext("y : " + string.valueof(js.gety())); textview3.settext("angle : " + string.valueof(js.getangle())); textview4.settext("distance : " + string.valueof(js.getdistance())); int direction = js.get4direction(); if(direction == joystickclass.stick_up) { textview5.settext("direction : up"); system.out.println("up"); } else if(direction == joystickclass.stick_right) { textview5.settext("direction : right"); system.out.println("right"); } else if(direction == joystickclass.stick_down) { textview5.settext("direction : down"); system.out.println("down"); } else if(direction == joystickclass.stick_left) { textview5.settext("direction : left"); system.out.println("left"); } else if(direction == joystickclass.stick_none) { textview5.settext("direction : center"); system.out.println("center"); } } else if(arg1.getaction() == motionevent.action_up) { textview1.settext("x :"); textview2.settext("y :"); textview3.settext("angle :"); textview4.settext("distance :"); textview5.settext("direction :"); } return true; } }); }
}
xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margin="10dp" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="x" android:textcolor="#444444" android:textsize="20dp" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview1" android:text="y" android:textcolor="#444444" android:textsize="20dp" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview2" android:text="angle" android:textcolor="#444444" android:textsize="20dp" /> <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview3" android:text="distance" android:textcolor="#444444" android:textsize="20dp" /> <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview3" android:text="direction" android:textcolor="#444444" android:textsize="20dp" /> </linearlayout> <relativelayout android:id="@+id/layout_joystick" android:layout_width="200dp" android:layout_height="200dp" android:layout_below="@+id/linearlayout1" android:layout_centerhorizontal="true" android:layout_margintop="29dp" android:background="@drawable/image_button_bg" > </relativelayout>
seems it's expected behavior, because fire once every action_down
, , multiple times action_move
. seems action_move
called many times. avoid process every action_move
event try add global variable:
int prevdirection = 0;
then in public boolean ontouch(view arg0, motionevent arg1)
add condition:
... int direction = js.get4direction(); if (direction != prevdirection) { if(direction == joystickclass.stick_up) { textview5.settext("direction : up"); system.out.println("up"); } else if(direction == joystickclass.stick_right) { ... prevdirection = direction; } ...
it's not solution, not spoil code of example, not ). ideally should create custom view.
Comments
Post a Comment