Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Per gestire gli eventi di input tocco, leggi l'array
motionEvents nel tuo ciclo di gioco. Questi
contengono eventi che si sono verificati dopo l'ultima volta in cui questi array sono stati cancellati.
Il numero di eventi contenuti è archiviato in motionEventsCount.
Esegui l'iterazione e gestisci ogni evento nel tuo ciclo di gioco. In questo esempio,
il seguente codice esegue l'iterazione di motionEvents e li gestisce tramite handle_event:
for(size_ti=0;i < mApp->motionEventsCount;++i){GameActivityMotionEvent*motionEvent=mApp->motionEvents[i];intaction=motionEvent->action;intactionMasked=action & AMOTION_EVENT_ACTION_MASK;intptrIndex=(action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)>>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;structCookedEventev;memset(&ev,0,sizeof(ev));if(actionMasked==AMOTION_EVENT_ACTION_DOWN||actionMasked==AMOTION_EVENT_ACTION_POINTER_DOWN){ev.type=COOKED_EVENT_TYPE_POINTER_DOWN;}elseif(actionMasked==AMOTION_EVENT_ACTION_UP||actionMasked==AMOTION_EVENT_ACTION_POINTER_UP){ev.type=COOKED_EVENT_TYPE_POINTER_UP;}else{ev.type=COOKED_EVENT_TYPE_POINTER_MOVE;}ev.motionPointerId=motionEvent->pointers[ptrIndex].id;ev.motionIsOnScreen=motionEvent->source==AINPUT_SOURCE_TOUCHSCREEN;ev.motionX=GameActivityPointerInfo_getX(&motionEvent->pointers[ptrIndex]);ev.motionY=GameActivityPointerInfo_getY(&motionEvent->pointers[ptrIndex]);if(ev.motionIsOnScreen){// Use screen size as the motion range.ev.motionMinX=0.0f;ev.motionMaxX=SceneManager::GetInstance()->GetScreenWidth();ev.motionMinY=0.0f;ev.motionMaxY=SceneManager::GetInstance()->GetScreenHeight();}handle_event(&ev);}
Al termine, ricordati di cancellare la coda degli eventi
gestiti:
android_app_clear_motion_events(mApp);
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-27 UTC.
[null,null,["Ultimo aggiornamento 2025-07-27 UTC."],[],[],null,["# Add touch support\n\nTo handle touch input events, read the array\n[`motionEvents`](/reference/android/view/MotionEvent) in your game loop. These\ncontain events that have happened since the last time these arrays were cleared.\nThe number of events contained is stored in `motionEventsCount`.\n| **Note:** Prior to the addition of `GameActivity`, the `AInputQueue* inputQueue` field was used to handle input events. With `GameActivity`, this field has been removed from the `android_app` and no longer used for handling input events.\n\n1. Iterate and handle each event in your game loop. In this example, the\n following code iterates `motionEvents` and handles them via `handle_event`:\n\n for(size_t i = 0; i \u003c mApp-\u003emotionEventsCount; ++i) {\n GameActivityMotionEvent* motionEvent = mApp-\u003emotionEvents[i];\n\n int action = motionEvent-\u003eaction;\n int actionMasked = action & AMOTION_EVENT_ACTION_MASK;\n int ptrIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) \u003e\u003e\n AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;\n\n struct CookedEvent ev;\n memset(&ev, 0, sizeof(ev));\n\n if (actionMasked == AMOTION_EVENT_ACTION_DOWN ||\n actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN) {\n ev.type = COOKED_EVENT_TYPE_POINTER_DOWN;\n } else if (actionMasked == AMOTION_EVENT_ACTION_UP ||\n actionMasked == AMOTION_EVENT_ACTION_POINTER_UP) {\n ev.type = COOKED_EVENT_TYPE_POINTER_UP;\n } else {\n ev.type = COOKED_EVENT_TYPE_POINTER_MOVE;\n }\n\n ev.motionPointerId = motionEvent-\u003epointers[ptrIndex].id;\n ev.motionIsOnScreen = motionEvent-\u003esource == AINPUT_SOURCE_TOUCHSCREEN;\n ev.motionX = GameActivityPointerInfo_getX(\n &motionEvent-\u003epointers[ptrIndex]);\n ev.motionY = GameActivityPointerInfo_getY(\n &motionEvent-\u003epointers[ptrIndex]);\n\n if (ev.motionIsOnScreen) {\n // Use screen size as the motion range.\n ev.motionMinX = 0.0f;\n ev.motionMaxX = SceneManager::GetInstance()-\u003eGetScreenWidth();\n ev.motionMinY = 0.0f;\n ev.motionMaxY = SceneManager::GetInstance()-\u003eGetScreenHeight();\n }\n\n handle_event(&ev);\n }\n\n2. When you are done, remember to clear the queue of events that you have just\n handled:\n\n android_app_clear_motion_events(mApp);"]]