使Director创作过程自动化

来源:网络时间:2011-04-28 13:01:07

  很多人都没有觉察到在工作中用Lingo去自动操作一些冗长乏味的任务能够为他们省下多少时间。为CAST成员重命名,调整物体大小,更改文本和剪切位图是一些我们不需要花一点脑细胞但每天不得不用大量时间去做的任务。写一个快速创造的脚本能节省很多工作。我将给你们看如何写这些简单的脚本和有用的Lingo命令。我们也简要的看看Score记录,另一个非常大的特点,它能使你通过Lingo去做创造来改变Score。
  
  基础

  让我们从简单的事情开始。我们假设 你的基本按钮的行为被设为:在普通状态为''PLAY'',当rollover时为''play roll'',当按下时的状态为''play down'',但是你的设计师给了你300个以"play", "play_roll" 和"play_down" 为模型命名的新按钮文件。你可以改变属性,但在电影中已经有了大量的其他按钮,而且你也不想冒破坏现有代码的危险,所以你决定把新按钮都改名以适应你计划。手工去做这个简直就是一场恶梦,所以通过Lingo去做似乎就像一个理想的瞬间。
  
  让我们在一个脚本里做第一次偿试。
  
on renamebuttons
  repeat with j = 1 to the number of castlibs
    repeat with i = 1 to the number of members of castlib j
      mem=member (i, j)
      nm=mem.name
      repeat with k = 1 to nm.length
        if nm.char[k] = ''_'' then put '' '' into nm.char[k]
      end repeat
      mem.name=nm
    end repeat
  end repeat
end
  
  一旦你在电影脚本中进入了这个程序,你所要做的只是打开信息窗口并输入:
  
  renamebuttons
  
它会很相当清楚的知道它要做什么。它通过循环检查CASt(演员)库中的的每一个演员,检查每一个CAST成员的名字看是否包含有 一个underscores,如果有替换成一个空格。
  
  现在,它工作得很好,但是有一些危险存在。这其中最重要的便是在你的cast 库中如果有任何其他的cast成员碰巧也有underscores,那么,他们也会被改名。这可能是个大问题。所以相应的,我们要限定自己以操作那些表中被我们选中的CAST成员。我们也将会做有效的小的方面的名字改动通过offset function(移位函数)功能,(但不会是很大的变动的)
  
  要想在单个已选择的CAST成员上操作,我们能利用cast库的选择特性。这个返回一个目录:[[1,4],[6,8]] 的意思是当前的第1,2,3,4,6,7,8CAST成员被选中。只从安全的角度来讲,我会做一个功能用来只对一个CAST库中操作。这么做是因为可能在其它的CAST库中有被选中的CAST


  成员而你却并不感兴趣。这里是新的处理方法:
  
 on renamebuttonsbylib lib
   
  if voidp (lib) then lib= the activecastlib
  s=castlib (lib).selection
  repeat with lyst in s
    repeat with i = lyst[1] to lyst[2]
      mem = member (i, lib)
      nm = mem.name
      off = offset (''_'', nm)
      if off <> 0 then put '' '' into nm.char[off]
      mem.name=nm
    end repeat
  end repeat
    
end
  
  运行这个,你需要传给它一个CAST库的名字或是编号,例如:
  
  renamebuttonsbylib "buttons"
  
  这个脚本运行时通过库中的所有的已选择的CAST成员并且根据需要来更改名字。你也应该注意到,我包含了一条使用缺省activecastlib(动态成员库)变量的线,这就是说如果你不传给计算表一个参考,它将默认的寻找最近的选择。
  这个基本的技术使你可以自动操作任何大量的不同任务。这里有一个使用位图Lingo把所有32位的位图转换成16位的例子,保留了注册点:
  
  on transformbitmaps lib
  
  if voidp (lib) then lib= the activecastlib
  s=castlib (lib).selection
  repeat with lyst in s
    repeat with i = lyst[1] to lyst[2]
      mem = member (i, lib)
      if mem.type <> #bitmap then next repeat
      im = mem.image
      if im.depth = 32 then
        im2=image (im.width, im.height, 16)
        im2.copypixels (im, im.rect, im.rect)
        reg = mem.regpoint
        mem.image = im2
        mem.regpoint = reg
      end if
    end repeat
  end repeat
  
end

  附带的说一句,这个脚本用了member.type 属性来检查某成分是否被看成是一个位图。这样的检查通常很有用。这里是这种模型的最后一个例子:写一个程序去转换你所有的文本text成员到field成员, 保留基本的格式。这里用一些新的命令创建一个全新的计算成分,一项非常强大的技术。

 

