游戏中的onTouchEvent()

问题描述 投票:-3回答:1

我正在使用android开发游戏应用程序。

对象(框)上下滑动。并且它必须击中从屏幕右端朝向它的物体(橙色和粉红色的球),这将增加他的分数。

还会有黑球(从屏幕右端拍摄),他应该避免击中。


enter image description here

我有问题

onTouchEvent(MotionEvent me)

实现代码时的功能。

我正在关注thisthis series of tutorials教程。


我的问题:

  1. 要使用onTouchEvent(MotionEvent me)函数,我是否需要导入任何类?
  2. 本教程已经在onTouchEvent(MotionEvent me)方法之外声明了onCreate。哪个没关系。但该计划并没有在任何地方调用它。它是如何工作的呢?
  3. 在编写教程中提到的代码之后,程序无法按预期工作。活动开始时会出现该框。但是,一旦我点击屏幕,它就会消失。可能是什么问题呢?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/scoreLabel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text=" : 300"
        android:paddingLeft="10dp"
        android:gravity="center_vertical"  />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/startLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="130dp"/>

        <ImageView

            android:id="@+id/box"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/box"
            android:layout_gravity="center_vertical" />

        <ImageView
            android:id="@+id/orange"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/orange" />

        <ImageView
            android:id="@+id/black"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/black" />

        <ImageView
            android:id="@+id/pink"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/pink" />



    </FrameLayout>

</RelativeLayout>

main activity.Java

package com.example.catcheggs1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView scoreLabel;
    private TextView startLabel;
    private ImageView box;
    private ImageView orange;
    private ImageView black;
    private ImageView pink;

    //Position
    private int boxY;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoreLabel=(TextView)findViewById(R.id.scoreLabel);
        startLabel=(TextView)findViewById(R.id.startLabel);
        box=(ImageView)findViewById(R.id.box);
        orange=(ImageView)findViewById(R.id.orange);
        pink=(ImageView)findViewById(R.id.pink);
        black=(ImageView)findViewById(R.id.black);

        //Move To Out of Screen
        orange.setX(-80);
        orange.setY(-80);
        pink.setX(-80);
        pink.setY(-80);
        black.setX(-80);
        black.setY(-80);


        //Temporary
        startLabel.setVisibility(View.INVISIBLE);
        boxY=500;


    }

    public boolean onTouchEvent(MotionEvent me)
    {
       if(me.getAction()==MotionEvent.ACTION_DOWN)
       {
           boxY -= 1 ;
       }
       box.setY(boxY);

       return true;
    }
}
android android-layout ontouchevent motionevent
1个回答
0
投票

1.)不,你不需要进口任何东西。

2.)有关详细信息,您可以看到链接。它是一个事件方法,它会自动调用,您不需要调用它。 https://developer.android.com/guide/topics/ui/ui-events#java

3.)您在触摸时使用boxY变量并为其分配任意数字。您的问题很可能是由此造成的。而不是那样,你应该首先得到当前的位置,而不是调整它。您可以使用此方法获取当前位置。

int[] location = new int[2];
imageView.getLocationOnScreen(location);

https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])

Bonus。)onTouchEvent是一个活动方法,因此您应该使用视图自己的事件侦听器来执行特定任务,而不是onTouchEvent。

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {   
        return true;
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.