道具百科
触发器百科
脚本API百科
创作手册
创作功能
其他
Studio百科
综合案例:可以坐上去的沙发
彼得兔 更新时间: 2022-08-18 18:10:26

 

游戏内的效果:

原理:

利用Player:mountActor(...)接口,可以让玩家骑乘生物;利用微缩和插件做一个沙发模型的生物,然后禁止移动和攻击,即可实现。

 

教程:

第一步,做一个沙发微缩方块:

 

第二步,利用生物模型方块或模型编辑器制作一个沙发外观的生物:

 

第三步,新建自定义生物插件,模型选刚刚制作的沙发生物模型:

 

第四步:打开脚本编辑器,点击ID库查询新建的沙发生物的生物id:

 

第五步,新建脚本,复制以下内容粘贴过去,注意id改为自己存档中的沙发id:

local sofaid=3--注意这里改为从ID库里查询到的沙发生物id
local function Create(e)
    local objid=e.eventobjid
	local result,actorid=Creature:getActorID(objid)
	if actorid==sofaid then
		Actor:setActionAttrState(objid,1,false)
		Actor:setActionAttrState(objid,32,false)
		Actor:setActionAttrState(objid,64,false)
		Actor:setActionAttrState(objid,128,false)
		Actor:setActionAttrState(objid,256,false)
		Creature:setAIActive(objid,false)
	end
end
ScriptSupportEvent:registerEvent('Actor.Create',Create)
local function ClickActor(e)
	local playerid,objid=e.eventobjid,e.toobjid
	local result,actorid=Creature:getActorID(objid)
	if actorid==sofaid then
		Player:mountActor(playerid, objid, 1)
	end
end
ScriptSupportEvent:registerEvent('Player.ClickActor',ClickActor)
local function InputKeyDown(e)
	local playerid,key=e.eventobjid,e.vkey
	if key=="SHIFT" then
		Player:mountActor(playerid)
	end
end
ScriptSupportEvent:registerEvent('Player.InputKeyDown',InputKeyDown)

 

现在已经基本完成了,转成玩法测试一下:

在玩法模式,放置沙发生物蛋到地图上,点击沙发即可坐上去;按下Shift或潜行键即可脱离。

但有个小问题,摆放出来的沙发朝向是随机的,能不能自己设定呢?

可以利用Actor里的转向接口:

local sofaid,turnid=3,12054--3要改为沙发id,12054是小雪球id,可以根据需要自行更改
local function Create(e)
    local objid=e.eventobjid
	local result,actorid=Creature:getActorID(objid)
	if actorid==sofaid then
		Actor:setActionAttrState(objid,1,false)
		Actor:setActionAttrState(objid,32,false)
		Actor:setActionAttrState(objid,64,false)
		Actor:setActionAttrState(objid,128,false)
		Actor:setActionAttrState(objid,256,false)
		Creature:setAIActive(objid,false)
		Actor:setFaceYaw(objid,0)
	end
end
ScriptSupportEvent:registerEvent('Actor.Create',Create)
local function ClickActor(e)
	local playerid,objid=e.eventobjid,e.toobjid
	local result,actorid=Creature:getActorID(objid)
	if actorid==sofaid then
		Player:mountActor(playerid, objid, 1)
	end
end
ScriptSupportEvent:registerEvent('Player.ClickActor',ClickActor)
local function InputKeyDown(e)
	local playerid,key=e.eventobjid,e.vkey
	if key=="SHIFT" then
		Player:mountActor(playerid)
	end
end
ScriptSupportEvent:registerEvent('Player.InputKeyDown',InputKeyDown)
local function Hit(e)
	local itemid,objid=e.itemid,e.toobjid
	if objid~=nil and itemid==turnid then
		local result,actorid=Creature:getActorID(objid)
		if actorid==sofaid then
			Actor:turnFaceYaw(objid,45)
		end
	end
end
ScriptSupportEvent:registerEvent('Actor.Projectile.Hit',Hit)

复制上面的脚本替换过去,再转成玩法,放置沙发,可以发现所有沙发的朝向都是固定的了。

那么想让它们换个方向怎么办呢?用小雪球(12054)砸沙发即可。小雪球击中沙发后会执行Actor:turnFaceYaw(objid,45),意思是让被击中的生物转向45度,看看效果:

 

这样一个可以坐的沙发就做好了。不过这样的沙发在玩法模式是无法被清除的,因为在沙发创建的时候执行了以下代码:

Actor:setActionAttrState(objid,1,false)
Actor:setActionAttrState(objid,32,false)
Actor:setActionAttrState(objid,64,false)
Actor:setActionAttrState(objid,128,false)
Actor:setActionAttrState(objid,256,false)

查一下接口和常量可以发现

也就是沙发不可移动、不可攻击、不可被攻击、不可被杀死、不可拾取道具。因为不可被攻击,所以玩法模式下没办法清除掉沙发。解决方案有两个,一个是把“不可被攻击、不可被杀死”两条删除掉,另一个办法就是再添加一个投掷物,当沙发被这个投掷物击中时调用接口World:despawnCreature(...)来移除沙发。小伙伴们可以自己试一下~

站点有内容要更新是否更新。
更新
忽略