# 学习案例
示例1:
游戏开始,角色附近每隔10秒随机刷新一批怪物,怪物生存时间是10秒,玩家需要击杀10只野人,则游戏胜利,弹出胜利界面
local ActorId = 3102
local obj_ids = {} --怪物id表
local def_count = 0 --击败次数
local mob_count = 5 --怪物数量
local function SpawnMonsters()
local ret, x, y, z = Player:getPosition()
for idx = 1, mob_count do --生成怪物ID
local newX = math.random(-5, 5) + x
local newZ = math.random(-5, 5) + z
local ret, objids = World:spawnCreature(newX, y, newZ, ActorId)
if ret == ErrorCode.OK then
obj_ids[#obj_ids+1] = objids[1]
obj_ids[#obj_ids+1] = objids[2]
end
end
end
local function ClearMonsters()
if #obj_ids == 0 then return end
for idx = 1, #obj_ids do
World:despawnCreature(obj_ids[idx])
end
obj_ids = {}--将ID表清空
end
Game_Start = function()
local ret, timerId = MiniTimer:createTimer("计时X")
if ret == ErrorCode.OK then --启动计时器
MiniTimer:startForwardTimer(timerId)
SpawnMonsters()
end
end
Timer_Change = function(event_args)
local timerid = event_args['timerid']
local ret, secs = MiniTimer:getTimerTime(timerid)
if ret == ErrorCode.OK then
if math.fmod(secs, 10) == 0 then
ClearMonsters() --每隔10秒刷新下怪物
SpawnMonsters()
end
end
end
Defeat_Actor = function(event_args)
local toobjid = event_args['toobjid'] --被击败的目标ID
local playerId = event_args['eventobjid']--玩家ID
local ret, actorId = Creature:getActorID(toobjid)
if ret==ErrorCode.OK and actorId==ActorId then
def_count = def_count + 1
Chat:sendSystemMsg("杀怪数量:"..def_count)
if def_count == 10 then
local win = TEAM_RESULTS.TEAM_RESULTS_WIN
local result = Player:setGameResults(playerId, win)
if result == ErrorCode.OK then
Game:doGameEnd() --游戏结束
end
end
end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)
ScriptSupportEvent:registerEvent([=[minitimer.change]=], Timer_Change)
ScriptSupportEvent:registerEvent([=[Player.DefeatActor]=], Defeat_Actor)
示例2:
游戏开始后,玩家每走一步,就在自己的右前方放置一个石块(右侧或者旁边,很直观的能看到)
Player_MoveStep = function(event_args)
local playerId = event_args['eventobjid']
local ret, x, y, z = Player:getPosition()
local ret, curdir = Player:getCurPlaceDir(playerId)
if curdir == FACE_DIRECTION.DIR_NEG_X then x = x+2; z = z+2 end
if curdir == FACE_DIRECTION.DIR_POS_X then x = x-2; z = z-2 end
if curdir == FACE_DIRECTION.DIR_NEG_Z then z = z+2; x = x+2 end
if curdir == FACE_DIRECTION.DIR_POS_Z then z = z-2; x = x-2 end
Block:replaceBlock(104, x, y, z)
end
ScriptSupportEvent:registerEvent([=[Player.MoveOneBlockSize]=], Player_MoveStep)
示例3:
在场景中放置开关和灯,当玩家挖掉10个土块,电灯亮起
local x, y, z = 5, 7, 5 --信号灯的位置
Game_Start = function()
-- 放置开关和信号灯
local palyer_1 = Player:getHostUin()
Player:setPosition(palyer_1, 0, 7, 0)
local switchId, lightId = 724, 707
Block:replaceBlock(lightId, x, y, z)
Block:replaceBlock(switchId, x, y+1, z, 4)
for idx= 1, 10 do -- 放置草块
Block:replaceBlock(100, x+idx, y, z)
end
end
local remove_count = 0
Block_Removed = function(event_args)
local blockid = event_args['blockid']
if blockid == 100 then --草块被破坏
remove_count = remove_count + 1
if remove_count == 10 then --将开关打开
Block:setBlockSwitchStatus({x=x,y=y+1,z=z}, true)
end
end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block_Removed)