脚本里也包含了两个附加的选项:是否保留一份原始成分的副本,和新的域是否应该和原始成分放在同一计算成分位置里。这两项默认为是。
  
  on converttofield lib, retain, sameplace
  
  if voidp (lib) then lib = the activecastlib
  if voidp (retain) then retain = 1
  if voidp (sameplace) then sameplace = 1
  s=castlib (lib).selection
  repeat with lyst in s
    repeat with i = lyst[1] to lyst[2]
      mem=member (i, lib)
      if mem.type <> #text then next repeat
      if sameplace then
        newmem = new (#text)
        newmem.media = mem.media
        newmem.name = mem.name
        erase mem
        mem=newmem
        f=new (#field, member (i, lib))
      else
        f = new (#field)
      end if
      f.text = mem.text
      f.rect=mem.rect
      f.alignment = string (mem.alignment)
      f.name = mem.name
      repeat with k = 1 to mem.text.length
        the font of char k of field f = mem.char[k].font
        the fontsize of char k of field f = mem.char[k].fontsize
        s = mem.char[k].fontstyle
        tx = ''''
        repeat with sym in s
          tx = tx & sym & '',''
        end repeat
        if tx.length > 0 then
          delete tx.char[tx.length]
          the fontstyle of char k of field f = tx
        end if
        
      end repeat
      if not retain then erase mem
    end repeat
  end repeat
  
end

  
  选择你感兴趣的CAST成员不是进行复杂搜索的唯一方法,顺便一提-Lingo 存诸了CAST成员的所有工具。你可以用creationDate 属性去改变上个星期才输入的CAST成员,或是用modifiedBy 属性去改变那些还没有被修改过的CAST。

#p#副标题#e#

  快速绘图

  创作脚本不仅仅在适应或改变现有的ASSETS上有作用。你也可以用它们创建图形。最近,我在一个教育站点从事包括大量几何图形的数学的工作,而且很快我便厌烦了用Photoshop 创建图形。所以我创建了一些程序使我被允许在Director 中画几何图形。这里是一个例子:
 
on drawsquare w, nm, col, bg, s
 
 v=new (#vectorshape)
 v.vertexlist = [[#vertex: point (0, 0)], [#vertex: point (0, w)], [#vertex: point
 (w, w)],
 [#vertex: point (w, 0)]]
 if voidp (bg) then bg = rgb (255, 255, 255)
 if voidp (col) then col = rgb (0, 0, 0)
 if voidp (s) then s = 1
 v.backgroundcolor = bg
 v.strokecolor = col
 v.strokewidth = s
 v.closed = 1
 v.fillmode = #none
 mem = new (#bitmap)
 mem.image = v.image
 mem.name = string (nm)
 erase v
 
end
   
  这个例子相当的简单,但是你可以用这个程序很长时间。那么,这个怎么样:一种画曲径的方法。我们用这种形式的数据清单供给它:[[1,2],[0,1]]。每一个数字代表曲径里一个单一的正方形,and has a value representing whether there is a wall to the right or to the
  right or to the bottom of that square.
  因此:
  
  0 = exit at bottom and right
  
  1 = exit at bottom
  
  2 = exit at right
  
  3 = no exit to bottom or right
  
  我们不必担心会离开当前命令行外壳到顶部或是左边,因为那是多余的,并且我们假定曲径在每
  一边都是关闭的。我们可以开始画了。
  
on drawmaze m, w
  if voidp (w) then w = 20
  vert = m.count
  hor = m[1].count
  maze = image (w * hor + 1, w * vert + 1, 16)
  repeat with i = 1 to vert
    repeat with j = 1 to hor
      sq = m[ i ][j]
      if sq mod 2 or i = vert then maze.draw ((j - 1) * w, i * w, j * w, i * w + 1,
      rgb (0, 0, 0))
      if sq > 1 or j = hor then maze.draw (j * w, (i - 1) * w, j * w + 1, i * w, rgb
      (0, 0, 0))
    end repeat
  end repeat
  maze.draw (0, 0, 1, w * vert, rgb (0, 0, 0))
  maze.draw (0, 0, w * hor, 1, rgb (0, 0, 0))
  
  mem = new (#bitmap)
  mem.image = maze
  
  
end
 
  试着做-在信窗口里写入:
  
  drawmaze ([[1,0,3,0],[0,3,0,0],[0,0,2,0]])
  
  检查你的内在成员Internal cast且一个新的曲径已经添加了。顺便提一下,这项技术不仅仅用于创作-它也可以用于运行时间,来保存文件大小。但是不要忘记你在运行时间里添加的每一个新的cast成员在被擦除之前都会在内存中,所以它可能在执行时产生影响。请确信在用完你的CAST成员后清除它们。

#p#副标题#e#

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:rjfawu@163.com