Setting UI Button width and height in cocos2d js android application

不问归期 提交于 2019-12-02 10:23:59

问题


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.

Thanks.

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...

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


回答1:


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);

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!



来源:https://stackoverflow.com/questions/29193210/setting-ui-button-width-and-height-in-cocos2d-js-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!