Shop.ts 16 KB

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