cocos js 怎样做出按钮选中效果

cocos js 怎样做出按钮选中效果,第1张

cocos js 做出按钮选中效果示例:

一,首先使用cocos新建一个Cocos2d-js的新项目,然后再cocostudio中创建一个场景,在场景中添加三个按钮分别设置三态的图片

二,打开编辑器,实现代码如下:

var HelloWorldLayer = cc.Layer.extend({

ctor:function () {

this._super()

//导入cocostudio中拼好的界面

mainscene = ccs.load(res.MainScene_json).node

this.addChild(mainscene)

this.teamButton = ccui.helper.seekWidgetByName(mainscene,"Button_0")

var btn2 = ccui.helper.seekWidgetByName(mainscene,"Button_1")

var btn3 = ccui.helper.seekWidgetByName(mainscene,"Button_2")

//先默认设置一个按钮为选中状态   this.teamButton.setBrightStyle(ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT)

this.teamButton.setEnabled(false)

var teamInfo = this.teamButton

this.teamButton.addTouchEventListener(this.selectedBtn1,this)

btn2.addTouchEventListener(this.selectedBtn2,this)

btn3.addTouchEventListener(this.selectedBtn3,this)

return true

},

selectedBtn1: function (sender, type) {

if(type == ccui.Widget.TOUCH_ENDED){

this.callBack(sender)

cc.log("==========商店界面")

}

},

selectedBtn2: function (sender, type) {

if(type == ccui.Widget.TOUCH_ENDED){

this.callBack(sender)

cc.log("==========卡牌界面")

}

},

selectedBtn3: function (sender, type) {

if(type == ccui.Widget.TOUCH_ENDED){

this.callBack(sender)

cc.log("==========战斗界面")

}

},

callBack: function (sender) {

if (this.teamButton == sender){

return

}else{

this.teamButton.setBrightStyle(ccui.Widget.BRIGHT_STYLE_NORMAL)

this.teamButton.setEnabled(true)

sender.setBrightStyle(ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT)

sender.setEnabled(false)

this.teamButton = sender

}

},

})

var HelloWorldScene = cc.Scene.extend({

onEnter:function () {

this._super()

var layer = new HelloWorldLayer()

this.addChild(layer)

}

})

三,运行就可以查看界面,点击不同的按钮显示不同的输出结果

[Log] ==========商店界面 (CCDebugger.js, line 331)

[Log] ==========卡牌界面 (CCDebugger.js, line 331)

[Log] ==========战斗界面 (CCDebugger.js, line 331)

哎呀,你随便百度一下就有的,标题都一模一样。。。我下面的是直接复制的哈

方法1:

给图片加一个onclick事件,用js事件来提交就可以实现。

例:

<input type="image" src="图片地址" />

方法2:

有CSS把图片设为按钮背景就可以了

例: <input name="" type="button"  style=" width:10px height:10px  border:0 background:url(图片路径) no-repeat left top" />

提示:需要把按钮的宽高设的和图片一样。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11771963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存