o0CCButton.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var o0 = require('o0');
  2. var o0CC = require('o0CC');
  3. var o0Game = require('o0Game');
  4. cc.Class({
  5. extends: cc.Sprite,
  6. properties: {
  7. //background:null,
  8. button:null,
  9. sprite:null,
  10. label:null,
  11. o0ClickEvent:[],
  12. },
  13. o0OnClick:function(){
  14. for(var i=0;i<this.o0ClickEvent.length;++i){
  15. this.o0ClickEvent[i]();
  16. }
  17. },
  18. // use this for initialization
  19. onLoad: function () {
  20. this.sprite = this;
  21. var self = this;
  22. cc.loader.loadRes('button', cc.SpriteFrame, function (err, spriteFrame) {
  23. self.sprite.spriteFrame = spriteFrame;
  24. });/** */
  25. this.sprite.trim = true;
  26. this.sprite.type = cc.Sprite.Type.SLICED;
  27. this.sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  28. this.sprite.SrcBlendFactor = cc.macro.BlendFactor.SRC_ALPHA;
  29. this.sprite.DstBlendFactor = cc.macro.BlendFactor.ONE_MINUS_SRC_ALPHA;//精灵必须在button之前
  30. this.button = this.node.addComponent('cc.Button');
  31. //this.button = this;
  32. this.button.target = this.node;
  33. this.button.interactable = true;
  34. this.button.duration = 0.1;
  35. this.button.transition = cc.Button.Transition.COLOR;
  36. this.button.normalColor = new cc.Color(200,200,200,255);
  37. this.button.pressedColor = new cc.Color(100,100,100,255);
  38. this.button.hoverColor = new cc.Color(150,150,150,255);
  39. this.button.disabledColor = new cc.Color(50,50,50,255);/** */
  40. var o0OnClickHandler = new cc.Component.EventHandler();
  41. o0OnClickHandler.target = this.node;
  42. o0OnClickHandler.component = "o0CCButton";
  43. o0OnClickHandler.handler = "o0OnClick";
  44. o0OnClickHandler.emit([]);
  45. this.button.clickEvents.push(o0OnClickHandler);
  46. this.label = o0CC.addScriptNode(this.node,'cc.Label',0);
  47. this.label.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
  48. this.label.verticalAlign = cc.Label.VerticalAlign.CENTER;
  49. //this.label.fontSize = 16;
  50. //this.label.lineHeight = 20;
  51. this.label.overflow = cc.Label.Overflow.SHRINK;
  52. this.label.node.color = new cc.Color(0,0,0);
  53. this.label.enableWrapText = false;/** */
  54. },
  55. // called every frame
  56. update: function (dt) {
  57. this.label.node.anchorX = this.node.anchorX;
  58. this.label.node.anchorY = this.node.anchorY;
  59. this.label.node.x = 0;
  60. this.label.node.y = 0;
  61. this.label.node.width = this.node.width-5;
  62. this.label.node.height = this.node.height;
  63. this.label.fontSize = this.node.height - 0;
  64. this.label.string = this.name;/** */
  65. }
  66. })