-
-
Save codeforfun-jp/559df2f5793742e17b95bb4677729dc7 to your computer and use it in GitHub Desktop.
CTB 4-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.codeforfun.catchtheball; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
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); | |
} | |
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