在cocos2d js android应用程序中设置UI按钮的宽度和高度 [英] Setting UI Button width and height in cocos2d js android application

查看:17
本文介绍了在cocos2d js android应用程序中设置UI按钮的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Cocos2d js android 应用程序中,我有一个 UI 按钮

In my Cocos2d js android app i have a UI button as

    var button=new ccui.Button();
    button.loadTextures(res.play_png,res.play_png);
    button.setAnchorPoint(cc.p(0,0));
    button.x=size.width/2;
    button.y=size.height/2;
    this.addChild(button);

按钮加载,我可以将其设置在屏幕上的任何位置.

The button loads and i am able to set it at any position on the screen.

但我无法将其宽度和高度更改为指定的像素数.我想要类似的东西

But i am unable to change its width and height to specified number of pixels. I want something like

button.width=100;
button.height=100;

我没有找到任何方法来做到这一点...

I did not find any method to do that...

任何有关如何实现这一点的帮助都会很棒.

Any help how to accomplish this would be greatful.

谢谢.

我使用的按钮是播放、如何播放、分享等选项.

The buttons i use are options like play,how to play,share etc..

所以我必须以这样一种方式放置它们,使它们不会因各种屏幕分辨率而重叠或扭曲...

So i have to position them in such a way that they dont overlap or get distorted for various screen resolutions...

如何在这种情况下使用 setScale 方法???

How can i use the setScale method in this context???

推荐答案

我们可以调用一个函数,该函数可以通过执行一个动作来缩放 UIButton 的大小.在这种情况下,以下代码可能会有所帮助:

We can make a call to a function that can scale the size of the UIButton by performing an action. In this case, the following code might help:

 performScaleAction : function() 
 {
    var duration = 0;   // duration in seconds for performing this action
    var scaleX = 0.5;   // scale factor for X-axis
    var scaleY = 0.5;   // scale factor for Y-axis
    var scaleAction = new cc.ScaleTo(duration , scaleX , scaleY );
    this.runAction(scaleAction);
 }

现在,通过以下方式调用上述函数:

Now, call the above function in the following way:

this.performScaleAction();
this.addChild(button);

因此,如果用于创建 UIButton 的图像的实际高度为 h,在这种情况下,屏幕上显示的新大小将为 h 乘以 scaleX .UIButton 的宽度相同.
希望这会有所帮助!

So, if the actual height of the image used for creating the UIButton is h, in this case, the new size as displayed on the screen would be h times scaleX . Same for the width of the UIButton.
Hope this helps!

这篇关于在cocos2d js android应用程序中设置UI按钮的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