Cocos2d-x3.0 不规则Button

这是我参照之前在iOS项目中用过的一个不规则形状按钮的第三方Button,这里用Cocos2d-x实现一个相似功能的按钮。

原文地址:http://blog.csdn.net/qqmcy/article/details/26161339

代码下载:http://download.csdn.net/detail/qqmcy/7365843

使用方法:

.h

//
//  TestScene.h
//  maptest
//
//  Created by 杜甲 on 14-5-18.
//
//

#ifndef __maptest__TestScene__
#define __maptest__TestScene__

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "DJShapeButton.h"
USING_NS_CC;

class TestScene :public Layer,public DJShapeButtonDelegate{

public:
    static Scene* createScene();
    virtual bool init();

    CREATE_FUNC(TestScene);
    void buttonCallBack(DJShapeButton* sender);

};

#endif /* defined(__maptest__TestScene__) */
        DJShapeButton* djShapeBtn  = DJShapeButton::create("res/red/1300_r.png");
        djShapeBtn->setDelegate(this);
        //设置tag
        djShapeBtn->setShapeBtnTag(1000);
        djShapeBtn->setPosition(Point(0, 40));
        addChild(djShapeBtn);

回调函数

void TestScene::buttonCallBack(DJShapeButton *sender)
{
    log("%d",sender->getTag());
}

DJShapeButton.h不规则形状Button类

//
//  DJShapeButton.h
//  maptest
//
//  Created by 杜甲 on 14-5-18.
//
//

#ifndef __maptest__DJShapeButton__
#define __maptest__DJShapeButton__

class DJShapeButton;

class DJShapeButtonDelegate {

public:
    virtual void buttonCallBack(DJShapeButton* sender) = 0;

};

#include "cocos2d.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

class DJShapeButton :public ui::Layout{

public:
    static DJShapeButton* create(const std::string& normalImage);

    CC_SYNTHESIZE(DJShapeButtonDelegate*, _delegateBtn, Delegate);

    virtual bool init(const std::string& normalImage);

    CC_SYNTHESIZE_RETAIN(Sprite*,  shapeBtn, ShapeBtn);

    void setShapeBtnTag(int tag);
private:
    bool isSwallow ;

};

#endif /* defined(__maptest__DJShapeButton__) */

DJShapeButton.cpp

//
//  DJShapeButton.cpp
//  maptest
//
//  Created by 杜甲 on 14-5-18.
//
//

#include "DJShapeButton.h"
DJShapeButton* DJShapeButton::create(const std::string &normalImage)
{
    DJShapeButton *pRet = new DJShapeButton();
    if (pRet && pRet->init(normalImage))
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }

}
bool DJShapeButton::init(const std::string &normalImage)
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!ui::Layout::init());

        Image* myImg = new Image();
        myImg->initWithImageFile(normalImage);

        Texture2D* temp = new Texture2D();
        temp->initWithImage(myImg);

        shapeBtn =Sprite::createWithTexture(temp);

        log("%f,%f",shapeBtn->getAnchorPoint().x ,shapeBtn->getAnchorPoint().y);
        log("width = %f,height = %f",shapeBtn->getContentSize().width , shapeBtn->getContentSize().height);

        addChild(shapeBtn);

        this->setSize(shapeBtn->getContentSize());
        shapeBtn->setPosition(Point(getSize().width / 2, getSize().height / 2));

        auto listener1 = EventListenerTouchOneByOne::create();

        listener1->onTouchBegan = [=](Touch* touch, Event* event)
        {
            auto target = static_cast<Sprite*>(event->getCurrentTarget());
            Point locationInNode = target->convertToNodeSpace(touch->getLocation());
            Color4B c = {0,0,0,0};

            Point pt = Point(locationInNode.x, target->getContentSize().height - locationInNode.y);

            Size s = target->getContentSize();
            Rect rect = Rect(0, 0, s.width, s.height);

            if (rect.containsPoint(locationInNode)) {
                log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);

                unsigned int x = pt.x, y = pt.y;
                unsigned char* data = myImg->getData();
                unsigned int* pixel = (unsigned int*)data;
                pixel = pixel + (y * 116) + x;
                c.r = *pixel & 0xff;
                c.g = (*pixel >> 8 ) & 0xff;
                c.b = (*pixel >> 16) & 0xff;
                c.a = (*pixel >> 24) & 0xff;
                log("8888888888%d",c.a);
                if (c.a <= 4) {
                    isSwallow = false;
                }else
                {
                    isSwallow = true;

                }

            }else{
                isSwallow = false;
            }

            listener1->setSwallowTouches(isSwallow);

            return isSwallow;
        };

        listener1->onTouchEnded =  [=](Touch* touch, Event* event)
        {
            if (_delegateBtn != nullptr) {
                _delegateBtn->buttonCallBack(this);
            }
        };

        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, shapeBtn);

        bRet = true;
    } while (0);
    return bRet;
}

