【转】CCScale9Sprite和CCControlButton
转自:http://blog.csdn.net/nat_myron/article/details/12975145
在2dx下用到了android下的.9.png图片,下面是原图
查了一下2dx里有CCScale9Sprite,直接贴上背景图,毫无问题,
- CCSize bgRect = CCSizeMake(size.width,size.height/3);
- CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
- background->setContentSize(bgRect);
- background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
- this->addChild(background,5);
CCSize bgRect = CCSizeMake(size.width,size.height/3);
CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
background->setContentSize(bgRect);
background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
this->addChild(background,5);
然后按钮里也需要用到这个素材图,拉伸图片到我们需要的,用到了CCControlButton
- CCLabelTTF *title1 = CCLabelTTF::create("拍照", "Marker Felt", 30);
- CCControlButton* btn_takephoto = CCControlButton::create(title1,CCScale9Sprite::create("dialog_normal.png"));
- /* 设置按钮按下时的图片 */
- btn_takephoto->setBackgroundSpriteForState(CCScale9Sprite::create("dialog_pressed.png"), CCControlStateSelected);
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setPreferredSize(btnRect);
- btn_takephoto->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogPhoto::MenuItemCallback), CCControlEventTouchUpInside);
- background->addChild(btn_takephoto);
CCLabelTTF *title1 = CCLabelTTF::create("拍照", "Marker Felt", 30);
CCControlButton* btn_takephoto = CCControlButton::create(title1,CCScale9Sprite::create("dialog_normal.png"));
/* 设置按钮按下时的图片 */
btn_takephoto->setBackgroundSpriteForState(CCScale9Sprite::create("dialog_pressed.png"), CCControlStateSelected);
btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
btn_takephoto->setPreferredSize(btnRect);
btn_takephoto->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogPhoto::MenuItemCallback), CCControlEventTouchUpInside);
background->addChild(btn_takephoto);
当然也可以用CCMenuItemSprite
- CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
- sp1->setContentSize(btnRect);
- CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
- sp2->setContentSize(btnRect);
- CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1,sp2,this,menu_selector(DialogShare::MenuItemCallback));
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setTag(DialogTakePhoto);
- CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
- pLabel1->setColor(ccc3(0, 0, 0));
- pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_takephoto->addChild(pLabel1);
CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
sp1->setContentSize(btnRect);
CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
sp2->setContentSize(btnRect);
CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1,sp2,this,menu_selector(DialogShare::MenuItemCallback));
btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
btn_takephoto->setTag(DialogTakePhoto);
CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
pLabel1->setColor(ccc3(0, 0, 0));
pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
btn_takephoto->addChild(pLabel1);
- <pre class="cpp" name="code">m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);</pre>
- m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);
m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
m_pMenu->setPosition(CCPointZero);
background->addChild(m_pMenu);
下面就是我们所需的效果
这里用到了对话框的思想,点击按钮之后从底部弹出菜单,下面贴出全部代码
- #pragma once
- #include "cocos2d.h"
- #include "HelloWorldScene.h"
- #include "cocos-ext.h"
- USING_NS_CC_EXT;
- USING_NS_CC;
- class DialogShare: public CCLayerColor
- {
- // 模态对话框菜单
- CCMenu *m_pMenu;
- // 记录菜单点击
- bool m_bTouchedMenu;
- public:
- DialogShare();
- ~DialogShare();
- static cocos2d::CCScene* scene();
- virtualbool init();
- // 初始化对话框内容
- void initDialog();
- CREATE_FUNC(DialogShare);
- void onEnter();
- void onExit();
- virtualbool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
- void MenuItemCallback(CCObject *pSender);
- };
#pragma once #include "cocos2d.h"
#include "HelloWorldScene.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC; class DialogShare: public CCLayerColor
{
// 模态对话框菜单
CCMenu *m_pMenu;
// 记录菜单点击
bool m_bTouchedMenu;
public:
DialogShare();
~DialogShare();
static cocos2d::CCScene* scene();
virtual bool init();
// 初始化对话框内容
void initDialog(); CREATE_FUNC(DialogShare); void onEnter();
void onExit();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); void MenuItemCallback(CCObject *pSender);
};
- #include "DialogShare.h"
- #include "HelloWorldScene.h"
- enum {
- DialogTakePhoto,
- DialogPhotoAlbum,
- DialogCancel,
- };
- CCScene* DialogShare::scene()
- {
- CCScene *scene = CCScene::create();
- DialogShare *layer = DialogShare::create();
- scene->addChild(layer);
- return scene;
- }
- DialogShare::DialogShare()
- {
- }
- DialogShare::~DialogShare()
- {
- }
- bool DialogShare::init()
- {
- bool bRet = false;
- do {
- CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));
- this->initDialog();
- bRet = true;
- } while (0);
- return bRet;
- }
- void DialogShare::initDialog()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSize bgRect = CCSizeMake(size.width,size.height/3);
- CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
- background->setContentSize(bgRect);
- background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
- this->addChild(background,5);
- CCSize btnRect = CCSizeMake(bgRect.width/2,bgRect.height/5);
- CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
- sp1->setContentSize(btnRect);
- CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
- sp2->setContentSize(btnRect);
- CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1
- ,sp2,this,menu_selector(DialogShare::MenuItemCallback));
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setTag(DialogTakePhoto);
- CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
- pLabel1->setColor(ccc3(0, 0, 0));
- pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_takephoto->addChild(pLabel1);
- CCScale9Sprite* sp3 = CCScale9Sprite::create("dialog_normal.png");
- sp3->setContentSize(btnRect);
- CCScale9Sprite* sp4 = CCScale9Sprite::create("dialog_pressed.png");
- sp4->setContentSize(btnRect);
- CCMenuItemSprite* btn_photoalbum = CCMenuItemSprite::create(sp3
- ,sp4,this,menu_selector(DialogShare::MenuItemCallback));
- btn_photoalbum->setPosition(ccp(bgRect.width/2,bgRect.height/5*5/2));
- btn_photoalbum->setTag(DialogPhotoAlbum);
- CCLabelTTF* pLabel2 = CCLabelTTF::create("相册中选取", "Arial", 18);
- pLabel2->setColor(ccc3(0, 0, 0));
- pLabel2->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_photoalbum->addChild(pLabel2);
- CCScale9Sprite* sp5 = CCScale9Sprite::create("dialog_cancel_normal.png");
- sp5->setContentSize(btnRect);
- CCScale9Sprite* sp6 = CCScale9Sprite::create("dialog_pressed.png");
- sp6->setContentSize(btnRect);
- CCMenuItemSprite* btn_cancel = CCMenuItemSprite::create(sp5
- ,sp6,this,menu_selector(DialogShare::MenuItemCallback));
- btn_cancel->setPosition(ccp(bgRect.width/2,bgRect.height/5));
- btn_cancel->setTag(DialogCancel);
- CCLabelTTF* pLabel3 = CCLabelTTF::create("取消", "Arial", 16);
- pLabel3->setColor(ccc3(0, 0, 0));
- pLabel3->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_cancel->addChild(pLabel3);
- m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);
- background->runAction(CCEaseExponentialOut::create(CCMoveTo::create(0.5f,ccp(bgRect.width/2,bgRect.height/2))));
- }
- void DialogShare::onEnter()
- {
- CCLayerColor::onEnter();
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority-1, true);
- }
- void DialogShare::onExit()
- {
- CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
- CCLayerColor::onExit();
- }
- bool DialogShare::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);
- returntrue;
- }
- void DialogShare::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchMoved(pTouch, pEvent);
- }
- }
- void DialogShare::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchEnded(pTouch, pEvent);
- m_bTouchedMenu = false;
- }
- }
- void DialogShare::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchEnded(pTouch, pEvent);
- m_bTouchedMenu = false;
- }
- }
- void DialogShare::MenuItemCallback(cocos2d::CCObject *pSender)
- {
- CCMenuItemImage* button=(CCMenuItemImage*)pSender;
- switch (button->getTag())
- {
- case DialogTakePhoto:
- CCLog("DialogTakePhoto++++++");
- break;
- case DialogPhotoAlbum:
- CCLog("DialogPhotoAlbum++++++");
- break;
- case DialogCancel:
- CCLog("DialogCancel++++++");
- this->removeFromParentAndCleanup(true);
- break;
- }
- }
【转】CCScale9Sprite和CCControlButton的更多相关文章
- 12.解决CCScale9Sprite或者CCControlButton无法使用的问题。
问题: 使用CCScale9Sprite或者CCControlButton等控件的时候,会出现无法识别的情况. 解决方式: 1.include对应的头部,即#include "cocos-e ...
- Cocos2d-x中jsb结构剖析
libs/javascript下有两部分bindings和spidermonkey.其中spidermonkey为js虚拟机,暂时不去管它.bindings下分为四部分,分别为主干部分,generat ...
- 1cocos2dx扩展UI控制,CCControlSlider,CCScale9Sprite(九妹图。),CCControlSwitch,CCControlButton
UI控件来自cocos2dx的扩展库.完好了UI方面的元素,使cocos2dx更加丰富多彩.使用扩展库需包括: #include "cocos-ext.h" USING_NS ...
- cocos2d-x中CCScale9Sprite的另一种实现
cocos2d 2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观 ...
- cocos2dx CCControlButton button大事
=================================.cpp文件 <pre name="code" class="cpp">bool ...
- CCControlExtension/CCControlButton
#ifndef __CCCONTROL_BUTTON_H__ #define __CCCONTROL_BUTTON_H__ #include "CCControl.h" #incl ...
- CCControlSwitch 、CCControlSlider、CCControlButton
/* *bool hasMoved(); 这里获取的不是开关是否正在被用户拨动,而是开关最终的状态是由用户手动拨动开关进行的, *还是用户点击开关进行的状态更改 */ CCControlSwitch* ...
- cocos2dx基础篇(10) 按钮控件CCControlButton
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态 ...
- cocos2d-x CCScale9Sprite
转自:http://www.cocos2dev.com/?p=295 前段时间看CCEditBox的时候,发现里面有个利用9宫格图缩放图片的,也就是缩放带圆角的图片. 这个比较有用处,很多游戏中有很多 ...
随机推荐
- datepicker clone 控件错误
删除id,并删除hasDatepicker //+ - function changeRows(sender,desc){ var tr = $(sender).closest("tr&q ...
- Js参数值中含有单引号或双引号解决办法
<script type="text/javascript"> function Display(LoginEmail, UserName, ID) { ...
- ASP.net在网页上显示当前时间,利用AJAX不刷新网页
前台页面代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...
- Java常用数据结构之Set之TreeSet
前言 上篇文章我们分析了HashSet,它是基于HashMap实现的,那TreeSet会是怎么实现的呢?没错!和大家想的一样,它是基于TreeMap实现的.所以,TreeSet的源码也很简单,主要还是 ...
- 如何用一个for循环打印出一个二维数组
思路分析: 二维数组在内存中默认是按照行存储的,比如一个二维数组{{1,2,3,},{4,5,6}},它在内存中存储的顺序就是1.2.3.4.5.6,也就是说,对于这6个数组元素,按照从0到5给它们编 ...
- JS有趣的单线程
一.JS的执行特点 源于单线程的特性, JS在一段时间内只能执行一部分代码, 那么, 当有多块代码需要执行时, 就需要排队等候了. 二.单线程与异步事件 (1) 什么是异步事件? 异 ...
- 计算 md5
代码从polarssl中扒来的,略作改动,md5.h & md5.cpp 如下 #ifndef POLARSSL_MD5_H #define POLARSSL_MD5_H #include & ...
- Python easyGUI 猜数字
import easygui as g import random d=random.randint(0,10) while 1: g.msgbox("现在开始猜数字小游戏:") ...
- BootStrap Table将时间戳更改为日期格式
一.使用BootStrap Table遇到的问题: 1.MyBatis从数据库中取出的时间格式如下:2017-12-04 21:43:19.0,时间后面多了一个点零. 2.从BootStrap Tab ...
- Ansible 安装和管理服务
ansible 使用 yum 模块来安装软件包,使用 service 模块来启动软件: [root@localhost ~]$ ansible 192.168.119.134 -m yum -a &q ...