加入房间的输入层

本文介绍了一个使用Lua编写的输入层模块,该模块用于游戏场景中处理玩家输入,包括房间号输入和回放码输入等功能。文章详细展示了如何创建输入层、处理按键事件及输入验证等关键步骤。
--创建输入层
local InputLayer = class("InputLayer", function()
    return cc.Layer:create();
end)


InputLayer.__index = InputLayer;


INPUT_LAYER_JOIN_ROOM = 1;
INPUT_LAYER_SEE_REPLAY = 2;


InputLayer._layerType = 0;
InputLayer._currInputNumIndex = 1; --当前输入的数字索引号
InputLayer._inputNumTable = {}; --当前输入的数字
InputLayer._isEnter = false; 
InputLayer._inputLayer = nil;
InputLayer._rootNode = nil;


--创建场景
function InputLayer:createInputLayer(inputLayerId)
    local layer = InputLayer.new();  
    layer:setName("SCMJ_InputLayer");
    --限定类型 
    if inputLayerId<1 or inputLayerId>2 then
        return nil;
    end
    --添加场景
    layer._layerType = inputLayerId;


    layer._inputLayer = layer:InitLayer();


    --场景节点事件处理  
    local function onNodeEvent(event)  
        if event == "enter" then  
            layer:onEnter()  
--        elseif event == "enterTransitionFinish" then  
--            scene:onEnterTransitionFinish()  
--        elseif event == "exit" then  
--            scene:onExit()  
--        elseif event == "exitTransitionStart" then  
--            scene:onExitTransitionStart()  
--        elseif event == "cleanup" then  
--            scene:cleanup()  
       end  
    end  
    --注册
    layer:registerScriptHandler(onNodeEvent);


    layer:addChild(layer._inputLayer);


    return layer;
end


--进入场景
function InputLayer:onEnter()
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode then
        ViewPopAnimation(baseNode);
    end


end


------------------------------------------------
--按钮的触摸
function InputLayer:BtnTouchCallBack(sender,eventType)
    if eventType == ccui.TouchEventType.began then
        --播放按钮音效
        ButtonSE();
    end
end


--数字按钮的触摸
function InputLayer:NumBtnTouchCallBack(sender,eventType)
    if eventType == ccui.TouchEventType.began then
        --播放按钮音效
        ButtonSE();
    end
end


--关闭按钮的实现
function InputLayer:CloseBtnCallback(sender)
    --移除界面
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode then
        ViewCloseAnimation(baseNode, function()
            self:removeFromParent();
        end);
    end
end


--数字按钮的实现
function InputLayer:NumBtnCallBack(sender)
    --获取按钮的标签
    local btnTag = sender:getTag();
    
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --获取当前输入的索引
    local currNumIndex = self._currInputNumIndex;
    --不同按钮的不同操作
    if btnTag>0 and btnTag<10 then
        --数字1~9的按钮
        if currNumIndex <= 6 then
            --添加数字到table
            self._inputNumTable[currNumIndex] = btnTag;
            --显示输入的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex) );
            if textNum ~= nil then
                textNum:setString(string.format("%d",btnTag));
            end
            --输入数字的索引增加
            self._currInputNumIndex = currNumIndex + 1;
        end


        --数字索引大于等于6 直接调用确认进入
        if currNumIndex >= 6 then
            self:confirmJoin();
        end


    elseif btnTag == 11 then
        --数字0的按钮
        if currNumIndex <= 6 then
            --显示输入的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex) );
            if textNum ~= nil then
                textNum:setString(string.format("%d",0));
            end
            --添加数字到table
            self._inputNumTable[currNumIndex] = 0;
            --输入数字的索引增加
            self._currInputNumIndex = currNumIndex + 1;
        end


        --数字索引大于等于6 直接调用确认进入
        if currNumIndex >= 6 then
            self:confirmJoin();
        end


    elseif btnTag == 10 then
        --重输的按钮
        self._isEnter = false;
        self:confirmJoin();
        
    elseif btnTag ==12 then
        --删除的按钮
        self._isEnter = false;
        if currNumIndex>1 then
            --删除一个显示的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex-1) );
            if textNum ~= nil then
                textNum:setString("");
            end


            --删除table表中的一个数字
            self._inputNumTable[currNumIndex-1] = nil;
            self._currInputNumIndex = currNumIndex-1;
        end
    --确定按钮
    elseif btnTag == 13 then    
        self:ClearInputNum();
    end
end


--确认加入房间
function InputLayer:confirmJoin()
    if self._currInputNumIndex >= 7 and self._isEnter == false then
        --self._isEnter = true;
        --跳转
        print("InputLayer: number is full!");
        if self._layerType == INPUT_LAYER_JOIN_ROOM then
            --发送加入房间的信息
            local RoomDataManager = require("app.Manager.RoomDataManager");  
            --获取房间号          
            local inputNumStr = "";
            for i=1,6 do
                inputNumStr = inputNumStr .. IntToString(self._inputNumTable[i]);
            end
            local roomId = StringToInt(inputNumStr);
           
            --发送房间号
            RoomDataManager:SendJoinRoom(roomId);
            print("send roomId=",roomId);
--                isCoinPlay = false      --非匹配赛
            --添加加载层
            AddLoadingLayer(10, TryToReconnectServer);


        elseif self._layerType == INPUT_LAYER_SEE_REPLAY then
            --查看他人回放的界面


        end
    end
end


function InputLayer:ClearInputNum()
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --清除显示的数字
    for i=1,6 do
        local textNum = baseNode:getChildByName( string.format("Text_num_%d",i) );
        if textNum ~= nil then
            textNum:setString("");
        end
    end


    --清除table
    self._inputNumTable = {};
    self._currInputNumIndex = 1;
