android - Drawable's movement with buttons -


i've been learning android month, , want make simple game custom class extends view, , it's included on main_activity.xml. in main activity.class create instance of view, , control movement of sprite on gameview buttons, each button has method control sprite's movement this:

public void move_up(view v){ gameview.sprite.move(dir.up);} 

the problem works when button released, , it's executed 1 time. want method executed while botton pressed can't figure out how this.

i'm assuming name of method you're adding onclick property xml layout file in order handle event. while may seem convenient @ first not give need.

instead, should implement ontouchlistener gives information touch event happening, in case you'd want handle action_down action:

findviewbyid(r.id.btn_up).setontouchlistener(new view.ontouchlistener() {     @override     public boolean ontouch(view v, motionevent event) {         if(event.getaction() == motionevent.action_down){             gameview.sprite.move(dir.up); //or whatever             return true; //in case wan handle action_up         }         return false;     } }); 

although make activity implement listener , handle multiple buttons there. achieved telling activity implement ontouchlistener, imeplementing method ontouch in code above instead method of activity.

and resulting set, simplified findviewbyid(r.id.btn_up).setontouchlistener(this); r.id.btn_up id you've defined in xml file.

this make start moving when press, instead of when release, if want make move until release (which make sense), like:

if(event.getaction() == motionevent.action_down){     gameview.sprite.move(dir.up); //or whatever     return true; //in case wan handle action_up }else if(event.getaction() == motionevent.action_up){     gameview.sprite.stop(); //or whatever call stop method     return false; } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -