私はいくつかの助けが必要です。オンスクリーンのナビゲーションボタンを使用してビットマップオブジェクトを移動する
私は、画面上に配置されたビットマップイメージと4つのナビゲーションボタン(上、下など)を持っています。今、私はそれらのボタンを使用して画像を移動したいと思います。もちろん、ボタンが押されていれば画像は動き続け、ユーザーが指を離すと画像は停止します。今私は、以下のコードが意図したとおりに動作するかどうかわからないので、これが私がここに来た理由です。ここ
コードです:ここ
画像です:
//SRC-Rect Values
int playerLeft, playerTop, playerBottom, playerRight;
ここで、画面上の画像の位置は次のとおりです。ここ
//DEST-Rect Values
int destBottom, destRight, destTop, destLeft;
はボタンである:
//Arrow Buttons to move the player
up = (Button)findViewById(R.id.buttonUp);
right = (Button)findViewById(R.id.buttonRight);
down = (Button)findViewById(R.id.buttonDown);
left = (Button)findViewById(R.id.buttonLeft);
//Up Button
up.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (true){
velocityY++;
destTop += topMove;
destBottom += bottomMove;
}
}
});
//Right Button
right.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (true){
velocityX++;
destRight += rightMove;
destLeft += leftMove;
}
}
});
//Down Button
down.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (true){
velocityY--;
destTop -= topMove;
destBottom -= bottomMove;
}
}
});
//Left Button
left.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (true){
velocityX--;
destRight -= rightMove;
destLeft -= leftMove;
}
}
});
ベロシティXとYは移動中の画像の速度、 であり、移動ボタンの値は、ボタンがタッチされたときに移動する必要があるピクセルの量です。ここで
//Arrow Buttons
private Button up, right, down, left;
//Movement Button Values
public static int topMove = -20;
public static int rightMove = -20;
public static int bottomMove = 20;
public static int leftMove = 20;
//Movement speed
public static int velocityX = 0;
public static int velocityY = 0;
は、XMLのレイアウトです:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonUp"
android:background="@drawable/up"
android:layout_above="@+id/buttonLeft"
android:layout_toRightOf="@+id/buttonLeft"
android:layout_toEndOf="@+id/buttonLeft" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonLeft"
android:background="@drawable/left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRight"
android:background="@drawable/right"
android:layout_alignTop="@+id/buttonDown"
android:layout_toRightOf="@+id/buttonDown"
android:layout_toEndOf="@+id/buttonDown" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonDown"
android:background="@drawable/down"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/buttonUp"
android:layout_alignStart="@+id/buttonUp" />