CocosCreator 版本的石头剪刀布

Stella981
• 阅读 658

石头剪刀布功能:界面一打开,石头剪刀布三个图标就会循环滚动。点击挺就会对比下标,计算结果

游戏界面:

CocosCreator 版本的石头剪刀布

 代码实现:

import UI_Base from "./UI_Base";
import { SlideCell } from "../Common/UI/LoopSlide";
import { G_TurntableData } from "../Data/TurntableData";
import { SmallGameType, SmallGameResType } from "../Net/MsgDefine";
import { ConfigManger } from "../Manager/ConfigManager";
import { Util } from "../Common/Util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UI_ShiTouJIanDao extends UI_Base {
    slotList: UI_ShiTouJIanDaoItem[] = new Array<UI_ShiTouJIanDaoItem>()
    button: cc.Button
    curSprite: cc.Sprite
    playerIcon: cc.Sprite
    item_icon: cc.Sprite
    fansName: cc.Label

    private speed: number = 1000
    private btnArr: cc.Node[] = [];
    private hRange: cc.Vec2
    private yMax: number = -88
    private yMin: number = -616
    private size: cc.Vec2 = new cc.Vec2(176, 176)
    private isRun: boolean = false
    private isPause: boolean = false

    finishedCallBack: Function
    // 1石头 2剪刀 3布
    slotDataList: number[] = [1, 2, 3, 1, 1, 2, 3, 2, 3, 1, 3, 3, 2, 2, 1, 2, 3, 2, 2, 1, 2, 3, 3, 2, 1]
    spriteFrame = {}
    targetType: number = 2
    onLoad() {
        this.item_icon = this.findSpriteByName(this.node, "item_icon")
        this.playerIcon = this.findSpriteByName(this.node, "playIcon")
        this.curSprite = this.findSpriteByName(this.node, "Result/icon")
        this.button = this.findButtonByName(this.node, "okButton")
        this.addButtonClick(this.button, () => {
            this.OnStop()
        })
        this.fansName = this.findLabelByName(this.node, "fensi_expdi/fansName")
        for (let i = 1; i <= 4; i++) {
            let node = this.findChildByName(this.node, "view/content/item_" + i)
            let slotItem = node.addComponent(UI_ShiTouJIanDaoItem)
            slotItem.OnInit(node, this)
            slotItem.index = i
            this.slotList.push(slotItem)
            this.btnArr.push(node)
        }

        this.spriteFrame[1] = this.slotList[0].img.spriteFrame
        this.spriteFrame[2] = this.slotList[1].img.spriteFrame
        this.spriteFrame[3] = this.slotList[2].img.spriteFrame

        //随机一个目标
        this.targetType = Util.getRandomInt(1, 3)
        this.curSprite.spriteFrame = this.spriteFrame[this.targetType]

    }

    start() {
        let film_random_config = ConfigManger.GetTable("film_random_portrait")
        let array = []
        for (var v in film_random_config) {
            let item = film_random_config[v]
            let iconImage: string = item.portrait
            if (iconImage.search("woman") > 0) {
                array.push(item)
            }
        }

        let index = Util.getRandomInt(0, array.length - 1)
        this.loadTextureByName(this.playerIcon, "sprites/wxusericon/" + array[index].portrait)

        let configTable = ConfigManger.GetTable("game_fingleguess")
        let config;
        for (let v in configTable) {
            config = configTable[v]
        }

        //计算奖励数量
        let itemId: number = 0
        let itenNum: number = 0
        for (var v in config.currencyAward) {
            itemId = Number(v)
        }
        let itemConfig = ConfigManger.GetTableConfigByKey("item_base", itemId)
        this.loadTextureByName(this.item_icon, itemConfig.icon)
        this.RandomFansName()
    }

    RandomFansName() {
        this.fansName.string = ConfigManger.GetTableConfigRandom("nickname_female").name;
    }

    curY: number = 0
    waitTime: number = 0
    update(dt) {
        if (this.isRun) {
            if (this.waitTime <= 0) {
                this.curY += 22
                this.movePosition(22)
                if (this.curY % this.size.y == 0) {
                    if (this.isPause) {
                        this.isRun = false
                        this.ShowSelectItem()
                        return
                    }
                    this.waitTime = 0.13
                }
            }
            else {
                this.waitTime -= dt
            }
        }
    }

    ShowSelectItem() {
        for (let i = 0; i < this.btnArr.length; i++) {
            if (this.btnArr[i].y == -264) {
                let ui_item: UI_ShiTouJIanDaoItem = this.btnArr[i].getComponent(UI_ShiTouJIanDaoItem)
                let curType = this.slotDataList[ui_item.index - 1]
                ui_item.OnSelect()
                let result = SmallGameResType.FG_RES_FAIL
                console.error(this.targetType, curType)

                if (this.targetType == 1) {
                    //石头
                    if (curType == 1)  //石头  平局
                    {
                        result = SmallGameResType.FG_RES_DRAW
                    }
                    else if (curType == 2) { //剪刀 失败
                        result = SmallGameResType.FG_RES_FAIL
                    }
                    else if (curType == 3) { //布 胜利
                        result = SmallGameResType.FG_RES_SUCCESS
                    }
                }
                else if (this.targetType == 2) {
                    //剪刀
                    if (curType == 1)  //石头  胜利
                    {
                        result = SmallGameResType.FG_RES_SUCCESS
                    }
                    else if (curType == 2) { //剪刀 平局
                        result = SmallGameResType.FG_RES_DRAW
                    }
                    else if (curType == 3) { //布  失败
                        result = SmallGameResType.FG_RES_FAIL
                    }
                }
                else if (this.targetType == 3) {
                    //布
                    if (curType == 1)  //石头  失败
                    {
                        result = SmallGameResType.FG_RES_FAIL
                    }
                    else if (curType == 2) { //剪刀 胜利
                        result = SmallGameResType.FG_RES_SUCCESS
                    }
                    else if (curType == 3) { //布  平局
                        result = SmallGameResType.FG_RES_DRAW
                    }
                }

                if (result == SmallGameResType.FG_RES_SUCCESS) {
                    console.error("胜利")
                }
                else if (result == SmallGameResType.FG_RES_DRAW) {
                    console.error("平局")
                }
                else if (result == SmallGameResType.FG_RES_FAIL) {
                    console.error("失败")
                }
                G_TurntableData.RequestGameAward(SmallGameType.GAME_FINGLEGUESS, result)

                //延迟两秒
                this.scheduleOnce(() => {
                    if (this.finishedCallBack != null) {
                        this.finishedCallBack()
                    }
                }, 1)
            }
        }
    }

    private movePosition(y: number) {

        if (y == 0) return
        this.btnArr.forEach(element => {
            element.setPositionY(element.position.y + y)
        });

        let maxidx: number = 0;
        let minidx: number = 0;
        let miny: number = this.btnArr[0].position.y
        let maxy: number = this.btnArr[0].position.y

        if (y > 0) {   //向上移动
            for (let index = 1; index < this.btnArr.length; index++) {
                let element = this.btnArr[index];
                if (miny > element.y) {
                    miny = element.y;
                }
                if (maxy < element.y) {
                    maxy = element.y;
                    maxidx = index;
                }
            }
            if (maxy > this.yMax) {
                this.btnArr[maxidx].setPositionY(miny - this.size.y);
                let operationcell: SlideCell = this.btnArr[maxidx].getComponent(UI_ShiTouJIanDaoItem)
                let idx: number = operationcell.index;
                idx = idx + 1;
                if (idx > this.slotDataList.length) {
                    idx = 1;
                }
                operationcell.refreshCellData(idx);
            }
        }
        else if (y < 0) {  //向下移动
            for (let index = 1; index < this.btnArr.length; index++) {
                let element = this.btnArr[index];
                if (miny > element.y) {
                    miny = element.y;
                    minidx = index;
                }
                if (maxy < element.y) {
                    maxy = element.y;
                }
            }

            if (miny < this.yMin) {
                this.btnArr[minidx].setPositionY(maxy + this.size.y);
                let operationcell: UI_ShiTouJIanDaoItem = this.btnArr[minidx].getComponent(UI_ShiTouJIanDaoItem)
                let idx: number = operationcell.index;
                idx = idx - 1;
                if (idx < 1) {
                    idx = this.slotDataList.length;
                }
                operationcell.refreshCellData(idx);
            }
        }
    }

    Play(callback) {
        this.finishedCallBack = callback
        this.node.active = true
        this.isRun = true
        this.isPause = false
        this.unscheduleAllCallbacks()
    }

    OnStop() {
        if (this.isPause == false) {
            this.isRun = true
            this.isPause = true
        }
    }

    // update (dt) {}
}

