位置: 编程技术 - 正文

cocos2dx3.6 弹出对话框的实现(cocos2djs)

编辑:rootadmin

推荐整理分享cocos2dx3.6 弹出对话框的实现(cocos2djs),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:cocos2djs,cocos2dx schedule,cocos2dx接入广告,cocos loading,cocos2djs,cocos弹窗,cocos弹窗,cocos2dx接入广告,内容如对您有帮助,希望把文章链接给更多的朋友!

头文件:

//

// PopAlertDialog.h

// macstudycocos2dx

//

// Created by WangWei on /6/8.

//

//

#ifndef __macstudycocos2dx__PopAlertDialog__

#define __macstudycocos2dx__PopAlertDialog__

#pragma once

#include "cocos2d.h"

#include "cocos-ext.h"

USING_NS_CC;

USING_NS_CC_EXT;

class PopAlertDialog:public LayerColor{ //继承LayerColor类,方便更改layer的颜色和透明度

public:

PopAlertDialog();

~PopAlertDialog();

virtual bool init();

CREATE_FUNC(PopAlertDialog);

static PopAlertDialog* create(constchar* backgroudImage,Size dialogSize);

bool onTouchBegan(Touch* touch,Event* event);

void onTouchMoved(Touch* touch,Event* event);

void onTouchEnded(Touch* touch,Event* event);

void setTitle(constchar* title,int fontsize=);

void setContentText(constchar* text,int fontsize=,int padding=,int paddingTop=);

void setCallBackFunc(Ref* target,SEL_CallFuncN callfun);

bool addButton(constchar* normalImage,constchar* selectedImage,constchar* title,int tag=0);

virtual void onEnter();

virtual void onExit();

void backgroundFinish();

private:

void buttonCallBack(Ref* pSender);

int m_contentPadding;

int m_contentPaddingTop;

Size m_dialogContentSize; //对话框大小

Ref* m_callbackListener;

SEL_CallFuncN m_callback;

CC_SYNTHESIZE_RETAIN(Menu*,m__pMenu, MenuButton);

CC_SYNTHESIZE_RETAIN(Sprite*,m__sfBackGround, SpriteBackGround);

CC_SYNTHESIZE_RETAIN(Scale9Sprite*,m__s9BackGround, Sprite9BackGround);

CC_SYNTHESIZE_RETAIN(LabelTTF*,m__ltTitle, LabelTitle);

CC_SYNTHESIZE_RETAIN(LabelTTF*,m__ltContentText, LabelContentText);

};

#endif /* defined(__macstudycocos2dx__PopAlertDialog__) */

cpp文件:

//

// PopAlertDialog.cpp

// macstudycocos2dx

//

// Created by WangWei on /6/8.

//

//

#include "PopAlertDialog.h"

PopAlertDialog::PopAlertDialog()

:

m__pMenu(NULL)

,m_contentPadding(0)

,m_contentPaddingTop(0)

,m_callbackListener(NULL)

,m_callback(NULL)

,m__sfBackGround(NULL)

,m__s9BackGround(NULL)

,m__ltContentText(NULL)

,m__ltTitle(NULL)

{

}

PopAlertDialog::~PopAlertDialog(){

CC_SAFE_RELEASE(m__pMenu);

CC_SAFE_RELEASE(m__sfBackGround);

CC_SAFE_RELEASE(m__s9BackGround);

CC_SAFE_RELEASE(m__ltContentText);

CC_SAFE_RELEASE(m__ltTitle);

}

bool PopAlertDialog::init(){

if (!CCLayerColor::init()) {

return false;

}

Menu* menu=Menu::create();

menu->setPosition(CCPointZero);

setMenuButton(menu);

auto listener=EventListenerTouchOneByOne::create();

listener->setSwallowTouches(true);

listener->onTouchBegan=CC_CALLBACK_2(PopAlertDialog::onTouchBegan,this);

listener->onTouchMoved=CC_CALLBACK_2(PopAlertDialog::onTouchMoved,this);

listener->onTouchEnded=CC_CALLBACK_2(PopAlertDialog::onTouchEnded,this);

auto dispatcher=Director::getInstance()->getEventDispatcher();

dispatcher->addEventListenerWithSceneGraphPriority(listener,this);

//设置弹出层的颜色,指定为淡灰色

setColor(Color3B::GRAY);

setOpacity();

return true;

}

bool PopAlertDialog::onTouchBegan(Touch* touch,Event* event){

return true;

}

void PopAlertDialog::onTouchMoved(Touch* touch,Event* event){

}

void PopAlertDialog::onTouchEnded(Touch* touch,Event* event){

}

PopAlertDialog* PopAlertDialog::create(constchar* backgoundImage,Size dialogSize){

//创建弹出对话框,指定背景图和大小。

PopAlertDialog* layer=PopAlertDialog::create();

cocos2dx3.6  弹出对话框的实现(cocos2djs)

//layer->setSpriteBackGround(Sprite::create(backgoundImage));

layer->setSprite9BackGround(Scale9Sprite::create(backgoundImage));

layer->m_dialogContentSize=dialogSize;

return layer;

}

