我在android中制作了一个简单的游戏,中间的精灵由4个按钮控制,我希望分配给该按钮的方法在按下时按相应的方向移动精灵,我已尝试在onCreate方法中执行此操作:
def times_table(times,factor):
"""takes two positive integers and returns a list of lists
int, int -> list of lists"""
table = [[0]*(factor+1)]
for i in range(1,times+1):
table.append(list(range(0,i*factor+1,i)))
return table
def print_table(listx):
"""takes a list of lists of numbers and displays them on the screen
list of numbers -> numbers"""
for x in listx:
for y in x:
print('{0:<10}'.format(y), end='')
print()
number1 = int(input("What is the maximum value for the first factor?"))
number2 = int(input("What is the maximun value for the second factor?"))
print("Here is your times table:")
table = times_table(number1, number2)
print_table(table)
但是按下按钮时这只能工作一次。我希望在按下按钮时执行该方法,以及如何控制方法的更新时间。
答案 0 :(得分:0)
boolean fingerDown = false;
button1.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event){
if(event.getAction == MotionEvent.ACTION_DOWN){
fingerDown = true;
move_sprite();
} else if ( event.getAction() == MotionEvent.ACTION_UP ){
fingerDown = false;
}
return true;
}
});
public void move_sprite(){
// in new thread, or handler,
while(fingerDown){
// do your thing
}
}