Zhiyi game development tutorial cocos2d-x transplant version 004

Source: Internet
Author: User
Document directory
  • Ccstandardtouchdelegate
  • Cctargetedtouchdelegate

We know that cocos2d-x is a C ++ porting version of The cocos2d-iphone project, which has cross-platform features. At the same time cocos2d-x and cocos2d-iphone to maintain a high degree of synchronization, which fundamentally limits it is a mobile phone, tablet and other devices tailored to the game engine. The support for Win32 and other platforms is only for convenient development and debugging.
If you are preparing to develop a PC game, you are right to use the engine specifically designed for PC. Although such a sentence appeared in an article about cocos2d-x, but I think some things are still clear, no one's time should be wasted.

If you are adding PC-side development to cocos2d-x like me, there are a lot of concepts to note.
For example, the mouse that we often use on a PC turns into a finger on a cell phone or tablet. Of course, this is not only a media change, but also a change in usage habits.

The disappearing mouse goes through the status
As we all know, a button on a PC usually contains four states: Normal, mouse passing, mouse pressing, and disabling, while on the mobile phone and panel, A button usually has only three States: Normal, pressed, and disabled.
This is easy to understand.
First, no matter where the mouse is placed, it will not be able to break away from the display screen after all, and it is much more difficult to let a person put his finger on the touch screen at all times.
Second, the mouse has buttons, but the human hand has no buttons. It is difficult to move the mouse away from the click.
Therefore, on mobile phones and tablets, the mouse passing through the traditional meaning is almost nonexistent.

Magical multi-touch
For mobile phone and tablet users, multi-point touch screens are common. Although PCS also have peripherals that support multi-point input, software that supports this feature is not common. For those who are familiar with PC, it is easier to accept single point of mouse input.

The so-called habit is just getting used to it. It takes a long time, that is, the thing. Today, let's get to know this Touch ).

Cocos2d-x touch event processing

In cocos2d-x, touch is an important means of interaction. Although we did not explicitly state this touch event before, we did not leave it for a moment. The trigger of each Menu is actually a touch.

CCStandardTouchDelegate

To be precise, we have a relationship with Touch. In article 1, we have implemented the function of moving the ship after clicking the screen.

