Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Pour gérer les événements de saisie tactile, lisez le tableau motionEvents dans votre boucle de jeu. Ceux-ci contiennent les événements survenus depuis le dernier effacement de ces tableaux.
Le nombre d'événements contenus est stocké dans motionEventsCount.
Itérez et gérez chacun des événements de votre boucle de jeu. Dans cet exemple, le code suivant itère les motionEvents et les gère via 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);}
Lorsque vous avez terminé, n'oubliez pas de vider la file d'attente des événements que vous venez de gérer :
android_app_clear_motion_events(mApp);
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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);"]]