end
------------------------------------------------------------------
--加载场景
function InputLayer:InitLayer()


    --创建有颜色的底层
    local diLayer = cc.LayerColor:create(cc.c4b(0,0,0,150));


    --获取界面
    local upLayer = cc.CSLoader:createNode("InputLayer.csb");
    self._rootNode = upLayer;
    diLayer:addChild(upLayer);


    --关闭按钮的功能
    local function closeBtnFunc(sender)
        self:CloseBtnCallback(sender);
    end


    local baseNode = upLayer:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --获取关闭按钮
    local closeBtn = baseNode:getChildByName("Button_back");
    if closeBtn ~= nil then
        closeBtn:addClickEventListener(closeBtnFunc);
        closeBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
    end


    --获取文字
    local textNotice = baseNode:getChildByName("Text_title");
    if textNotice ~= nil then
        if self._layerType == INPUT_LAYER_JOIN_ROOM then
            --显示请输入房间号
            print("id=",DictTable_InputLayer_JoinRoom,"; g_DictTable Count=",#g_DictTable)      
            textNotice:setString( g_DictTable[DictTable_InputLayer_JoinRoom]["Content"] );       
        else
            --显示请输入他人分享的回访码
            textNotice:setString(g_DictTable[DictTable_InputLayer_SeeReplay]["Content"]);
        end
    end


    --获取输入的号码
    for i=1,6 do
        local TextNum = baseNode:getChildByName( string.format("Text_num_%d",i) );
        if TextNum ~= nil then
            TextNum:setString("");
        end
    end
    
    --按钮的功能
    local function numBtnTouch(sender,eventType)
        self:NumBtnTouchCallBack(sender,eventType);
    end
    local function numBtnFunc(sender)
        self:NumBtnCallBack(sender);
    end
    --获取按钮
    for i=1,13 do
        local numBtn = baseNode:getChildByName( string.format("Button_num_%d",i) );
        if numBtn ~= nil then
            numBtn:addClickEventListener(numBtnFunc);
            numBtn:addTouchEventListener(numBtnTouch);
            numBtn:setTag(i);
        end
    end
   


    return diLayer;
end






return InputLayer;
题目描述 传说很遥远的藏宝楼顶藏着诱人的宝藏。小明历尽千辛万苦终于找到传说中的这个藏 宝楼,藏宝楼的门口竖着一个木板,上面写有几个大字:寻宝说明书。说明书的内容如下: 藏宝楼共有 N+1 ,最上面一是顶,顶有一个房间里面藏着宝藏。除了顶外, 藏宝楼另有 N ,每 M 个房间,这 M 个房间围成一圈并按逆时针方向依次编号为 0,…, M-1。其中一些房间有通往上一的楼梯,每楼的楼梯设计可能不同。每个房间里有一个 指示牌,指示牌上有一个数字 x,表示从这个房间开始按逆时针方向选择第 x 个有楼梯的房 间(假定该房间的编号为 k),从该房间上楼,上楼后到达上一的 k 号房间。比如当前房 间的指示牌上写着 2,则按逆时针方向开始尝试,找到第 2 个有楼梯的房间,从该房间上楼。 如果当前房间本身就有楼梯通向上,该房间作为第一个有楼梯的房间。 寻宝说明书的最后用红色大号字体写着:“寻宝须知:帮助你找到每上楼房间的指示 牌上的数字(即每第一个进入的房间内指示牌上的数字)总和为打开宝箱的密钥”。 请帮助小明算出这个打开宝箱的密钥。 输入描述 第一行 2 个整数 N 和 M,之间用一个空格隔开。N 表示除了顶外藏宝楼共 N 楼, M 表示除顶外每楼有 M 个房间。 接下来 N*M 行,每行两个整数,之间用一个空格隔开,每行描述一个房间内的情况, 其中第(i-1)*M+j 行表示第 i j-1 号房间的情况(i=1, 2, …, N;j=1, 2, … ,M)。第一个整数 表示该房间是否有楼梯通往上一(0 表示没有,1 表示有),第二个整数表示指示牌上的数 字。注意,从 j 号房间的楼梯爬到上一到达的房间一定也是 j 号房间。 最后一行,一个整数,表示小明从藏宝楼底的几号房间进入开始寻宝(注:房间编号 从 0 开始)。 输出描述 输出只有一行,一个整数,表示打开宝箱的密钥,这个数可能会很大,请输出对 20123 取模的结果即可。 正解代码如下:#include<bits/stdc++.h> using namespace std; struct node{ //结构体存储房间信息 int a,b; //a表示当前房间有没有楼梯,b是第几个有楼梯 }a[10005][105]; int n,m,p,c[10005]; int main(){ /** 选择房间的核心是逆时针选择第x个有楼梯的房间, **/ cin>>n>>m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j].a>>a[i][j].b; if(a[i][j].a==1){ c[i]++; } } } cin>>p; long long sum=0; int num; for(int i=0;i<n;i++){ //从第0开始模拟,p sum=(sum+a[i][p].b%20123)%20123; //求和 num=(a[i][p].b)%c[i]; //计算从第num个开始上楼 if (num==0) num=c[i]; while(num){ if(a[i][p].a==1){ num--; } if(num) p++; p%=m; //避免p超过 } } cout<<sum; return 0; } 问:num=(a[i][p].b)%c[i]; //计算从第num个开始上楼 if (num==0) num=c[i]; 为什么需要特判num为0的情况?
最新发布
12-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值