void PopAlertDialog::setTitle(constchar* title,int fontsize/*=*/){

LabelTTF* label=LabelTTF::create(title,"", fontsize);

label->setColor(Color3B::RED);

setLabelTitle(label);

}

void PopAlertDialog::setContentText(constchar* text,int fontsize,int padding,int paddingTop){

LabelTTF* ltf=LabelTTF::create(text,"", fontsize);

ltf->setColor(Color3B::BLUE);

setLabelContentText(ltf);

m_contentPadding=padding;

m_contentPaddingTop=paddingTop;

}

void PopAlertDialog::setCallBackFunc(Ref*target, SEL_CallFuncN callfun){

m_callbackListener=target;

m_callback=callfun;

}

bool PopAlertDialog::addButton(constchar *normalImage, const char *selectedImage,constchar* title,int tag){

Size winSize=Director::getInstance()->getWinSize();

Point center_point=Point(winSize.width/2,winSize.height/2);

auto menuImage=MenuItemImage::create(

normalImage,

selectedImage,

CC_CALLBACK_1(PopAlertDialog::buttonCallBack,this) );

menuImage->setTag(tag);

menuImage->setPosition(center_point);

Size menuSize=menuImage->getContentSize();

Label* labelttf=Label::createWithTTF(title,"fonts/arial.ttf", );

labelttf->setColor(Color3B(Color3B::BLACK));

labelttf->setPosition(Point(menuSize.width/2,menuSize.height/2));

menuImage->addChild(labelttf);

getMenuButton()->addChild(menuImage);

return true;

}

void PopAlertDialog::buttonCallBack(Ref* pSender){

Node* node=dynamic_cast<Node*>(pSender);

//log("[========PopAlertDialog:buttonCallBack=======]touch tag:%d",node->getTag());

if (m_callback&&m_callbackListener) {

(m_callbackListener->*m_callback)(node);

}

this->removeFromParentAndCleanup(true);

}

void PopAlertDialog::onEnter(){

//log("PopAlertDialog::onEnter is loading");

LayerColor::onEnter();

Size winSize=Director::getInstance()->getWinSize();

Point center=Point(winSize.width/2,winSize.height/2);

//Sprite* background=getSpriteBackGround();

Scale9Sprite* background=getSprite9BackGround();

background->setContentSize(m_dialogContentSize);//指定对话框大小

background->setPosition(Point(winSize.width/2,winSize.height/2));

this->addChild(background,0,0);

Action* popupActions=Sequence::create( ScaleTo::create(0.0,0.0),

ScaleTo::create(0.,1.),

ScaleTo::create(0.,0.),

ScaleTo::create(0.,1.0),

CallFunc::create(CC_CALLBACK_0(PopAlertDialog::backgroundFinish,this))

,NULL);

background->runAction(popupActions);

}

