Starting from this chapter, we will begin to explainCocos2d-iPhoneEngine user input processing mechanism (User input), Also known as the Event Response Mechanism (Event-Handle).IPhoneThere are two types of user input: Touch input (TouchAnd motion perception (Accelerometer). This chapter focuses on the former.
Interaction is the relationship between games and traditional art (literature, music, and movies )....) The biggest difference between them is that a well-designed game is first designed to give users a good operation experience. The colorful world of World of Warcraft attracts the attention of millions of players?WowerMost of the time is not spent in busy run, fight, collection and transactions? Therefore, direct, convenient, and well-designed interaction is the key to a successful game.
IPhoneThe multi-point concurrent touch input on isPCFor this reasonIPhone OSThe programming interface provides a collection class ((Nsset *) touches) To pass the user's current input. The first part of this chapter is how to resolve this class and correct the response.
Based onIPhone OSEvent Mode,Cocos-2D-iPhoneHow does the engine transmit user input to eachLayerAnd allowProgramAccording to what established rules? Slave0.8.0Start, useTargeted touchThe Touch processing mode is the recommendation mode of the system, which helps the programmer to break down(Nsset *) touchesAnd provides a targeted layer-response distribution mechanism. This is the focus of the second part of this chapter.
This chapter provides a complete example of touch processing.
IPhone OS OfTouche Event Basic knowledge
Start IntroductionIPhone OSOf4Before responding to events, let's take a lookCocoaThe Base Class Library provides the following collection classes:NssetAnd the derived class of this classNsmutableset.IPhone OSPassNssetTransmits various combined touch information from hardware sensors.
Event Processing Framework
IPhone OSProvides (Touch).4Event Response functions:
L (Void) touchesbegan :( nsset *) touches withevent :( uievent *) event {}
L (Void) touchesmoved :( nsset *) touches withevent :( uievent *) event {}
L (Void) touchesended :( nsset *) touches withevent :( uievent *) event {}
L (Void) touchescancelled :( nsset *) touches withevent :( uievent *) event {}
The preceding steps show that the finger is touched, moved (the finger is not lifted), raised, and canceled. EveryUiviewThe object will receive the above4Events.
Above4Event processing function frameworks are basically the same:
1) Obtain all touch information.
Can be used directlyTouchesParameters:
Nsmutableset * mutabletouches = [touches mutablecopy];
You can also useEventObtain parameters:
Nsset * alltouches = [event alltouches];
2) Process each touch point in sequence
Pass[Alltouches count]To determine whether it is multi-contact or single-contact, obtain the first touch point method:
Uitouch * Touch = [[alltouches allobjects] objectatindex: 0];
Obtain the second touch point:
Uitouch * touch2 = [[alltouches allobjects] objectatindex: 1];
Third and fourth...Multi-touch, and so on.
3) Processing of each touch point
Use the following function to check whether each touch point is clicked or double-clicked:
[Touch tapcount]