cocos2d-x 2.2.0 图片选中聚焦 ,图片描边 CCClippingNode 实现

效果如下图

左边箭头是x方向翻转的,右边箭头有旋转和缩放action。大概实现方法:用箭头作为遮罩层,底图是一个绘制的矩形,得到一个黄色箭头背景。在用schedule跟随要聚焦箭头动作。这个在电视端用遥控器上下左右选择聚焦有点用。希望这个是对同学们有帮助,谢谢。






代码

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  
    
    CCSprite *stencil = CCSprite::create("f1.png");
    stencil->setFlipX(true);
    
    CCClippingNode *clipper = CCClippingNode::create();
    clipper->setAlphaThreshold(0.005f);
    clipper->setAnchorPoint(ccp(0.5, 0.5));
    clipper->setPosition( ccp(visibleSize.width / 2 - 50, visibleSize.height / 2 - 50) );
    clipper->setStencil(stencil);
    
    this->addChild(clipper,0,cliper_tag);
    
    CCNode *content = shape(stencil->getContentSize());
    clipper->addChild(content,0,content_tag);
    
    CCSprite *updata = CCSprite::create("f1.png");
    updata->setFlipX(true);
    updata->setPosition( ccp(visibleSize.width / 2 - 50, visibleSize.height / 2 - 50) );
    this->addChild(updata,1,spr1_tag);
    
    
    CCSprite *updata2 = CCSprite::create("f2.png");
    updata2->setPosition( ccp(visibleSize.width / 2 + 50, visibleSize.height / 2 - 50) );
    updata2->runAction(CCRepeatForever::create(CCSequence::create(CCScaleTo::create(0.5, 1.3),CCRotateBy::create(2, 90),CCScaleTo::create(0.5, 1),NULL)));
    
    this->addChild(updata2,1,spr2_tag);
    
    
    this->setTouchEnabled(true);
    return true;
}

CCDrawNode* HelloWorld::shape(CCSize size)
{
    CCDrawNode *shape = CCDrawNode::create();
    static CCPoint shapedate[4];
    shapedate[0] = ccp(-(size.width / 2), -(size.height / 2));
    shapedate[1] = ccp((size.width / 2), -(size.height / 2));
    shapedate[2] = ccp((size.width / 2), (size.height / 2));
    shapedate[3] = ccp(-(size.width / 2), (size.height / 2));
    
    static ccColor4F yellow = {1, 1, 0, 1};
    shape->drawPolygon(shapedate, 4, yellow, 0, yellow);
    return shape;
}

void HelloWorld::setshaper(int tag){
    
    this->unschedule(schedule_selector(HelloWorld::cliperupdate));
    
    current_tag = tag;
    
    CCSprite* sp = (CCSprite*) this->getChildByTag(tag);
    CCClippingNode *clipper = (CCClippingNode*)this->getChildByTag(cliper_tag);
    
    CCSprite *stencil = CCSprite::createWithTexture(sp->getTexture());
    stencil->setFlipX(sp->isFlipX());
    stencil->setFlipY(sp->isFlipY());
    stencil->setScale(sp->getScale());
    stencil->setRotation(sp->getRotation());
    clipper->setStencil(stencil);
    clipper->setPosition(sp->getPosition());
    
    
    clipper->removeChildByTag(content_tag);
    clipper->addChild(shape(sp->getContentSize()),0,content_tag);
    
    
    
    this->schedule(schedule_selector(HelloWorld::cliperupdate), 0.05);
    
}

void HelloWorld::cliperupdate(float dt){
    
    CCSprite* sp = (CCSprite*) this->getChildByTag(current_tag);
    CCClippingNode *clipper = (CCClippingNode*)this->getChildByTag(cliper_tag);
    clipper->setPosition(sp->getPosition());
    
    CCSprite *stencil = (CCSprite*)clipper->getStencil();
    stencil->setFlipX(sp->isFlipX());
    stencil->setFlipY(sp->isFlipY());
    stencil->setScale(sp->getScale());
    stencil->setRotation(sp->getRotation());
    
    CCSprite *content = (CCSprite*)clipper->getChildByTag(content_tag);
    content->setScale(sp->getScale());
    content->setRotation(sp->getRotation());
    
    
}


bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
    
    
    
    CCPoint location = this->convertTouchToNodeSpace(pTouch);
    if (this->getChildByTag(spr1_tag)->boundingBox().containsPoint(location)) {
        setshaper(spr1_tag);
    }
    
    if (this->getChildByTag(spr2_tag)->boundingBox().containsPoint(location)) {
        setshaper(spr2_tag);
    }

    return true;
    
}
void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
    
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
    
}

void HelloWorld::registerWithTouchDispatcher(void){
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}
void HelloWorld::onEnter(){
    CCLayer::onEnter();
}
void HelloWorld::onExit(){
    CCLayer::onExit();
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

demo工程在这里下载 狂点我


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值