void PopAlertDialog::backgroundFinish(){

Size winSize=Director::getInstance()->getWinSize();

Point pCenter=Point(winSize.width/2,winSize.height/2);

this->addChild(getMenuButton());

float btnWidth=m_dialogContentSize.width/(getMenuButton()->getChildrenCount()&#;1);

Vector<Node*> vector=getMenuButton()->getChildren();

Ref* pObj=NULL;

int i=0;

for (Node*pObj:vector){

Node* node=dynamic_cast<Node*>(pObj);

node->setPosition(Point(winSize.width/2-m_dialogContentSize.width/2&#;btnWidth*(i&#;1),winSize.height/2-m_dialogContentSize.height/3));

i&#;&#;;

}

if (getLabelTitle()) {

getLabelTitle()->setPosition(ccpAdd(pCenter, ccp(0,m_dialogContentSize.height/2-.0f)));

this->addChild(getLabelTitle());

}

if (getLabelContentText()) {

CCLabelTTF* ltf=getLabelContentText();

ltf->setPosition(ccp(winSize.width/2,winSize.height/2));

ltf->setDimensions(CCSizeMake(m_dialogContentSize.width-m_contentPadding*2, m_dialogContentSize.height-m_contentPaddingTop));

ltf->setHorizontalAlignment(kCCTextAlignmentLeft);

this->addChild(ltf);

}

}

void PopAlertDialog::onExit(){

log("PopAlertDialog onExit");

CCLayerColor::onExit();

}

调用:

MenuItemSprite* item1=MenuItemSprite::create(btn_normal_sprite, btn_select_sprite,nullptr,CC_CALLBACK_1(MenuItemSpritTest::select_learn,this));

select_learn方法:

voidMenuItemSpritTest::select_learn(Object* pSender){

log("you had select this button");

PopAlertDialog* popup=PopAlertDialog::create("background.png",Size(,));

popup->setTitle("Message");

popup->setContentText("This is a test message!",,,);

popup->setCallBackFunc(this,callfuncN_selector(MenuItemSpritTest::popButtonCallback));

popup->addButton("btnreturn.png","btnreturn.png", "OK",0);

popup->addButton("btnreturn.png","btnreturn.png", "Cancel",1);

this->addChild(popup);

}

void MenuItemSpritTest::popButtonCallback(Node* pNode){

log("[==========]button call back.tag %d",pNode->getTag());

if (pNode->getTag()==0) {

Director::getInstance()->end();

}

if (pNode->getTag()==1) {

//pNode->getParent()->removeFromParent();

pNode->removeFromParent();

}

}

效果图:

设置glblendfunc出现黑框 发现用粒子编辑器设置粒子效果的混合模式,放到程序里会出现偏差,比如黑框等。搜了下,发现原因是:DestBlendFunc只接受以下8个参数:GL_ZEROGL_ONEGL_SR

cocos2dx多个精灵调用runAction()方法执行组合动作,只有最后一个精灵有效的问题 问题代码如下//将MoveTo和RotateTo两个动作转换成FiniteTimeAction类型FiniteTimeAction*MVto=(FiniteTimeAction*)(MoveTo::create(2.0,Vec2(,)));FiniteTimeAction*ROto=(FiniteTimeAction*

.6.开始写博客记录cocos2dx学习历程 今天起,写博客记录自己的cocos2dx学习过程。早在4.就到手的macbookpro,一直没抽出太多的时间去学习,拖拉两个月,才初步学会了搭建环境、创建工程

标签: cocos2djs

本文链接地址:https://www.jiuchutong.com/biancheng/368366.html 转载请保留说明!

上一篇:在cocos2dx中实现水波滤镜(cocos2dx schedule)

下一篇:cocos2dx多个精灵调用runAction()方法执行组合动作,只有最后一个精灵有效的问题(cocos2d-x教程)

  • 劳务外包合同需要交税吗
  • 金税四期怎么监控应收账款
  • 印花税的计税依据为各种应税凭证上所记载的计税金额
  • 开发票要多交费正常吗?
  • 银行取钱需要签协议吗
  • 不动产在建工程使用的外购物资可以抵扣吗
  • 建筑企业收到材料发票没有备注能用吗
  • 投资性房地产租赁期开始日
  • 购入办公楼的入账价值
  • 跨年度取得增值税发票能否抵扣?
  • 特定业务计算的应纳税所得额是什么意思
  • 企业盈利后又亏损怎么算
  • 代开的专票作废了怎么做账?
  • 债务重组损失金额
  • 关于固定资产管理
  • 工会是否具有有法律效力
  • 单位给员工发工资要交税吗
  • 土地增值税的纳税人是在我国境内
  • 典当行的账务处理会计分录大全
  • 出口退税免退税办法
  • 施工企业代甲方购材料的账务处理?
  • 其他权益工具投资公允价值变动怎么计算
  • 内存坏了电脑会黑屏吗
  • 苹果电脑macbookpro使用教程
  • 取得专用发票是进项还是销项
  • 企业对外股权投资涉及税收
  • 带息票据贴现如何计算
  • 工程施工质量罚款措施
  • 财务会计制度
  • windows11测试版升级正式版
  • 在win7中怎么安转只兼容win10的软件
  • qqexternal.exe是什么进程如何删除(CPU的使用率在90%)
  • 单目标优化函数
  • php从服务器下载文件
  • 递延所得税资产和负债怎么理解
  • nerf 怎么瞄准
  • 临时存款账户怎么开户
  • javascript动画教程
  • js fetch api
  • 前端跨域解决方案设计
  • 最多显示2行文字怎么弄
  • ssl查询网站
  • 出口退税率和进项税额
  • 本年利润在借方代表亏损吗
  • 网络编程知识
  • 二手车交易账务处理
  • 存货的成本包括哪几项
  • 供热管道属于什么结构类型
  • 酒店客房成本计算公式
  • 进项发票的作用
  • 企业培训费怎么入账
  • 佣金支出和手续费一样吗
  • 工人晚上加班的说说心情
  • 外国专家经费管理办法
  • 教育协会颁发的证书有用吗?
  • 金税盘服务费可以退吗
  • 支付的管理费用调到其他应付款怎么做账
  • 一般纳税人购买固定资产进项税可以抵扣吗
  • 发票专用章需要备案吗?
  • sql语句多行合并成一行
  • Linux(Ubuntu)下Mysql5.6.28安装配置方法图文教程
  • xp电脑启动项怎么设置启动项
  • 新移动硬盘安装视频教程
  • 如何设置个性化桌面
  • Win10 Mobile RS2预览版14951升级遭遇卡在0%的解决办法
  • mac系统如何安装双系统
  • 苹果电脑打不开txt
  • 如何把鼠标指针换成笔的形状
  • rapimgr.exe - rapimgr进程是什么文件.有哪些作用
  • w10系统音频怎么设置
  • 电脑操作系统win8
  • win7重新安装windows
  • opengl源码在哪里
  • unityc#脚本
  • html应用css
  • javascript和html区别
  • 广东电子税务系统社保1号都能申报吗
  • 农村摩托车上牌要什么证件和手续
  • 江苏退休核定表最低缴费系数是什么意思
  • 五证合一后工商变更完还要税务变更吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设