灵潭
2010-05-02 10:59:03
以下是npc自动行走函数(局部): 问题:多个npc时运行速度会很慢,请求指教:
核心原理:
当move==2时(NPC范围移动):
用随机取数,让npc自动选择运动方向,之后让npc按每步smallstep长度走长为steplong的一段距离,
然后休息长为stoptime的一段时间,休息完后重复以上动作......
变量说明:
pstop:主角运动情况(1值为不动,0值为动)
npcn:npc总数
npc[i]:第i个npc数组
具体:
npc[i]=
{
stop=(0值为npc不动,1值为npc动)
linekg=(npc路线开关,无视它)
direction=(npc运动方向:1,下移;2,左移;3,右移;4,上移)
p=(地图上现示的npc图片)
right1,right2,right3,left1,left2,left3,up1,up2,up3,down1,down2,down3=(地图上现示的每个方向的npc 3张图片,以达动态效果)
movemanwallkg=(npc路线挡路开关,无视它)
move=(NPC运动模式 0:NPC不动,1:NPC只动双脚,2:NPC范围移动)
tt=(NPC停止时的等待时间)
stoptime=(NPC停止时间1000=1秒)
count=(NPC时间计时器)
ran=(随机取数,让npc自动选择运动方向)
x,y=(NPC在地图上的坐标)
smallstep=(NPC移动速度)
steplong=(NPC一次运动的长度)
stepnow=(NPC已经运动的长度)
walktime=(借助主循环npc 3张图片变换计时器)
time1,time2,time3=(借助主循环npc 3张图片变换频率)
}
代码:(可以参考 掌家帖:<
---function npcmove()---
function npcmove()
if pstop==0 then
--------------------让npc两脚并拢----------------
for i=1,npcn do
if npc[i].stop ==1 and npc[i].linekg==0 then
if npc[i].direction==1 then npc[i].p=npc[i].down2 end
if npc[i].direction==2 then npc[i].p=npc[i].left2 end
if npc[i].direction==3 then npc[i].p=npc[i].right2 end
if npc[i].direction==4 then npc[i].p=npc[i].up2 end
end
--------------------让npc两脚并拢--完--------------
if npc[i].movemanwallkg==0 and npc[i].linekg==0 then
if npc[i].move == 2 then
npc[i].tt = npc[i].count:time()
if npc[i].tt > npc[i].stoptime +30 then
npc[i].count:reset(0) ---计时器复位
npc[i].count:start() ---计时器从起
end
if npc[i].tt > npc[i].stoptime and npc[i].tt < npc[i].stoptime+20 then
npc[i].ran = math.random()
npc[i].ran = npc[i].ran*10
if 0 < npc[i].ran and npc[i].ran < 2.5 then npc[i].direction = 1 end
if 2.5 < npc[i].ran and npc[i].ran < 5 then npc[i].direction = 2 end
if 5 < npc[i].ran and npc[i].ran < 7.5 then npc[i].direction = 3 end
if 7.5 < npc[i].ran and npc[i].ran < 10 then npc[i].direction = 4 end
npc[i].stop=0
end
end
if npc[i].move >0 then
if npc[i].move == 1 then npc[i].stop=0 end
if npc[i].stop== 0 and npc[i].linekg==0 then
if npc[i].tt > npc[i].stoptime+20 then
npc[i].count:stop()
end
if npc[i].direction == 1 then
if npc[i].move ==2 then
npc[i].y = npc[i].y + npc[i].smallstep
end
npc[i].walktime = npc[i].walktime + 1
if npc[i].walktime >= npc[i].time1 and npc[i].walktime <= npc[i].time2 then npc[i].p = npc[i].down1 end
if npc[i].walktime >= npc[i].time2 and npc[i].walktime <= npc[i].time3 then npc[i].p = npc[i].down3 end
if npc[i].walktime >= npc[i].time3 then npc[i].walktime=1 end
end
if npc[i].direction == 2 then
if npc[i].move ==2 then
npc[i].x = npc[i].x - npc[i].smallstep
end
npc[i].walktime = npc[i].walktime + 1
if npc[i].walktime >= npc[i].time1 and npc[i].walktime <= npc[i].time2 then npc[i].p = npc[i].left1 end
if npc[i].walktime >= npc[i].time2 and npc[i].walktime <= npc[i].time3 then npc[i].p = npc[i].left3 end
if npc[i].walktime >= npc[i].time3 then npc[i].walktime=1 end
end
if npc[i].direction == 3 then
if npc[i].move ==2 then
npc[i].x = npc[i].x + npc[i].smallstep
end
npc[i].walktime = npc[i].walktime + 1
if npc[i].walktime >= npc[i].time1 and npc[i].walktime <= npc[i].time2 then npc[i].p = npc[i].right1 end
if npc[i].walktime >= npc[i].time2 and npc[i].walktime <= npc[i].time3 then npc[i].p = npc[i].right3 end
if npc[i].walktime >= npc[i].time3 then npc[i].walktime=1 end
end
if npc[i].direction == 4 then
if npc[i].move ==2 then
npc[i].y = npc[i].y - npc[i].smallstep
end
npc[i].walktime = npc[i].walktime + 1
if npc[i].walktime >= npc[i].time1 and npc[i].walktime <= npc[i].time2 then npc[i].p = npc[i].up1 end
if npc[i].walktime >= npc[i].time2 and npc[i].walktime <= npc[i].time3 then npc[i].p = npc[i].up3 end
if npc[i].walktime >= npc[i].time3 then npc[i].walktime=1 end
end
npc[i].stepnow = npc[i].stepnow + npc[i].smallstep
end
end
if npc[i].move == 2 then
if npc[i].stepnow > npc[i].steplong then
npc[i].stepnow =0
npc[i].stop =1
npc[i].count:reset(0)
npc[i].count:start()
end
end
end
end
end
end
---function npcmove() end---
1239985932
2010-05-02 20:07:02
可以的话,下个周末专门请教你去
love_xiaolu
2010-05-07 06:13:52
大大果然很强大,