1 void GameLayer: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) 2 {3 CCTouch * touch = (CCTouch *) pTouches-> anyObject (); 4 // obtain the coordinates of the touch point 5 CCPoint location = touch-> locationInView (touch-> view (); 6 CCPoint convertedLocation = CCDirector: sharedDirector () -> convertToGL (location); 7 // Let the spacecraft move past 8 flight in one second-> runAction (CCMoveTo: actionWithDuration (1.0f, ccp (convertedLocation. x, convertedLocation. y); 9}

However, to make the above Code take effect, you must enable the touch event in initialization.

1 bool GameLayer::init(void) 2 { 3 ... 4 // accept touch now!5 setIsTouchEnabled(true); 6 ... 7 }

Follow in to see how to enable touch events.

 1 void CCLayer::setIsTouchEnabled(bool enabled) 2 { 3 if (m_bIsTouchEnabled != enabled) 4 { 5 m_bIsTouchEnabled = enabled; 6 if (m_bIsRunning) 7 { 8 if (enabled) 9 { 10 this->registerWithTouchDispatcher(); 11 } 12 else 13 { 14 // have problems?15 CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); 16 } 17 } 18 } 19 } 20  21 void CCLayer::registerWithTouchDispatcher() 22 { 23 CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); 24 }

The above Code adds a standard proxy through CCTouchDispatcher to enable the touch function.

The standard proxy means that you can receive all the messages-Began, Moved, Ended, and Cancelled ). The information of the touch point is represented by CCSet.

High flexibility is the same as high permissions, and the cost of high flexibility is that you have to write additional code on your own.

The flexibility is too high. Instead, I don't know where to start. I just don't want to talk about it. I will talk about it later when I need it. But one thing to mention is that the standard proxy type is basically the same as Apple's CocoaTouch framework, so most of cococoatouch's solutions are also feasible in cocos2d-x.

CCTargetedTouchDelegate

Compared with the standard proxy, the target proxy features are naturally weaker, but it is much easier to use.

There are two significant benefits to using the target Proxy:
1. You don't have to deal with CCSet. The scheduler will split it for you and get a single Touch event each time you use it.
2. You can "claim" a touch event by making ccTouchBegan return true. A "claimed" Touch event is only distributed to the proxy object of the "claimed" event. In this way, you can be freed from the multi-touch detection of the bitter sea.

Let's conduct a practical drill. Suppose we have received a task that requires an genie to be displayed on the screen, which is a sequential Frame Animation for loop playback. We can drag it when we press it on the Genie. If it is outside the genie, it cannot be dragged.

First, analyze the tasks we receive. There is no difficulty in displaying a sequence Frame Animation genie For Loop playback. We have done this many times before. You can drag it when you press it on the Genie. You cannot drag it when you press it on the outside. If you use a standard proxy object, you must add a large number of judgments and States, obviously, we should give priority to the target proxy.

Therefore, we need an genie that meets the target proxy, so this is a multi-inheritance class, which has two parent classes-CCSprite and CCTargetedTouchDelegate.

1 class KillingLight : public CCSprite, public CCTargetedTouchDelegate 2 { 3 public: 4 KillingLight(void); 5 virtual ~KillingLight(void); 6 }

To receive a touch event, you must first register it. If you no longer need it, you must perform a reverse registration. We rewrite the onEnter and onExit functions of CCNode to complete registration and anti-Registration of touch events.

1 void killinglight: onenter (void) 2 {3 ccsprite: onenter (); 4 // We Want to exclusive "claimed" Touch events, so the 3rd parameters are passed into true5 cctouchdispatcher: shareddispatcher ()-> addtargeteddelegate (this, 0, true); 6} 7 8 void killinglight: onexit (void) 9 {10 cctouchdispatcher:: shareddispatcher ()-> removedelegate (this); 11 ccsprite: onexit (); 12}

Only the "claim" Touch event can be used in the target proxy object. Therefore, we need to rewrite the cctouchbegan function of cctargetedtouchdelegate.

Because the task must be dragged only when it is placed on the sprite, we need to add a judgment to determine whether the touch point is on the sprite.

1 ccrect killinglight: rect (void) 2 {3 // The comparison follows 4 const ccsize & s = gettexturerect () based on the anchor (). size; 5 const ccpoint & P = This-> getanchorpointinpixels (); 6 RETURN ccrectmake (-P. x,-P. y, S. width, S. height); 7} 8 9 bool killinglight: containstouchlocation (cctouch * Touch) 10 {11 return ccrect: ccrectcontainspoint (rect (), converttouchtonodespaceaR (touch); 12} 13 14 bool killinglight: cctouchbegan (cctouch * ptouch, ccevent * pevent) 15 {16 if (! Containstouchlocation (ptouch) 17 return false; 18 19 Return true; 20}

Next, we will rewrite the cctouchmoved function of cctargetedtouchdelegate to implement the drag function.

1 void KillingLight::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) 2 { 3 CCPoint touchPoint = pTouch->locationInView(pTouch->view()); 4 touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); 5 setPosition(touchPoint); 6 }

For ease of use, we add another static member function killinglightwithbatchnod.E to create a killinglight object.

 1 KillingLight* KillingLight::KillingLightWithBatchNode(cocos2d::CCSpriteBatchNode *batchNode, const cocos2d::CCRect& rect) 2 { 3 KillingLight *pobSprite = new KillingLight(); 4 if (pobSprite && pobSprite->initWithBatchNode(batchNode, rect)) 5 { 6 pobSprite->autorelease(); 7 return pobSprite; 8 } 9 CC_SAFE_DELETE(pobSprite); 10 return NULL; 11 }

In this way, a wizard class that supports clicking and dragging is complete, and you can use it like using ccsprite.

If you want to make this class more perfect, for example, to only respond to the first pay-as-you-go touch event so as not to grab more than one Genie, you can refer to the paddle class in touchestest.

Download Code:
Http://files.cnblogs.com/cocos2d-x/ZYG004.rar

When processing touch events, you must note the coordinates of coordinates. This involves many layers, including the screen coordinate system or GL coordinate system, the world coordinate system, the local coordinate system, and the point relative to which the coordinates are calculated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.