void DJShapeButton::setShapeBtnTag(int tag)
{
    shapeBtn->setTag(tag);
    setTag(tag);

}

Cocos2d-x3.0 不规则Button

时间: 2024-12-13 02:08:07

Cocos2d-x3.0 不规则Button的相关文章

cocos2d(3.0)一些基础的东西

1.创建项目后环境配置: 附加文件夹:加入 $(EngineRoot) $(EngineRoot)cocos $(EngineRoot)cocos\editor-support $(EngineRoot)build\Debug.win32 ..\proj.win32 通用属性加入 (先从 cocos2d-x-3.0rc0 中 extensions   cocos\editor-support   cocos\ui 加入进去) libcocosstudio libExtensions libGUI

如何在Cocos2D 1.0 中掩饰一个精灵(六)

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并且在init方法上方添加如下方法: - (CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite { // 1 CCRenderTexture * rt = [CCRe

cocos2d 3.0 屏幕适配

1.解决方案 先直接给出解决方案,再慢慢解释,当然这个解决方案也不是完全完美的. [cpp] view plaincopy //如果是横屏游戏: glview->setDesignResolutionSize(960, 640, ResolutionPolicy::FIXED_HEIGHT); //一张960x640的背景显示 auto visibleSize = Director::getInstance()->getVisibleSize(); auto Bg = Sprite::crea

DISCUZ X3.0登录代码

// 初始化UC if(!function_exists('uc_user_login')) { loaducenter(); } //加载uc_user_login()方法,位于uc_client/client.php之中,执行登录第一步 $result = uc_user_login('admin', '[email protected]', 0, 1); 下一步将结果集序列化 list($tmp['uid'], $tmp['username'], $tmp['password'], $tm

DISCUZ X3.0 积分增减以及记录

1.调用source/function/function_core.php 里面的 updatemembercount()方法,该方法只是一个简单的入口方法 /* * @$uids 用户 * @$dataarr 操作规则,如扣减第二个积分2分:array ('extcredits2' => -2); * @$checkgroup 是否检查用户组升级,通常未true * @$operation 操作类型,默认空,如果需要增加记录,需要填充 * @$relatedid 关系ID,例如帖子ID * @

最新版本cocos2d&#173;2.0&#173;x&#173;2.0.2使用新资源加载策略!不再沿用-hd、-

 前段时间cocos2dx更新了最新版本cocos2d-2.0-x-2.0.2,也从这个版本开始对于资源加载与管理都改变了策略. 在之前的加载方式都是通过沿用与cocos2d-iphone一样的加载资源方式,对于图片名后添加-hd,-ipad,-ipadhd方式,当用户开启项目的高清视网膜后就可以默认寻找对应的资源.但是从cocos2d-2.0-x-2.0.2版本开始,资源加载策略不在如此了.对跨平台整合不清楚的请看cocos2dx最新2.x版本跨平台整合NDK+Xcode 最新资源加载策略

COCOS2D-X 3.0 文字button 不显示 或者闪动

由于这些控件和背景在addChild是在同一层,有时候被背景挡住了 把文字.button之类的addChild第二个參数设置得比背景大就OK了 老版本号cocos2d-x 2.1.3即使是同一层.也是后addChild的会覆盖之前addChild的对象,即使在同一层.可惜如今没这么方便了

Cocos2d 3.0继承自Sprite的类在addChild后出现故障

当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法,由于父类Sprite里的_textureAtlas须要初始化为null,在继承自Sprite的子类里的构造函数或init方法里加入Sprite::init()就能够了.

Swift3.0学习之Button

1.根据字符串的长度确定Button的长度(button的高度给定) let hight:CGFloat = 30.0 let size1 = CGSize(width: getLabWidth(labelStr: "我的升级换代卡号多少", font: UIFont.systemFont(ofSize: 14), height: hight), height: hight) let rect1 = CGRect(origin: CGPoint(x:40,y:200), size: s