It is easy to use partial highlight. Most of the functions of partial highlight are to guide players step by step during novice guidance.
I wrote a version of the cocos2d-iphone before, because it is set to rounded corner, more beautiful, if you do not need rounded corner, you can directly change Sprite into layercolor, the disadvantage of this version is that each step needs to produce a resource (if the size of the click area in each step is different). Now this version can be set directly.
The idea of this version is to use layercolor to set the highlighted area first, and then add a sprite with a small rounded corner to the four corners of layercolor, in this way, you do not need to prepare art resources for every step. You can set the size of the highlighted area at will. I wanted to directly set the layercolor rounded corner without using sprite. I didn't find a proper method. If you have a more suitable and convenient method, please click it.
The principle is: asynchronous hybrid rendering is used during rendering. CcBlendFunc
Not to mention, directly add the code
In order to obtain the click event, it inherits CCTargetedTouchDelegate and sets the touch permission to the highest.
//. H
[Cpp]
# Ifndef Good_PlayGuide_h
# Define Good_PlayGuide_h
# Include "cocos2d. h"
Using namespace cocos2d;
Class PlayerGuide: public CCSprite, public CCTargetedTouchDelegate
{
Public:
PlayerGuide ();
Virtual ~ PlayerGuide ();
Virtual bool init ();
Virtual void onEnter ();
Virtual void onExit ();
Virtual bool ccTouchBegan (CCTouch * touch, CCEvent * event );
Virtual void ccTouchMoved (CCTouch * touch, CCEvent * event );
Virtual void ccTouchEnded (CCTouch * touch, CCEvent * event );
Void onSetSpriteAndPosition (float width, float height, CCPoint point );
CCLayerColor * m_layer; // color layer
Float m_layerWidth; // layer width
Float m_layerHeight; // layer height
CCRenderTexture * m_pTarget; // render texture
};
# Endif
[Cpp]
# Include <iostream>
# Include "PlayerGuide. h"
PlayerGuide: PlayerGuide ()
{
M_layerWidth = 0.0f;
M_layerHeight = 0.0f;
}
PlayerGuide ::~ PlayerGuide ()
{
}
Bool PlayerGuide: init ()
{
If (! CCSprite: init ()){
Return false;
}
Return true;
}
Void PlayerGuide: onEnter ()
{
// Since v2.0
// CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this,-201, true );
// When you enter this object, set the touch Event Response permission. The menu response level is-128. We need to obtain higher permissions than this (the lower the set value, higher permissions)
CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this,-201, true );
CCSprite: onEnter ();
}
Void PlayerGuide: onExit ()
{
// Since v2.0
// CCDirector: sharedDirector ()-> getTouchDispatcher ()-> removeDelegate (this );
// Remove the touch RESPONSE event when exiting
CCTouchDispatcher: sharedDispatcher ()-> removeDelegate (this );
CCSprite: onExit ();
}
# Pragma mark -------------------------- setSpriteAndPosition ----------------------------
/*************************************** ****************************************
* @ Param width: sets the width of the highlighted area, height: Specifies the height of the highlighted area, and point: sets the position of the layer.
*
*
**************************************** ***************************************/
Void PlayerGuide: onSetSpriteAndPosition (float width, float height, CCPoint point)
{
If (width! = 0 & height! = 0 ){
M_layerWidth = width;
M_layerHeight = height;
M_layer = CCLayerColor: layerWithColorWidthHeight (ccc4 (0x00, 0x00, 0x00, 0xff), width, height );
M_layer-> retain ();
CcBlendFunc ccb = {GL_ZERO, GL_ONE_MINUS_SRC_ALPHA };
M_layer-> setBlendFunc (ccb );
M_layer-> setPosition (point );
CCSprite * topright = CCSprite: spriteWithFile ("yourfile"); // you can remove sprite directly if you do not need a rounded corner.
Topright-> getTexture ()-> setAliasTexParameters ();
CcBlendFunc cbf = {GL_ONE, GL_ONE_MINUS_DST_ALPHA };
Topright-> setBlendFunc (cbf );
Topright-> setPosition (ccp (m_layer-> getContentSize (). width-topright-> getContentSize (). width/2, m_layer-> getContentSize (). height-topright-> getContentSize (). height/2 ));
Topright-> setOpacity (255*0.5 );
M_layer-> addChild (topright, 1 );
CCSprite * topleft = CCSprite: spriteWithFile (IMG_PATH (IMAGE_PLAYERGUIDE_ROUNDED ));
Topleft-> getTexture ()-> setAliasTexParameters ();
CcBlendFunc cbf1 = {GL_ONE, GL_ONE_MINUS_DST_ALPHA };
Topleft-> setBlendFunc (cbf1 );
Topleft-> setPosition (ccp (topleft-> getContentSize (). width/2, m_layer-> getContentSize (). height-topleft-> getContentSize (). height/2 ));
Topleft-> setOpacity (255*0.5 );
Topleft-> setFlipX (true );
M_layer-> addChild (topleft, 1 );
CCSprite * buttomleft = CCSprite: spriteWithFile (IMG_PATH (IMAGE_PLAYERGUIDE_ROUNDED ));
Buttomleft-> getTexture ()-> setAliasTexParameters ();
CcBlendFunc cbf2 = {GL_ONE, GL_ONE_MINUS_DST_ALPHA };
Buttomleft-> setBlendFunc (cbf2 );
Buttomleft-> setPosition (ccp (buttomleft-> getContentSize (). width/2, buttomleft-> getContentSize (). height/2 ));
Buttomleft-> setOpacity (255*0.5 );
Buttomleft-> setFlipX (true );
Buttomleft-> setFlipY (true );
M_layer-> addChild (buttomleft, 1 );
CCSprite * buttomright = CCSprite: spriteWithFile (IMG_PATH (IMAGE_PLAYERGUIDE_ROUNDED ));
Buttomright-> getTexture ()-> setAliasTexParameters ();
CcBlendFunc cbf3 = {GL_ONE, GL_ONE_MINUS_DST_ALPHA };
Buttomright-> setBlendFunc (cbf3 );
Buttomright-> setPosition (ccp (m_layer-> getContentSize (). width-buttomleft-> getContentSize (). width/2, buttomright-> getContentSize (). height/2 ));
Buttomright-> setOpacity (255*0.5 );
Buttomright-> setFlipY (true );
M_layer-> addChild (buttomright, 1 );
}
CCSize s = CCDirector: sharedDirector ()-> getWinSize ();
M_pTarget = CCRenderTexture: renderTextureWithWidthAndHeight (s. width, s. height );
CcBlendFunc ccb1 = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
M_pTarget-> getSprite ()-> setBlendFunc (ccb1 );
M_pTarget-> clear (0.0f, 0.0f, 0.0f, 0.5f );
M_pTarget-> setPosition (ccp (s. width/2, s. height/2 ));
This-> addChild (m_pTarget );
M_pTarget-> begin ();
If (width! = 0 & height! = 0 ){
M_layer-> visit ();
}
M_pTarget-> end ();
}
Bool PlayerGuide: ccTouchBegan (cocos2d: CCTouch * touch, cocos2d: CCEvent * event)
{
CCPoint touchpoint = touch-> locationInView (touch-> view ());
Touchpoint = CCDirector: sharedDirector ()-> convertToGL (touchpoint );
If (m_layerWidth! = 0 & m_layerHeight! = 0 ){
// If you click the highlighted area, the lower area is returned.
CCRect rect = m_layer-> boundingBox ();
If (CCRect: CCRectContainsPoint (rect, touchpoint )){
Return false;
}
Return true;
}
Void PlayerGuide: ccTouchMoved (cocos2d: CCTouch * touch, cocos2d: CCEvent * event)
{
}
Void PlayerGuide: ccTouchEnded (cocos2d: CCTouch * touch, cocos2d: CCEvent * event)
{
}
Author: nong1209