Item.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // const i18n = require('i18n');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. backSprite: cc.Node,
  6. rankLabel: cc.Label,
  7. avatarImgSprite: cc.Sprite,
  8. nickLabel: cc.Label,
  9. topScoreLabel: cc.Label,
  10. outsideSprite:cc.Sprite
  11. },
  12. onLoad: function () {
  13. },
  14. updateItem: function(data) {
  15. let avatarUrl = data.head;
  16. let nick = data.nick.length <= 10 ? data.nick : data.nick.substr(0, 10) + "...";
  17. let grade = data.score;
  18. let rank = data.rank;
  19. this.backSprite.active = true;
  20. this.rankLabel.string = rank;
  21. this.createImage(avatarUrl);
  22. this.nickLabel.string = nick;
  23. this.topScoreLabel.string = grade;
  24. this.outsideSprite.active = false;
  25. },
  26. outSide(){
  27. this.outsideSprite.active = true;
  28. },
  29. createImage(avatarUrl) {
  30. var index = avatarUrl.indexOf('https:');
  31. if(index == 0){
  32. //以'https:'开头
  33. }else{
  34. avatarUrl = 'https:'+avatarUrl;
  35. }
  36. if (window.wx != undefined) {
  37. try {
  38. let image = wx.createImage();
  39. image.onload = () => {
  40. try {
  41. let texture = new cc.Texture2D();
  42. texture.initWithElement(image);
  43. texture.handleLoadedTexture();
  44. this.avatarImgSprite.spriteFrame = new cc.SpriteFrame(texture);
  45. } catch (e) {
  46. cc.log(e);
  47. this.avatarImgSprite.node.active = false;
  48. }
  49. };
  50. image.src = avatarUrl;
  51. }catch (e) {
  52. cc.log(e);
  53. this.avatarImgSprite.node.active = false;
  54. }
  55. } else {
  56. cc.loader.load({
  57. url: avatarUrl, type: 'jpg'
  58. }, (err, texture) => {
  59. this.avatarImgSprite.spriteFrame = new cc.SpriteFrame(texture);
  60. });
  61. }
  62. }
  63. });