在优化程序的时候要考虑一些常用的问题,解决这些问题的关键是要做些简易的测试,了解他们的性能与效率。
我常常看到,有这样的代码:
on exitframe me
sprite(1).loch= sprite(1).loch+1
end
很显然这是有问题的,就是效率较低。
通用的改进:
property pSprite
on beginsprite me
pSprite=sprite(me.spritenum)
end
on exitframe me
pSprite.loch = pSprite.loch +1
end
好像没有问题,再变通一下:
property pSprite
on beginsprite me
pSprite=sprite(me.spritenum)
end
on exitframe me
pSprite.loch = pSprite.loch +1
if (pSprite.loch>=the stageright)then pSprite.loch=thestageright
end
这段代码完成需要解决的任务,但我还是认为效率低。
关键出在pSprite.loch = pSprite.loch +1 上,如果没有下面的判断是可以的,但加了判断执行的效率就值得注意。应改成:
property pSprite
on beginsprite me
pSprite=sprite(me.spritenum)
end
on exitframe me
xLoch=pSprite.loch ***
xLoch=xLoch+1 ***
if (xLoch>=the stageright)then pSprite.loch=the stageright ***
pSprite.loch = xLoch
end
经过测试效率提高了,如果把the stageright也设置成变量:
property pSprite
property pStageRight
on beginsprite me
pSprite=sprite(me.spritenum)
pStageRight= the stageright
end
on exitframe me
xLoch=pSprite.loch ***
xLoch=xLoch+1 ***
if (xLoch>= pStageRight)then pSprite.loch= pStageRight ***
pSprite.loch = xLoch
end
看来代码无懈可击,但我始终不满意。于是作了大量的测试,改成了如下:
property pSprite
property pStageRight
property pLoch
on beginsprite me
pSprite=sprite(me.spritenum)
pStageRight= the stageright
pLoch=pSprite.loch ***
end
on prepareframe me
xLoch=xLoch+1 ***
if (xLoch>= pStageRight)then pSprite.loch= pStageRight ***
pSprite.loch = xLoch
end
只是一个小小改变,但花了我半天的测试,于是我迅速用进我的潜艇战中的优化。结果我惊喜我的运行效率提高了(因为有大量物体运动),当然改进不止这些。
希望能提高大家对程序优化的积极性。只要对速度不满意就想办法优化你的程序。
小议Lingo代码优化
发布: 2007-1-25 16:58 | 作者: cjx2000 | 来源: aougu.com | 查看: 220次
