Shop.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName, { PopType } from "./EventName/EventName";
  8. import LocalData from "./LocalData";
  9. import MyComponent from "./Template/MyComponent";
  10. const { ccclass, property } = cc._decorator;
  11. type CharacterSkinType = {
  12. name: string,
  13. introduce: string,
  14. image: string,
  15. imageSp: cc.SpriteFrame,
  16. price: number,
  17. id: number,
  18. }
  19. type SkinType = {
  20. CharacterSkin: CharacterSkinType[],
  21. ParabolaSkin: CharacterSkinType[],
  22. }
  23. @ccclass
  24. export default class Shop extends MyComponent {
  25. @property(cc.Node)
  26. content: cc.Node = null;
  27. @property(cc.Node)
  28. CharacterSkinPrefab: cc.Node = null;
  29. @property(cc.Label)
  30. ShowCharacterName: cc.Label = null;
  31. @property(cc.Label)
  32. ShowCharacterintroduce: cc.Label = null;
  33. @property(cc.Sprite)
  34. ShowCharacterSprite: cc.Sprite = null;
  35. @property(cc.Node)
  36. ParabolaSkin: cc.Node = null;
  37. @property(cc.Node)
  38. CharacterSkin: cc.Node = null;
  39. @property(cc.Node)
  40. Select: cc.Node = null;
  41. @property(cc.Label)
  42. Gold: cc.Label = null;
  43. SelectCharacterSkinType: CharacterSkinType = null
  44. SelectParabolaSkinType: CharacterSkinType = null
  45. Click_CharacterSkin: Boolean = true
  46. onLoad(): void {
  47. super.onLoad()
  48. this.regEvent(EventName.changeGold, this.UpdataGold, this)
  49. cc.resources.load("json/Skin", cc.JsonAsset, this.LoadDone.bind(this));
  50. }
  51. start() {
  52. this.scheduleOnce(() => {
  53. this.SetScrollViewSize()
  54. this.UpdataGold()
  55. }, 0)
  56. }
  57. jsonData: SkinType = null
  58. LoadDone(error: Error, resource: cc.JsonAsset) {
  59. if (error) {
  60. console.log(error.name);
  61. console.log(error.message);
  62. return;
  63. }
  64. this.jsonData = resource.json;
  65. // console.log(jsonData);
  66. this.clickCharacterSkin()
  67. this.initSetScrollView()
  68. }
  69. initSetScrollView() {
  70. let jsonData = this.jsonData
  71. // console.log(jsonData);
  72. if (this.Click_CharacterSkin) {
  73. for (let index = 0; index < jsonData.CharacterSkin.length; index++) {
  74. let element = jsonData.CharacterSkin[index];
  75. let temp_node = cc.instantiate(this.CharacterSkinPrefab)
  76. this.content.addChild(temp_node)
  77. let Skin = temp_node.getChildByName("Skin")
  78. //加入发光节点
  79. this.LightNodes.push(temp_node.getChildByName("checkbox"))
  80. //是否拥有了这个皮肤
  81. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  82. if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
  83. let has = temp_node.getChildByName("has")
  84. temp_node.getComponent(cc.Sprite).spriteFrame =
  85. has.getComponent(cc.Sprite).spriteFrame
  86. if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
  87. NodeLabel.string = '使用中'
  88. //设置当前选择的皮肤类型
  89. this.SelectCharacterSkinType = element
  90. } else {
  91. NodeLabel.string = '选择'
  92. }
  93. } else {
  94. NodeLabel.node.active = false
  95. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  96. Gold.active = true
  97. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  98. Label.string = element.price.toString()
  99. }
  100. //加载图
  101. const url = element.image
  102. cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
  103. if (!err) {
  104. element.imageSp = spriteframe
  105. Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
  106. temp_node['CharacterSkin'] = element
  107. }
  108. });
  109. //添加点击事件
  110. temp_node.on('touchstart', () => {
  111. let temp = temp_node['CharacterSkin'] as CharacterSkinType
  112. this.ShowCharacterSprite.spriteFrame = temp.imageSp
  113. this.ShowCharacterintroduce.string = temp.introduce
  114. this.ShowCharacterName.string = temp.name
  115. this.setLight(temp_node.getChildByName("checkbox"))
  116. //设置当前选择的皮肤类型
  117. this.SelectCharacterSkinType = temp
  118. })
  119. }
  120. } else {
  121. for (let index = 0; index < jsonData.ParabolaSkin.length; index++) {
  122. let element = jsonData.ParabolaSkin[index];
  123. let temp_node = cc.instantiate(this.CharacterSkinPrefab)
  124. this.content.addChild(temp_node)
  125. let Skin = temp_node.getChildByName("Skin")
  126. //加入发光节点
  127. this.LightNodes.push(temp_node.getChildByName("checkbox"))
  128. //是否拥有了这个皮肤
  129. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  130. if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
  131. let has = temp_node.getChildByName("has")
  132. temp_node.getComponent(cc.Sprite).spriteFrame =
  133. has.getComponent(cc.Sprite).spriteFrame
  134. if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
  135. NodeLabel.string = '使用中'
  136. //设置当前选择的皮肤类型
  137. this.SelectCharacterSkinType = element
  138. } else {
  139. NodeLabel.string = '选择'
  140. }
  141. } else {
  142. NodeLabel.node.active = false
  143. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  144. Gold.active = true
  145. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  146. Label.string = element.price.toString()
  147. }
  148. //加载图
  149. const url = element.image
  150. cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
  151. if (!err) {
  152. element.imageSp = spriteframe
  153. Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
  154. temp_node['ParabolaSkin'] = element
  155. }
  156. });
  157. //添加点击事件
  158. temp_node.on('touchstart', () => {
  159. let temp = temp_node['ParabolaSkin'] as CharacterSkinType
  160. this.ShowCharacterSprite.spriteFrame = temp.imageSp
  161. this.ShowCharacterintroduce.string = temp.introduce
  162. this.ShowCharacterName.string = temp.name
  163. this.setLight(temp_node.getChildByName("checkbox"))
  164. //设置当前选择的皮肤类型
  165. this.SelectParabolaSkinType = temp
  166. })
  167. }
  168. }
  169. }
  170. updataSetScrollView() {
  171. if (this.Click_CharacterSkin) {
  172. for (let index = 0; index < this.content.children.length; index++) {
  173. const temp_node = this.content.children[index];
  174. let element = this.jsonData.CharacterSkin[index];
  175. //是否拥有了这个皮肤
  176. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  177. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  178. if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
  179. Gold.active = false
  180. NodeLabel.node.active = true
  181. let has = temp_node.getChildByName("has")
  182. temp_node.getComponent(cc.Sprite).spriteFrame =
  183. has.getComponent(cc.Sprite).spriteFrame
  184. if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
  185. NodeLabel.string = '使用中'
  186. //设置当前选择的皮肤类型
  187. this.SelectCharacterSkinType = element
  188. } else {
  189. NodeLabel.string = '选择'
  190. }
  191. } else {
  192. NodeLabel.node.active = false
  193. Gold.active = true
  194. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  195. Label.string = element.price.toString()
  196. }
  197. }
  198. } else {
  199. for (let index = 0; index < this.content.children.length; index++) {
  200. const temp_node = this.content.children[index];
  201. let element = this.jsonData.ParabolaSkin[index];
  202. //是否拥有了这个皮肤
  203. let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
  204. let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
  205. if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
  206. Gold.active = false
  207. NodeLabel.node.active = true
  208. let has = temp_node.getChildByName("has")
  209. temp_node.getComponent(cc.Sprite).spriteFrame =
  210. has.getComponent(cc.Sprite).spriteFrame
  211. if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
  212. NodeLabel.string = '使用中'
  213. //设置当前选择的皮肤类型
  214. this.SelectParabolaSkinType = element
  215. } else {
  216. NodeLabel.string = '选择'
  217. }
  218. } else {
  219. NodeLabel.node.active = false
  220. Gold.active = true
  221. let Label = Gold.getChildByName("Label").getComponent(cc.Label)
  222. Label.string = element.price.toString()
  223. }
  224. }
  225. }
  226. }
  227. SetScrollViewSize() {
  228. let content_width = this.content.getContentSize().width
  229. let spacingX = (content_width - 4 * this.CharacterSkinPrefab.getContentSize().width) / 5
  230. this.content.getComponent(cc.Layout).spacingX = spacingX
  231. this.content.getComponent(cc.Layout).paddingLeft = spacingX
  232. }
  233. clickParabolaSkin() {
  234. this.ParabolaSkin.color = new cc.Color(200, 200, 200, 255);
  235. this.CharacterSkin.color = new cc.Color(255, 255, 255, 255);
  236. this.Select.setPosition(this.ParabolaSkin.position)
  237. this.Click_CharacterSkin = false
  238. this.content.children.forEach(e => e.destroy())
  239. this.initSetScrollView()
  240. }
  241. clickCharacterSkin() {
  242. this.ParabolaSkin.color = new cc.Color(255, 255, 255, 255);
  243. this.CharacterSkin.color = new cc.Color(200, 200, 200, 255);
  244. this.Select.setPosition(this.CharacterSkin.position)
  245. this.Click_CharacterSkin = true
  246. this.content.children.forEach(e => e.destroy())
  247. this.initSetScrollView()
  248. }
  249. clickExit() {
  250. this.node.destroy()
  251. }
  252. //发光节点数组
  253. LightNodes: cc.Node[] = []
  254. setLight(selectNode: cc.Node) {
  255. this.LightNodes.forEach(e => {
  256. if (e == selectNode) {
  257. e.active = true
  258. } else {
  259. e.active = false
  260. }
  261. })
  262. }
  263. // update (dt) {}
  264. UpdataGold() {
  265. this.Gold.string = LocalData.getInstance().getGold().toString()
  266. }
  267. UseSkin() {
  268. if (this.Click_CharacterSkin == false) {
  269. //是否拥有这个皮肤
  270. if (LocalData.getInstance().getHasParabolaSkin().includes(this.SelectParabolaSkinType.id)) {
  271. //拥有
  272. LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
  273. //更新展示
  274. this.updataSetScrollView()
  275. } else {
  276. //没拥有
  277. if (LocalData.getInstance().getGold() >= this.SelectParabolaSkinType.price) {
  278. //购买
  279. LocalData.getInstance().setGold(this.SelectParabolaSkinType.price, '-')
  280. //拥有
  281. LocalData.getInstance().setHasParabolaSkin(this.SelectParabolaSkinType.id)
  282. //当前用的
  283. LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
  284. //更新展示
  285. this.updataSetScrollView()
  286. } else {
  287. //提示看广告拿金币
  288. let temp: PopType = new PopType()
  289. temp.string = '看广告拿金币'
  290. temp.Title = '提示'
  291. temp.OK = () => {
  292. this.OpenAD()
  293. }
  294. temp.Fail = () => {
  295. }
  296. this.Pop(temp)
  297. }
  298. }
  299. } else {
  300. //是否拥有这个皮肤
  301. if (LocalData.getInstance().getHasCharacterSkin().includes(this.SelectCharacterSkinType.id)) {
  302. //拥有
  303. LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
  304. //更新展示
  305. this.updataSetScrollView()
  306. } else {
  307. //没拥有
  308. if (LocalData.getInstance().getGold() >= this.SelectCharacterSkinType.price) {
  309. //购买
  310. LocalData.getInstance().setGold(this.SelectCharacterSkinType.price, '-')
  311. //拥有
  312. LocalData.getInstance().setHasCharacterSkin(this.SelectCharacterSkinType.id)
  313. //当前用的
  314. LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
  315. //更新展示
  316. this.updataSetScrollView()
  317. } else {
  318. //提示看广告拿金币
  319. let temp: PopType = new PopType()
  320. temp.string = '看广告拿金币'
  321. temp.Title = '提示'
  322. temp.OK = () => {
  323. this.OpenAD()
  324. }
  325. temp.Fail = () => {
  326. }
  327. this.Pop(temp)
  328. }
  329. }
  330. }
  331. }
  332. //看广告
  333. OpenAD() {
  334. LocalData.getInstance().setGold(1100, '+')
  335. }
  336. }