class UI_ShiTouJIanDaoItem extends SlideCell {
    node: cc.Node
    ui: UI_ShiTouJIanDao
    curPosition
    targetPosition
    img: cc.Sprite

    isPlay = false

    OnInit(_node: cc.Node, _ui: UI_ShiTouJIanDao) {
        this.node = _node
        this.ui = _ui
        this.img = _ui.findSpriteByName(this.node, "icon")
        this.curPosition = new cc.Vec2(0, -50)
        this.targetPosition = new cc.Vec2(0, 0)
    }

    OnSelect() {
        let action1 = cc.scaleTo(0.5, 1.2)
        let action2 = cc.scaleTo(0.2, 0.8)
        let action3 = cc.scaleTo(0.3, 1)

        this.img.node.runAction(cc.sequence(action1, action2, action3))
    }

    refreshCellData(p_index: number) {
        super.refreshCellData(p_index)
        this.img.spriteFrame = this.ui.spriteFrame[this.ui.slotDataList[p_index - 1]]
    }
}

export class SlideCell extends cc.Component {
    index: number

    refreshCellData(p_index: number) {
        this.index = p_index
    }
}

大 ``

点赞
收藏
评论区
推荐文章
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
3年前
DDD专题案例三《领域驱动设计架构基于SpringCloud搭建微服务》
!(https://oscimg.oschina.net/oscnet/ea8c4fbbc514341be35cc89aa9874e02209.jpg)作者:付政委成长总是来自于对未知领域的探索| 库布齐50公里穿行微信公众号:bugstack虫洞栈|关注获取源码沉淀、分享、成长,专注于原创专题案例,以最易学习编程的方式分
Stella981 Stella981
3年前
Spring boot源码分析之Spring循环依赖揭秘
!(https://oscimg.oschina.net/oscnet/be79581de12c41704c44e976d329ad35ad1.gif)若你是一个有经验的程序员,那你在开发中必然碰到过这种现象:事务不生效。或许刚说到这,有的小伙伴就会大惊失色了。Spring不是解决了循环依赖问题吗,它是怎么又会发生循环依赖的呢?,接下来就
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
CSS 奇思妙想:超级酷炫的边框动画
点上方蓝字关注公众号「前端从进阶到入院」精选原创好文助你进入大厂文章转载自公众号「iCSS前端趣闻」今天逛博客网站shoptalkshow\1\,看到这样一个界面,非常有意思:!(https://oscimg.oschina.net/oscnet/9655b35af5a045999ff55c144a3f7c
Stella981 Stella981
3年前
Nepxion Discovery 5.5.1 发布
!(https://oscimg.oschina.net/oscnet/a04d68686fc04ebd87d233f33be49196.png)NepxionDiscovery5.5.1发布版本更新:升级SpringBoot到2.1.10.RELEASE升级SpringCloudAl
可莉 可莉
3年前
10 年 bloger 告诉你要不要写博客,又该如何优雅地写博客?
!(https://oscimg.oschina.net/oscnet/a5f6a3b8057148fdb458379b1f71e33b.gif)点击上方蓝色字体,关注我——一个在阿里云打工的清华学渣!关于作者:程序猿石头(ID:tangleithu),现任阿里巴巴技术专家,清华学渣,前大疆后端L
Wesley13 Wesley13
3年前
2D小游戏开发学习笔记(5)
一、围住神经猫游戏游戏玩法:玩法很简单,蓝色圆圈代表神经猫,通过点击周围圆圈把猫困住,就算游戏成功游戏效果!(https://oscimg.oschina.net/oscnet/up968a35abafe07c092eacca8126719e14a50.png)逻辑梳理:1、
Wesley13 Wesley13
3年前
Unity5.6.4f1 配置WebGL教程
Unity5.6.4f1发布WebGL的配置教程步骤一:先查看自带的Unity是否yi配置好WebGL的项,若无,则可遵循以下教程来设置!(https://oscimg.oschina.net/oscnet/54612ae3d9b094f1db96b00b1c81a5fe432.png)步骤二:下图是我已经设置好的,未设置
Stella981 Stella981
3年前
ReactNative state更新,视图不更新的问题
开发中遇到这样的问题,我更新了state一个数组的某个元素的选中状态,打印出的数据也显示修改正确了,但是界面却没更新。例如下图点击某项修改选中状态。!(https://oscimg.oschina.net/oscnet/c3291a62b5f638d1e35dd7a719ade39f226.png)代码中之前是这样写的,结果界面没有更新。