123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- // Learn TypeScript:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
- import EventName, { EventLabel, PopType } from "./EventName/EventName";
- import Hall from "./Hall";
- import LocalData from "./LocalData";
- import MyComponent from "./Template/MyComponent";
- const { ccclass, property } = cc._decorator;
- type CharacterSkinType = {
- name: string,
- introduce: string,
- image: string,
- imageMain: string,
- imageSp: cc.SpriteFrame,
- imageSpMain: cc.SpriteFrame,
- price: number,
- id: number,
- }
- type SkinType = {
- CharacterSkin: CharacterSkinType[],
- ParabolaSkin: CharacterSkinType[],
- }
- @ccclass
- export default class Shop extends MyComponent {
- @property(cc.Node)
- content: cc.Node = null;
- @property(cc.Node)
- CharacterSkinPrefab: cc.Node = null;
- @property(cc.Label)
- ShowCharacterName: cc.Label = null;
- @property(cc.Label)
- ShowCharacterintroduce: cc.Label = null;
- @property(cc.Sprite)
- ShowCharacterSprite: cc.Sprite = null;
- @property(cc.Node)
- ParabolaSkin: cc.Node = null;
- @property(cc.Node)
- CharacterSkin: cc.Node = null;
- @property(cc.Node)
- Select: cc.Node = null;
- @property(cc.Label)
- Gold: cc.Label = null;
- SelectCharacterSkinType: CharacterSkinType = null
- SelectParabolaSkinType: CharacterSkinType = null
- Click_CharacterSkin: Boolean = true
- onLoad(): void {
- super.onLoad()
- this.regEvent(EventName.changeGold, this.UpdataGold, this)
- // cc.resources.load("json/Skin", cc.JsonAsset, this.LoadDone.bind(this));
- let bundle = cc.assetManager.getBundle("sub");
- bundle.load("res/json/Skin", cc.JsonAsset, this.LoadDone.bind(this));
- }
- start() {
- this.scheduleOnce(() => {
- this.SetScrollViewSize()
- this.UpdataGold()
- }, 0)
- }
- jsonData: SkinType = null
- LoadDone(error: Error, resource: cc.JsonAsset) {
- if (error) {
- console.log(error.name);
- console.log(error.message);
- return;
- }
- this.jsonData = resource.json;
- // console.log(jsonData);
- // this.clickCharacterSkin()
- this.initSetScrollView()
- }
- initSetScrollView() {
- let jsonData = this.jsonData
- console.log(jsonData);
- if (this.Click_CharacterSkin) {
- for (let index = 0; index < jsonData.CharacterSkin.length; index++) {
- let element = jsonData.CharacterSkin[index];
- let temp_node = cc.instantiate(this.CharacterSkinPrefab)
- this.content.addChild(temp_node)
- let Skin = temp_node.getChildByName("Skin").getChildByName("Skin")
- //加入发光节点
- this.LightNodes.push(temp_node.getChildByName("checkbox"))
- //是否拥有了这个皮肤
- let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
- if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
- let has = temp_node.getChildByName("has")
- temp_node.getComponent(cc.Sprite).spriteFrame =
- has.getComponent(cc.Sprite).spriteFrame
- if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
- NodeLabel.string = '使用中'
- //设置当前选择的皮肤类型
- this.SelectCharacterSkinType = element
- } else {
- NodeLabel.string = '选择'
- }
- } else {
- NodeLabel.node.active = false
- let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
- Gold.active = true
- let Label = Gold.getChildByName("Label").getComponent(cc.Label)
- Label.string = element.price.toString()
- }
- //加载图
- const url = element.image
- let bundle = cc.assetManager.getBundle("sub");
- bundle.load(url, cc.SpriteFrame, (err: Error, spriteframe) => {
- if (!err) {
- element.imageSp = spriteframe
- Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
- temp_node['CharacterSkin'] = element
- }
- });
- bundle.load(element.imageMain, cc.SpriteFrame, (err: Error, spriteframe) => {
- if (!err) {
- element.imageSpMain = spriteframe
- }
- });
- //添加点击事件
- temp_node.on('touchstart', () => {
- let temp = temp_node['CharacterSkin'] as CharacterSkinType
- this.ShowCharacterSprite.spriteFrame = temp.imageSpMain
- this.ShowCharacterintroduce.string = temp.introduce
- this.ShowCharacterName.string = temp.name
- this.setLight(temp_node.getChildByName("checkbox"))
- //设置当前选择的皮肤类型
- this.SelectCharacterSkinType = temp
- })
- }
- } else {
- for (let index = 0; index < jsonData.ParabolaSkin.length; index++) {
- let element = jsonData.ParabolaSkin[index];
- let temp_node = cc.instantiate(this.CharacterSkinPrefab)
- this.content.addChild(temp_node)
- let Skin = temp_node.getChildByName("Skin").getChildByName("Skin")
- //加入发光节点
- this.LightNodes.push(temp_node.getChildByName("checkbox"))
- //是否拥有了这个皮肤
- let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
- if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
- let has = temp_node.getChildByName("has")
- temp_node.getComponent(cc.Sprite).spriteFrame =
- has.getComponent(cc.Sprite).spriteFrame
- if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
- NodeLabel.string = '使用中'
- //设置当前选择的皮肤类型
- this.SelectCharacterSkinType = element
- } else {
- NodeLabel.string = '选择'
- }
- } else {
- NodeLabel.node.active = false
- let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
- Gold.active = true
- let Label = Gold.getChildByName("Label").getComponent(cc.Label)
- Label.string = element.price.toString()
- }
- //加载图
- const url = element.image
- // cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
- // if (!err) {
- // element.imageSp = spriteframe
- // Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
- // temp_node['ParabolaSkin'] = element
- // }
- // });
- let bundle = cc.assetManager.getBundle("sub");
- bundle.load(url, cc.SpriteFrame, (err: Error, spriteframe) => {
- if (!err) {
- element.imageSp = spriteframe
- Skin.getComponent(cc.Sprite).spriteFrame = spriteframe
- temp_node['ParabolaSkin'] = element
- }
- });
- bundle.load(element.imageMain, cc.SpriteFrame, (err: Error, spriteframe) => {
- if (!err) {
- element.imageSpMain = spriteframe
- }
- });
- //添加点击事件
- temp_node.on('touchstart', () => {
- let temp = temp_node['ParabolaSkin'] as CharacterSkinType
- this.ShowCharacterSprite.spriteFrame = temp.imageSpMain
- this.ShowCharacterintroduce.string = temp.introduce
- this.ShowCharacterName.string = temp.name
- this.setLight(temp_node.getChildByName("checkbox"))
- //设置当前选择的皮肤类型
- this.SelectParabolaSkinType = temp
- })
- }
- }
- }
- updataSetScrollView() {
- if (this.Click_CharacterSkin) {
- for (let index = 0; index < this.content.children.length; index++) {
- const temp_node = this.content.children[index];
- let element = this.jsonData.CharacterSkin[index];
- //是否拥有了这个皮肤
- let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
- let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
- if (LocalData.getInstance().getHasCharacterSkin().includes(element.id)) {
- Gold.active = false
- NodeLabel.node.active = true
- let has = temp_node.getChildByName("has")
- temp_node.getComponent(cc.Sprite).spriteFrame =
- has.getComponent(cc.Sprite).spriteFrame
- if (LocalData.getInstance().getCurrentCharacterSkin() == element.id) {
- NodeLabel.string = '使用中'
- //设置当前选择的皮肤类型
- this.SelectCharacterSkinType = element
- } else {
- NodeLabel.string = '选择'
- }
- } else {
- NodeLabel.node.active = false
- Gold.active = true
- let Label = Gold.getChildByName("Label").getComponent(cc.Label)
- Label.string = element.price.toString()
- }
- }
- } else {
- for (let index = 0; index < this.content.children.length; index++) {
- const temp_node = this.content.children[index];
- let element = this.jsonData.ParabolaSkin[index];
- //是否拥有了这个皮肤
- let NodeLabel = temp_node.getChildByName("btn").getChildByName("Label").getComponent(cc.Label)
- let Gold = temp_node.getChildByName("btn").getChildByName("Gold")
- if (LocalData.getInstance().getHasParabolaSkin().includes(element.id)) {
- Gold.active = false
- NodeLabel.node.active = true
- let has = temp_node.getChildByName("has")
- temp_node.getComponent(cc.Sprite).spriteFrame =
- has.getComponent(cc.Sprite).spriteFrame
- if (LocalData.getInstance().getCurrentParabolaSkin() == element.id) {
- NodeLabel.string = '使用中'
- //设置当前选择的皮肤类型
- this.SelectParabolaSkinType = element
- } else {
- NodeLabel.string = '选择'
- }
- } else {
- NodeLabel.node.active = false
- Gold.active = true
- let Label = Gold.getChildByName("Label").getComponent(cc.Label)
- Label.string = element.price.toString()
- }
- }
- }
- }
- SetScrollViewSize() {
- let content_width = this.content.getContentSize().width
- let spacingX = (content_width - 4 * this.CharacterSkinPrefab.getContentSize().width) / 5
- this.content.getComponent(cc.Layout).spacingX = spacingX
- this.content.getComponent(cc.Layout).paddingLeft = spacingX
- }
- clickParabolaSkin() {
- this.ParabolaSkin.color = new cc.Color(200, 200, 200, 255);
- this.CharacterSkin.color = new cc.Color(255, 255, 255, 255);
- this.Select.setPosition(this.ParabolaSkin.position)
- this.Click_CharacterSkin = false
- this.content.children.forEach(e => e.destroy())
- this.initSetScrollView()
- }
- clickCharacterSkin() {
- this.ParabolaSkin.color = new cc.Color(255, 255, 255, 255);
- this.CharacterSkin.color = new cc.Color(200, 200, 200, 255);
- this.Select.setPosition(this.CharacterSkin.position)
- this.Click_CharacterSkin = true
- this.content.children.forEach(e => e.destroy())
- this.initSetScrollView()
- }
- clickExit() {
- cc.Canvas.instance.node.getComponent(Hall).init()
- this.node.destroy()
- }
- //发光节点数组
- LightNodes: cc.Node[] = []
- setLight(selectNode: cc.Node) {
- this.LightNodes.forEach(e => {
- if (e == selectNode) {
- e.active = true
- } else {
- e.active = false
- }
- })
- }
- // update (dt) {}
- UpdataGold() {
- this.Gold.string = LocalData.getInstance().getGold().toString()
- }
- UseSkin() {
- if (this.Click_CharacterSkin == false) {
- //是否拥有这个皮肤
- if (LocalData.getInstance().getHasParabolaSkin().includes(this.SelectParabolaSkinType.id)) {
- //拥有
- LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
- //更新展示
- this.updataSetScrollView()
- } else {
- //没拥有
- if (LocalData.getInstance().getGold() >= this.SelectParabolaSkinType.price) {
- //购买
- LocalData.getInstance().setGold(this.SelectParabolaSkinType.price, '-')
- //拥有
- LocalData.getInstance().setHasParabolaSkin(this.SelectParabolaSkinType.id)
- //当前用的
- LocalData.getInstance().setCurrentParabolaSkin(this.SelectParabolaSkinType.id)
- //更新展示
- this.updataSetScrollView()
- } else {
- let temp: PopType = new PopType()
- temp.string = EventLabel.OpenVideo
- temp.Title = '提示'
- temp.OK = () => {
- this.OpenAD()
- }
- temp.Fail = () => {
- }
- this.Pop(temp)
- }
- }
- } else {
- //是否拥有这个皮肤
- if (LocalData.getInstance().getHasCharacterSkin().includes(this.SelectCharacterSkinType.id)) {
- //拥有
- LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
- //更新展示
- this.updataSetScrollView()
- } else {
- //没拥有
- if (LocalData.getInstance().getGold() >= this.SelectCharacterSkinType.price) {
- //购买
- LocalData.getInstance().setGold(this.SelectCharacterSkinType.price, '-')
- //拥有
- LocalData.getInstance().setHasCharacterSkin(this.SelectCharacterSkinType.id)
- //当前用的
- LocalData.getInstance().setCurrentCharacterSkin(this.SelectCharacterSkinType.id)
- //更新展示
- this.updataSetScrollView()
- } else {
- let temp: PopType = new PopType()
- temp.string = EventLabel.OpenVideo
- temp.Title = '提示'
- temp.OK = () => {
- this.OpenAD()
- }
- temp.Fail = () => {
- }
- this.Pop(temp)
- }
- }
- }
- }
- //看广告
- OpenAD() {
- LocalData.getInstance().setGold(1100, '+')
- }
- }
|