Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CTB 4-comp
package com.codingwithsara.catchtheball;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView scoreLabel;
private TextView startLabel;
private ImageView box;
private ImageView orange;
private ImageView pink;
private ImageView black;
// サイズ
private int frameHeight;
private int boxSize;
// 位置
private float boxY;
// Handler & Timer
private Handler handler = new Handler();
private Timer timer = new Timer();
// Status
private boolean action_flg = false;
private boolean start_flg = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreLabel = findViewById(R.id.scoreLabel);
startLabel = findViewById(R.id.startLabel);
box = findViewById(R.id.box);
orange = findViewById(R.id.orange);
pink = findViewById(R.id.pink);
black = findViewById(R.id.black);
orange.setX(-80.0f);
orange.setY(-80.0f);
pink.setX(-80.0f);
pink.setY(-80.0f);
black.setX(-80.0f);
black.setY(-80.0f);
boxY = 500.0f;
}
public void changePos() {
if (action_flg) {
boxY -= 20;
} else {
boxY += 20;
}
if (boxY < 0) boxY = 0;
if (boxY > frameHeight - boxSize) boxY = frameHeight - boxSize;
box.setY(boxY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (start_flg == false) {
start_flg = true;
FrameLayout frame = findViewById(R.id.frame);
frameHeight = frame.getHeight();
boxY = box.getY();
boxSize = box.getHeight();
startLabel.setVisibility(View.GONE);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}, 0, 20);
} else {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
action_flg = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
action_flg = false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.