标签:
最后献上完整的1000行代码,基本功能已经完成,可以通过配置小怪和矿的位置和大小控制玩家时长和难度:
-------------------------------------------------------------------------------------------- -- Added by ??? -------------------------------------------------------------------------------------------- require("CommonFunction") require("CommonVariables") require("FontHelper") -------------------------------------------------------------------------------------------- local MineSecretDialog = class( "MineSecretDialog", function() return ModalDialogBase:create() end) local Tag = { MainBg = 50, LevelLabel = 100, SkillDotLabel = 101, MoneyLabel = 102, IronKeyLabel = 103, SilverKeyLabel = 104, GoldKeyLabel = 105, CollectionLabel = 110, -- 110-120 ExpNumLabel = 200, HpNumLabel = 201, ActiveNumLabel = 202, RecoverNumLabel = 203, AtkNumLabel = 204, DefNumLabel = 205, BonusNumLabel = 250, BonusLockLabel = 251, BonusSprite = 252, SysNumLabel = 260, SysCostLabel = 270, SysNameLabel = 271, } local userData = { floor = 1, -- 楼层 level = 0, -- 等级 curExp = 0, -- 当前经验 maxExp = 0, -- 升级经验 money = 0, -- 金钱 ironKey = 0, -- 铁钥匙 silverKey = 0, -- 银钥匙 goldKey = 0, -- 金钥匙 skillDot = 0, -- 升级点 curActive = 0, -- 当前体力 maxActive = 0, -- 体力 curHp = 0, -- 当前生命 maxHp = 0, -- 生命 recover = 0, -- 恢复 atk = 0, -- 攻击 def = 0, -- 防御 collection = { -- 收藏 {letter = "S", num = 0}, {letter = "H", num = 0}, {letter = "O", num = 0}, {letter = "W", num = 0}, {letter = "E", num = 0}, {letter = "R", num = 0}, }, } local ColorList = { H_Font_GrowUpSmall_10, H_Font_BrightYellow_10, G_Font_Purple_10, H_Font_LightBlue_10, H_Font_ORANGE_10, H_Font_DarkYellowLittle, } local ColorLock = { H_Font_LightBlue_10, H_Font_ORANGE_10, G_Font_Purple_10, } local BarMaxNum = { HP = 1000, ACTIVE = 500, RECOVER = 200, ATK = 200, DEF = 200, KEY = 99, COLLECT = 1, } local BonusType = CreateEnumTable({"MINE", "ENEMY", "SYSTEM", "BOSS"}) local SysType = CreateEnumTable({"AACT", "AATK", "ADEF", "AKEY", "ADOT", "ROCK"}) local sz = CCDirector:sharedDirector():getVisibleSize() local posX, posX2, posY, gap = 150, 320, sz.height - 13, 15 local barSize = CCSizeMake(120,12) -------------------------------------------------------------------------------------------- function MineSecretDialog:init() self:registerScriptHandler( self.onNodeEvent ) self:setContentSize( sz ); CFModuleReload("MineSecretConfig") local menu = ScrollMenu:create() menu:setContentSize(sz); self:addChild( menu, 2 ); menu:setPosition( ccp(0,0) ); self.m_menu = menu AddSprite("p_diabg_common.png", ccp(sz.width/2, sz.height/2), self, Tag.MainBg, -10) local bgKuang = CCScale9Sprite:create( "p_pvpol_bg_blue.png", CCRectMake(0, 0, 485/2, 173/2), CCRectMake(282/2, 90/2, 10, 10) ) bgKuang:setContentSize( CCSizeMake(sz.width, sz.height) ) self:addChild( bgKuang, 1 ) bgKuang:setPosition( sz.width/2, sz.height/2 ) self.m_bgKuang = bgKuang local arrowup = AddSprite("p_cmn_arrow_up.png", ccp(posX2 + 175, posY + 5), bgKuang) arrowup:setScale(0.5) arrowup:runAction(CCRepeatForever:create(CCSequence:createWithTwoActions( CCEaseSineInOut:create(CCMoveBy:create(0.8, ccp(0, 2))), CCEaseSineInOut:create(CCMoveBy:create(0.8, ccp(0, -2)))))) AddSprite("yaoshi1.png", ccp(posX2 + 150, posY - gap * 2), bgKuang):setScale(0.25) AddSprite("yaoshi2.png", ccp(posX2 + 177, posY - gap * 2), bgKuang):setScale(0.25) AddSprite("yaoshi3.png", ccp(posX2 + 204, posY - gap * 2), bgKuang):setScale(0.25) AddLabel( "经验", ccp(posX - 12, posY), bgKuang, 0, ColorList[1] ) self.m_expBar = AddScale9Sprite("p_purecolor_green.png", ccp(posX, posY), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_expBar:setAnchorPoint(ccp(0, 0.5)) AddLabel( "生命", ccp(posX - 12, posY - gap), bgKuang, 0, ColorList[2] ) self.m_hpBar = AddScale9Sprite("p_purecolor_brightyellow.png", ccp(posX, posY - gap), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_hpBar:setAnchorPoint(ccp(0, 0.5)) AddButton( "p_followpet_detail_addbtn.png", "p_followpet_detail_addbtn.png", menu, ccp(posX + 130, posY - gap), self.onAddHp, self ) AddLabel( "体力", ccp(posX - 12, posY - gap * 2), bgKuang, 0, ColorList[3] ) self.m_activeBar = AddScale9Sprite("p_purecolor_purple.png", ccp(posX, posY - gap * 2), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_activeBar:setAnchorPoint(ccp(0, 0.5)) AddButton( "p_followpet_detail_addbtn.png", "p_followpet_detail_addbtn.png", menu, ccp(posX + 130, posY - gap*2), self.onAddActive, self ) AddLabel( "恢复", ccp(posX2 - 12, posY), bgKuang, 0, ColorList[4] ) self.m_recoverBar = AddScale9Sprite("p_purecolor_blue.png", ccp(posX2, posY), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_recoverBar:setAnchorPoint(ccp(0, 0.5)) AddButton( "p_followpet_detail_addbtn.png", "p_followpet_detail_addbtn.png", menu, ccp(posX2 + 130, posY), self.onAddRecover, self ) AddLabel( "攻击", ccp(posX2 - 12, posY - gap), bgKuang, 0, ColorList[5] ) self.m_atkBar = AddScale9Sprite("p_purecolor_orange.png", ccp(posX2, posY - gap), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_atkBar:setAnchorPoint(ccp(0, 0.5)) AddButton( "p_followpet_detail_addbtn.png", "p_followpet_detail_addbtn.png", menu, ccp(posX2 + 130, posY - gap), self.onAddAtk, self ) AddLabel( "防御", ccp(posX2 - 12, posY - gap * 2), bgKuang, 0, ColorList[6] ) self.m_defBar = AddScale9Sprite("p_purecolor_darkyellow.png", ccp(posX2, posY - gap * 2), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), barSize, bgKuang) self.m_defBar:setAnchorPoint(ccp(0, 0.5)) AddButton( "p_followpet_detail_addbtn.png", "p_followpet_detail_addbtn.png", menu, ccp(posX2 + 130, posY - gap*2), self.onAddDef, self ) self:initUserData(1) self:updateUserData() self:addMineAndEnemy() -- 关闭按钮 local pClose = ImageButton:create("p_btn_close.png", "p_btn_close_down.png", nil, self) menu:addChild( pClose, 0 ) pClose:setPosition( ccp(sz.width - 1, sz.height - 1) ) pClose:registerScriptTapHandler( self.onClose ) pClose:setAnchorPoint(ccp(1, 1)) end -- 更新所有UserData function MineSecretDialog:updateUserData() self:addUserExp(0, 0, true) self:updateUserHp(0, 0, true) self:updateUserActive(0, 0, true) self:addUserRecoverAtkDef(0, 0, 0, true) self:updateUserMoney(0, true) self:updateUserKey(0, 0, 0, true) self:updateUserCollection(nil, 0, true) end -- 初始化固定等级的最大值 function MineSecretDialog:initUserData(level) userData.level = level userData.maxExp = getUserMaxExp(level) if level == 1 then local initBattleData = getInitBattleData() userData.curActive = initBattleData.active userData.maxActive = initBattleData.active userData.curHp = initBattleData.hp userData.maxHp = initBattleData.hp userData.recover = initBattleData.recover userData.atk = initBattleData.atk userData.def = initBattleData.def end end -- 更新等级,经验条和升级点 function MineSecretDialog:addUserExp(addExp, addSkillDot, isInit) addExp = addExp or 0 addSkillDot = addSkillDot or 0 isInit = isInit or false local updateLevel, updateSkillDot, updateExp = false, false, false if addExp > 0 then if userData.curExp + addExp >= userData.maxExp then self:updateUserLevel(addExp) updateLevel, updateSkillDot = true, true else userData.curExp = userData.curExp + addExp end updateExp = true end if addSkillDot ~= 0 then userData.skillDot = math.max(0, userData.skillDot + addSkillDot) updateSkillDot = true end if updateLevel or isInit then AddLabel( userData.floor.." F ".."LV. "..userData.level, ccp(48, sz.height - 16), self.m_bgKuang, Tag.LevelLabel, H_Font_Btn_Cancle_Stroke ) end if updateSkillDot or isInit then AddLabel( userData.skillDot, ccp(posX2 + 162, posY), self.m_bgKuang, Tag.SkillDotLabel, ColorList[1] ) end if updateExp or isInit then self.m_expBar:setScaleX(userData.curExp/userData.maxExp) AddLabel( userData.curExp.."/"..userData.maxExp, ccp(posX + 5, posY), self.m_bgKuang, Tag.ExpNumLabel, ColorList[1], ccp(0, 0.5) ) end end -- 计算增加的经验能够连升几级 function MineSecretDialog:updateUserLevel(addExp) userData.curExp = userData.curExp + addExp - userData.maxExp self:initUserData(userData.level + 1) userData.skillDot = userData.skillDot + getLevelUpAddSkillDot(userData.level) if userData.curExp >= userData.maxExp then self:updateUserLevel(0) end end -- 更新血量 function MineSecretDialog:updateUserHp(addHp, addMaxHp, isInit) addHp = addHp or 0 addMaxHp = addMaxHp or 0 isInit = isInit or false local updateHp = false if addMaxHp > 0 then userData.maxHp = userData.maxHp + addMaxHp updateHp = true end if addHp ~= 0 then userData.curHp = math.min(userData.maxHp, math.max(0, userData.curHp + addHp)) updateHp = true end if updateHp or isInit then self.m_hpBar:setScaleX(countBarScaleX(userData.curHp/userData.maxHp, userData.maxHp/BarMaxNum.HP)) AddLabel( userData.curHp.."/"..userData.maxHp, ccp(posX + 5, posY - gap), self.m_bgKuang, Tag.HpNumLabel, ColorList[2], ccp(0, 0.5) ) end end -- 更新体力 function MineSecretDialog:updateUserActive(addActive, addMaxActive, isInit) addActive = addActive or 0 addMaxActive = addMaxActive or 0 isInit = isInit or false local updateActive = false if addMaxActive > 0 then userData.maxActive = userData.maxActive + addMaxActive updateActive = true end if addActive ~= 0 then userData.curActive = math.min(userData.maxActive, math.max(0, userData.curActive + addActive)) updateActive = true end if updateActive or isInit then self.m_activeBar:setScaleX(countBarScaleX(userData.curActive/userData.maxActive, userData.maxActive/BarMaxNum.ACTIVE)) AddLabel( userData.curActive.."/"..userData.maxActive, ccp(posX + 5, posY - gap * 2), self.m_bgKuang, Tag.ActiveNumLabel, ColorList[3], ccp(0, 0.5) ) end end -- 更新恢复,攻击,防御 function MineSecretDialog:addUserRecoverAtkDef(addRecover, addAtk, addDef, isInit) addRecover = addRecover or 0 addAtk = addAtk or 0 addDef = addDef or 0 isInit = isInit or false if addRecover > 0 or isInit then userData.recover = userData.recover + addRecover self.m_recoverBar:setScaleX(countBarScaleX(userData.recover/BarMaxNum.RECOVER)) AddLabel( userData.recover, ccp(posX2 + 5, posY), self.m_bgKuang, Tag.RecoverNumLabel, ColorList[4], ccp(0, 0.5) ) end if addAtk > 0 or isInit then userData.atk = userData.atk + addAtk self.m_atkBar:setScaleX(countBarScaleX(userData.atk/BarMaxNum.ATK)) AddLabel( userData.atk, ccp(posX2 + 5, posY - gap), self.m_bgKuang, Tag.AtkNumLabel, ColorList[5], ccp(0, 0.5) ) end if addDef > 0 or isInit then userData.def = userData.def + addDef self.m_defBar:setScaleX(countBarScaleX(userData.def/BarMaxNum.DEF)) AddLabel( userData.def, ccp(posX2 + 5, posY - gap * 2), self.m_bgKuang, Tag.DefNumLabel, ColorList[6], ccp(0, 0.5) ) end end -- 更新金币 function MineSecretDialog:updateUserMoney(addMoney, isInit) addMoney = addMoney or 0 isInit = isInit or false if addMoney ~= 0 or isInit then userData.money = math.max(0, userData.money + addMoney) AddLabel( "$ "..userData.money, ccp(posX2 + 151, posY - gap), self.m_bgKuang, Tag.MoneyLabel, ColorList[2], ccp(0, 0.5) ) end end -- 更新铜,银,金钥匙 function MineSecretDialog:updateUserKey(addIronKey, addSilverKey, addGoldKey, isInit) addIronKey = addIronKey or 0 addSilverKey = addSilverKey or 0 addGoldKey = addGoldKey or 0 isInit = isInit or false if addIronKey ~= 0 or isInit then userData.ironKey = math.min(BarMaxNum.KEY, math.max(0, userData.ironKey + addIronKey)) AddLabel( userData.ironKey, ccp(posX2 + 163, posY - gap * 2), self.m_bgKuang, Tag.IronKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) ) end if addSilverKey ~= 0 or isInit then userData.silverKey = math.min(BarMaxNum.KEY, math.max(0, userData.silverKey + addSilverKey)) AddLabel( userData.silverKey, ccp(posX2 + 190, posY - gap * 2), self.m_bgKuang, Tag.SilverKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) ) end if addGoldKey ~= 0 or isInit then userData.goldKey = math.min(BarMaxNum.KEY, math.max(0, userData.goldKey + addGoldKey)) AddLabel( userData.goldKey, ccp(posX2 + 217, posY - gap * 2), self.m_bgKuang, Tag.GoldKeyLabel, H_Font_Gray_Stroke_10, ccp(0.5, 0.5) ) end end -- 更新收藏品 function MineSecretDialog:updateUserCollection(collectLetter, addNum, isInit) addNum = addNum or 1 isInit = isInit or false local count = 0 if collectLetter ~= nil then for i=1,#(userData.collection) do if userData.collection[i].letter == collectLetter then userData.collection[i].num = math.min(BarMaxNum.COLLECT, userData.collection[i].num + addNum) local color = H_Font_WhiteSmall if userData.collection[i].num > 0 then color = ColorList[i] end AddLabel( userData.collection[i].letter, ccp(15 + (i-1)*20, posY - gap * 2), self.m_bgKuang, Tag.CollectionLabel+i, color, ccp(0.5, 0.5) ) end if userData.collection[i].num > 0 then count = count + 1 end end if count == #(userData.collection) then -- TODO: 集齐七龙珠,召唤神龙 for i=1,#(userData.collection) do userData.collection[i].num = userData.collection[i].num - 1 end isInit = true end end if isInit then for i=1,#(userData.collection) do local color = H_Font_WhiteSmall if userData.collection[i].num > 0 then color = ColorList[i] end AddLabel( userData.collection[i].letter, ccp(15 + (i-1)*20, posY - gap * 2), self.m_bgKuang, Tag.CollectionLabel+i, color, ccp(0.5, 0.5) ) end end end -- 增加矿,敌人和系统 function MineSecretDialog:addMineAndEnemy() local bonusList = getBonusListInfo() for i,v in ipairs(bonusList) do if v.types == BonusType.MINE then self:initMineStatus(v) elseif v.types == BonusType.ENEMY then self:initEnemyStatus(v) elseif v.types == BonusType.SYSTEM then self:initSystemStatus(v) elseif v.types == BonusType.BOSS then end end end -- 初始化矿的信息和状态 function MineSecretDialog:initMineStatus(v) local times = 0 local haveRunAction = false local itemSize = CCSizeMake(v.width, v.height) local mineData = getMineActivendRecover(v.width, v.height) local sprite = CCScale9Sprite:create("p_purecolor_gray.png", CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6)) sprite:setContentSize(itemSize) local mineItem = CCMenuItemSprite:create( sprite, sprite ) self.m_menu:addChild(mineItem, 1) mineItem:setPosition(v.pos) mineItem:setAnchorPoint(ccp(0, 1)) local itemBar = AddScale9Sprite("p_purecolor_blue.png", ccp(0, v.height/2), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), itemSize, mineItem) itemBar:setAnchorPoint(ccp(0, 0.5)) if v.locked then AddLabel( "锁", ccp(v.width/2, v.height/2), mineItem, Tag.BonusLockLabel, ColorLock[v.key or 1] ) end if v.digged == false then itemBar:setScaleX(0) AddLabel( "0%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) end mineItem:registerScriptTapHandler(function () game.fleetingLabelManager:pushLabel( "posX "..v.pos.x.." posY "..v.pos.y, self ) cclog("the mine data is",mineData.life,v.digged,v.curlife) if v.locked then if self:checkKeyEnough(v.key) then v.locked = false game.fleetingLabelManager:pushLabel( "解锁成功", self ) mineItem:removeChildByTag( Tag.BonusLockLabel, true ) end else if v.digged and self.m_timer - times < 0.6 then return elseif v.digged then times = self.m_timer end -- 判断剩余的体力是否足够此次点击消耗 local cost = mineData.clickCost if v.digged == false then cost = math.ceil(math.min(mineData.life - v.curlife, mineData.clickCost)) end if userData.curActive < cost then game.fleetingLabelManager:pushLabel( "体力不足", self, "error" ) return end self:updateUserActive(0 - cost) if v.digged then -- 收获金币,并有1%概率获得收藏品 local collectPercent = self:getRandomNum(0.8, 1.2) self:updateUserMoney(math.floor((mineData.clickMoney)*collectPercent)) if v.collect and self:getRandomResult(1) then self:updateUserCollection(v.collect) end else v.curlife = v.curlife + cost itemBar:setScaleX(v.curlife/mineData.life) AddLabel( math.floor(100*v.curlife/mineData.life).."%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) if v.curlife == mineData.life then v.digged = true mineItem:stopAllActions() mineItem:removeChildByTag( Tag.BonusNumLabel, true ) -- 开发完成,开始收获金币、经验,并有20%概率获得收藏品 self:addUserExp(mineData.digExp) self:updateUserMoney(mineData.digMoney) if v.collect and self:getRandomResult(20) then self:updateUserCollection(v.collect) end else if haveRunAction == false then haveRunAction = true local array = CCArray:create() array:addObject(CCDelayTime:create(0.2)) array:addObject(CCCallFunc:create(function () if v.curlife > 0 then v.curlife = math.max(0, v.curlife - mineData.recover) itemBar:setScaleX(v.curlife/mineData.life) AddLabel( math.floor(100*v.curlife/mineData.life).."%", ccp(3, v.height/2), mineItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) end if v.curlife == 0 then haveRunAction = false mineItem:stopAllActions() end end)) mineItem:runAction(CCRepeatForever:create(CCSequence:create(array))) end end end end end) end -- 初始化敌人的信息和状态 function MineSecretDialog:initEnemyStatus(v) local haveRunAction = false local itemSize = CCSizeMake(v.width, v.height) local enemyData = getEnemyLifeAndRecover(v.width, v.height) local sprite = CCScale9Sprite:create("p_purecolor_red.png", CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6)) sprite:setContentSize(itemSize) local enemyItem = CCMenuItemSprite:create( sprite, sprite ) self.m_menu:addChild(enemyItem, 1) enemyItem:setPosition(v.pos) enemyItem:setAnchorPoint(ccp(0, 1)) local itemBar = AddScale9Sprite("p_purecolor_brightyellow.png", ccp(0, v.height/2), CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6), itemSize, enemyItem) itemBar:setAnchorPoint(ccp(0, 0.5)) if v.locked then AddLabel( "锁", ccp(v.width/2, v.height/2), enemyItem, Tag.BonusLockLabel, ColorLock[v.key or 1] ) end if v.digged == false then v.curlife = enemyData.life end itemBar:setScaleX(v.curlife/enemyData.life) AddLabel( v.curlife.."/"..enemyData.life, ccp(3, v.height/2), enemyItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) enemyItem:registerScriptTapHandler(function () game.fleetingLabelManager:pushLabel( "posX "..v.pos.x.." posY "..v.pos.y, self ) cclog("the enemy data is",v.locked,v.digged,v.curlife) if v.locked then if self:checkKeyEnough(v.key) then v.locked = false game.fleetingLabelManager:pushLabel( "解锁成功", self ) enemyItem:removeChildByTag( Tag.BonusLockLabel, true ) end else if v.digged == false then -- 根据攻击和防御计算消耗的血量 local userDamage = math.ceil(math.max(enemyData.atk/4, enemyData.atk - userData.def)) local enemyDamage = math.ceil(math.max(userData.atk/4, userData.atk - enemyData.def)) if userData.curHp < userDamage then self:updateUserHp(0 - userData.curHp) -- game.fleetingLabelManager:pushLabel( "血量不足", self, "error" ) return else self:updateUserHp(0 - userDamage) end v.curlife = math.max(0, v.curlife - enemyDamage) itemBar:setScaleX(v.curlife/enemyData.life) AddLabel( v.curlife.."/"..enemyData.life, ccp(3, v.height/2), enemyItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) if v.curlife == 0 then v.digged = true enemyItem:stopAllActions() -- 开发完成,开始收获金币、经验,并有50%概率获得收藏品 self:addUserExp(enemyData.digExp) self:updateUserMoney(enemyData.digMoney) if v.collect and self:getRandomResult(50) then self:updateUserCollection(v.collect) end else if haveRunAction == false then haveRunAction = true local array = CCArray:create() array:addObject(CCDelayTime:create(0.2)) array:addObject(CCCallFunc:create(function () if v.curlife < enemyData.life then v.curlife = math.min(enemyData.life, v.curlife + enemyData.recover) itemBar:setScaleX(v.curlife/enemyData.life) AddLabel( v.curlife.."/"..enemyData.life, ccp(3, v.height/2), enemyItem, Tag.BonusNumLabel, G_Font_Black_8, ccp(0, 0.5) ) end if v.curlife == enemyData.life then haveRunAction = false enemyItem:stopAllActions() end end)) enemyItem:runAction(CCRepeatForever:create(CCSequence:create(array))) end end end end end) end -- 初始化系统的信息和状态 function MineSecretDialog:initSystemStatus(v) local times = 0 local haveRunAction = false G_Font_Purple = { name = DefaultFont, size = 15, color = FontColor.Purple } G_Font_Orange = { name = DefaultFont, size = 15, color = FontColor.Orange } G_Font_Blue = { name = DefaultFont, size = 15, color = FontColor.Blue } local rockBonus = {"6", "8", "9", "E", "$", "@", "*"} local itemSize = CCSizeMake(v.width, v.height) local sprite = CCScale9Sprite:create("p_purecolor_"..v.color..".png", CCRectMake(0, 0, 10, 10), CCRectMake(2, 2, 6, 6)) sprite:setContentSize(itemSize) local sysItem = CCMenuItemSprite:create( sprite, sprite ) self.m_menu:addChild(sysItem, 1) sysItem:setPosition(v.pos) sysItem:setAnchorPoint(ccp(0, 1)) local clickCost,addNum,sysName = 0,0,"" if v.sys == SysType.AACT then clickCost,addNum,sysName = v.cost[v.curst],v.curst*5,"ACT" elseif v.sys == SysType.AATK then clickCost,addNum,sysName = v.cost[v.curst],v.curst*5,"ATK" elseif v.sys == SysType.ADEF then clickCost,addNum,sysName = v.cost[v.curst],v.curst*5,"DEF" elseif v.sys == SysType.ADOT then clickCost,addNum,sysName = v.cost[v.curst],v.curst*5,"DOT" elseif v.sys == SysType.AKEY then clickCost,sysName = v.cost[1],"KEY" AddLabel( " ? ", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel+1, G_Font_Purple, ccp(0.5, 0.5) ) elseif v.sys == SysType.ROCK then clickCost,sysName = v.cost[1],"ROCK" AddLabel( " ? ", ccp(v.width/2 - 20, v.height/2), sysItem, Tag.SysNumLabel+1, G_Font_Purple, ccp(0.5, 0.5) ) AddLabel( " ? ", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel+2, G_Font_Purple, ccp(0.5, 0.5) ) AddLabel( " ? ", ccp(v.width/2 + 20, v.height/2), sysItem, Tag.SysNumLabel+3, G_Font_Purple, ccp(0.5, 0.5) ) end AddLabel( "$"..clickCost, ccp(5, v.height-10), sysItem, Tag.SysCostLabel, G_Font_Black_8, ccp(0, 0.5) ) AddLabel( sysName, ccp(v.width-5, 10), sysItem, Tag.SysNameLabel, G_Font_Black_8, ccp(1, 0.5) ) if addNum > 0 then AddLabel( "+"..addNum, ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel, G_Font_Purple, ccp(0.5, 0.5) ) end sysItem:registerScriptTapHandler(function () if self.m_timer - times < 1 then return else times = self.m_timer end local clickcost = v.cost[1] if #v.cost > 1 and v.curst <= #v.cost then clickcost = v.cost[v.curst] end game.fleetingLabelManager:pushLabel( "posX "..v.pos.x.." posY "..v.pos.y.." cost "..clickcost, self ) local redMoney = true if v.sys == SysType.ROCK and v.curst > 0 then redMoney = false end if userData.money < clickcost and redMoney then game.fleetingLabelManager:pushLabel( "金币不足", self, "error" ) return elseif redMoney then self:updateUserMoney(0 - clickcost) end local addnum = 0 if v.sys == SysType.AACT then self:updateUserActive(0, v.curst*5) v.curst = v.curst + 1 addnum = v.curst*5 elseif v.sys == SysType.AATK then self:addUserRecoverAtkDef(0, v.curst*5, 0) v.curst = v.curst + 1 addnum = v.curst*5 elseif v.sys == SysType.ADEF then self:addUserRecoverAtkDef(0, 0, v.curst*5) v.curst = v.curst + 1 addnum = v.curst*5 elseif v.sys == SysType.ADOT then self:addUserExp(0, v.curst*5) v.curst = v.curst + 1 addnum = v.curst*5 elseif v.sys == SysType.AKEY then if self:getRandomResult(1) then AddLabel( " 紫 ", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel+1, G_Font_Purple, ccp(0.5, 0.5) ) self:updateUserKey(0, 0, 1) elseif self:getRandomResult(10) then AddLabel( " 橙 ", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel+1, G_Font_Orange, ccp(0.5, 0.5) ) self:updateUserKey(0, 1, 0) else AddLabel( " 蓝 ", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel+1, G_Font_Blue, ccp(0.5, 0.5) ) self:updateUserKey(1, 0, 0) end elseif v.sys == SysType.ROCK then if v.curst == 0 then v.curst = v.curst + 1 end if haveRunAction then v.curst = v.curst + 1 sysItem:stopAllActions() haveRunAction = false end if v.curst < 4 then local array = CCArray:create() array:addObject(CCDelayTime:create(0.1)) array:addObject(CCCallFunc:create(function () v.result[v.curst] = v.result[v.curst] + 1 if v.result[v.curst] > #rockBonus then v.result[v.curst] = 1 end AddLabel( rockBonus[v.result[v.curst]], ccp(v.width/2 + (v.curst-2)*20, v.height/2), sysItem, Tag.SysNumLabel+v.curst, G_Font_Purple, ccp(0.5, 0.5) ) haveRunAction = true end)) if haveRunAction == false then sysItem:runAction(CCRepeatForever:create(CCSequence:create(array))) end else -- 进行结算 game.fleetingLabelManager:pushLabel( v.result[1].." "..v.result[2].." "..v.result[3], self, "error" ) if v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 1 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 2 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 3 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 4 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 5 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 6 then elseif v.result[1] == v.result[2] and v.result[2] == v.result[3] and v.result[3] == 7 then end v.curst = 0 v.result = {0,0,0} end end if #v.cost > 1 then if v.curst > #v.cost then AddLabel( "完", ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel, G_Font_Purple, ccp(0.5, 0.5) ) sysItem:removeChildByTag( Tag.SysCostLabel, true ) sysItem:setEnabled(false) return else AddLabel( "$"..v.cost[v.curst], ccp(5, v.height-10), sysItem, Tag.SysCostLabel, G_Font_Black_8, ccp(0, 0.5) ) AddLabel( "+"..addnum, ccp(v.width/2, v.height/2), sysItem, Tag.SysNumLabel, G_Font_Purple, ccp(0.5, 0.5) ) end end end) end -- 检查对应种类的钥匙是否足够,不足进行提示,足够扣除钥匙、更新数量并解锁 function MineSecretDialog:checkKeyEnough(keyId, needNum) local keyList = {"ironKey", "silverKey", "goldKey"} local keyStr = {"蓝钥匙", "橙钥匙", "紫钥匙"} local costNum = {0, 0, 0} keyId = keyId or 1 keyId = math.max(1, math.min(3, keyId)) needNum = needNum or 1 if (userData[keyList[keyId]] - needNum) >= 0 then costNum[keyId] = 0 - needNum self:updateUserKey(costNum[1], costNum[2], costNum[3]) return true else game.fleetingLabelManager:pushLabel( keyStr[keyId].."不足", self, "error" ) return false end end -- 随机生成传入概率数的随机结果 function MineSecretDialog:getRandomResult(percentage) math.randomseed(tostring(os.time()):reverse():sub(1, 6)) local random = math.random(1, 100) if random <= percentage then return true else return false end end -- 随机生成两个数之间的数 function MineSecretDialog:getRandomNum(from, to) math.randomseed(tostring(os.time()):reverse():sub(1, 6)) local random = math.random(from*10, to*10)/10 return random end function MineSecretDialog:onEnter() self.schedulerId = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(function() -- 恢复体力值 if userData.curActive < userData.maxActive then self:updateUserActive(1) end -- 恢复生命值 if userData.curHp < userData.maxHp then self:updateUserHp(math.floor(userData.recover/10)) end self.m_timer = self.m_timer + 0.2 end , 0.2, false) end function MineSecretDialog:onExit() CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.schedulerId) end function MineSecretDialog:onEnterTransitionDidFinish() end function MineSecretDialog:create() local instance = MineSecretDialog:new() instance.m_menu = nil instance.m_bgKuang = nil instance.m_expBar = nil instance.m_hpBar = nil instance.m_activeBar = nil instance.m_recoverBar = nil instance.m_atkBar = nil instance.m_defBar = nil instance.m_timer = 0 function instance.onNodeEvent(event) if event == 'enter' then instance:onEnter() elseif event == 'exit' then instance:onExit() elseif event == 'enterTransitionFinish' then instance:onEnterTransitionDidFinish() end end function instance.onAddHp() if userData.skillDot > 0 then instance:addUserExp(0, -1) instance:updateUserHp(0, 1) else game.fleetingLabelManager:pushLabel( "技能点不足", instance, "error" ) end end function instance.onAddActive() if userData.skillDot > 0 then instance:addUserExp(0, -1) instance:updateUserActive(0, 1) else game.fleetingLabelManager:pushLabel( "技能点不足", instance, "error" ) end end function instance.onAddRecover() if userData.skillDot > 0 then instance:addUserExp(0, -1) instance:addUserRecoverAtkDef(1, 0, 0) else game.fleetingLabelManager:pushLabel( "技能点不足", instance, "error" ) end end function instance.onAddAtk() if userData.skillDot > 0 then instance:addUserExp(0, -1) instance:addUserRecoverAtkDef(0, 1, 0) else game.fleetingLabelManager:pushLabel( "技能点不足", instance, "error" ) end end function instance.onAddDef() if userData.skillDot > 0 then instance:addUserExp(0, -1) instance:addUserRecoverAtkDef(0, 0, 1) else game.fleetingLabelManager:pushLabel( "技能点不足", instance, "error" ) end end function instance.onClose() instance:close(false) end -- 初始化 instance:init() return instance end return MineSecretDialog
PS:有些函数可能没有列全,请参考之前文章写的lua函数
标签:
原文地址:http://blog.csdn.net/zhenyu5211314/article/details/50999220