如何使星星从屏幕顶部掉落?

时间:2019-01-26 18:12:29

标签: java android-studio

public class MainActivity extends AppCompatActivity {
    private TextView scoreLabel;
    private TextView startLabel;
    private ImageView bag;
    private ImageView star;
    private ImageView square;
    private ImageView circle;
    // Position
    private int bagX;
    private int starX;
    private int starY;
    private int circleX;
    private int circleY;
    private int squareX;
    private int squareY;
    // Size
    private int frameWidth;
    private int bagSize;
    private int screenWidth;
    private int screenHeight;
    // Initialize Class
    private Handler handler = new Handler();
    private Timer timer = new Timer();
    // Status check
    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);
        bag = findViewById(R.id.bag);
        star = findViewById(R.id.star);
        square = findViewById(R.id.square);
        circle = findViewById(R.id.circle);
        // Move to out of screen
        star.setY(-80);
        star.setY(-80);
        square.setY(-80);
        square.setY(-80);
        circle.setY(-80);
        circle.setY(-80);
        // Get screen size
        WindowManager wm = getWindowManager();
        Display disp = wm.getDefaultDisplay();
        Point size = new Point();
        disp.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
    }

    public void changePos() {
        // Move bag
        if (action_flg == true) {
            // Touching
            bagX -= 20;
        } else {
            // Releasing
            bagX += 20;
        }
        // Check bag position
        if (bagX < 0) bagX = 0;
        if (bagX > frameWidth - bagSize) bagX = frameWidth - bagSize;
        bag.setX(bagX);
        // Star shapes
        starY -= 12;
        if(starY < 0) {
            starY = screenHeight + 20;
            starX = (int)Math.floor(Math.random() * (frameWidth - star.getWidth()));
        }
        star.setX(starX);
        star.setY(starY);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (start_flg == false) {
            start_flg = true;
            // Get frame width and bag width from here
            // The UI has not been set on the screen in OnCreate()
            FrameLayout frame = findViewById(R.id.frame);
            frameWidth = frame.getWidth();
            bagX = (int)bag.getX();
            // The bag is a square (height and width are the same)
            bagSize = bag.getWidth();
            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;
    }
}

我正在尝试在Android手机上创建App游戏。上面的代码使星星从底部移动到顶部。我想做相反的事情。我希望星星从屏幕的顶部下降到底部。如果有人可以告诉我或告诉我如何做到这一点,我将不胜感激。

预览

Game application

0 个答案:

没有答案