xinranok
 
 
 
 
 

xidelongok 最新的 20 条日志

 
 
对象的properties [2006-2-6]
xidelongok 发表在 学习笔记
一、Object properties 就像属性列表

-->>>>>>>parent script code(nmae "simpleobj")

property thisOne, another, yetAnother
on new me
  set thisOne to 1
  set another to "2"
  set yetAnother to "garfle"
  return me
  end new
-------<<<<<<<<<<<<<<<<
Now open the Message window,Create a new instance of the object 
m= new (script"simpleObj")
put m
-- <offspring "simpleObj" 2 520ca50>
put m.thisone
-- 1
put m.another
-- "2"
put m.count
-- 3
put m[1]
-- 1
put m[2]
-- "2"
put m[3]
-- "garfle"
奇怪的是,下面不符合属性列表规则的句法,居然也能行得通
put getprop(m,3)
-- "garfle"
如果和属性列表对照一下 ,就更清楚了:
例如:
n=[#a:10, #b:12, #c:15, #d:22]
put getProp(n, 15)---错误句法
put getpropat(n,#c)
--15
不管Lingo dictionary怎么说,过去的经验告诉我们Object properties 就像属性列表
------------------------------------------

二、利用这一点,我们很容易创建类似的属性列表

-->>>>>>>>>>
on display scriptinstance
k=scriptinstance
  set x to count (k)
  repeat with index = 1 to x    
    set propName to getPropAt(k, index)
    set propValue to getaProp(k, propName)
    put propName & ":" && propValue
  end repeat

end display

----------------
重新为刚才的脚本命名,并播放。输入:
n=new(script"foo")

display(n)
-- "thisone: 1"
-- "another: 2"
-- "yetanother: garfle"

--------------------------------------------------------
三、运行期间观察属性变量--削球移动的例子
命名:"moveHorz"
---------------

property locX, locY, direction, mySprite 

on new me, aSprite
  
  set mySprite to aSprite
  set locX to the locH of sprite mySprite
  set locY to the locH of sprite mySprite
  set direction to 1 --forward in this case
  add (the actorList, me)
  
  return me
  
end new


on stepFrame me
  
  case (direction) of
    1:
      if locX + direction >300 then set direction     to direction * -1
    -1:
      if locX + direction < 50 then set direction     to direction * -1
  end case
  
  set locX to locX + direction
  
  set the loc of sprite mySprite to point(locX, locY)
  updateStage
  
end stepframe 


on display me
  
  put "Me" && me
  repeat with index = 1 to count(me)
    set propName to getPropAt(me, index)
    set propValue to getaProp(me, propName)
    put propName && ":" && propValue
  end repeat
  
end display

 -------------
四、修改对象的属性
我们已经创建了刚才的movehorz对象,再次运行她。如果你还没有清除the actorList ,那么,小球实例将不得不飞到最,后到达的位置。如果已经清除了,那么,你还要在建立这个对象。
在message window 输入:
put getprop(b,#direction)
目的是查看小球的方向(1  or -1)
改变方向,输入:
setprop(b,#direction,1)
或者使用如下句法:
b.locx=256
b.direction=-1
五、Adding ancestors

重新建福脚本对象 "simpleobj"--如上

--parent script code(nmae "simpleobj")

property thisOne, another, yetAnother
on new me
  set thisOne to 1
  set another to "2"
  set yetAnother to "garfle"
  return me
  end new
--修改display()添加后面几行,完整脚本:
on display me
  
  put "Me" && me
  repeat with index = 1 to count(me)
    set propName to getPropAt(me, index)
    set propValue to getaProp(me, propName)
    put propName && ":" && propValue
  end repeat
  
  if objectP (getaProp (me, #ancestor)) then
    put "Object's Ancestor:"
    display the ancestor of me
  end if
  
end display
--------------------------
运行:
x=new(script("simpleobj"))
display(x)
-- "Me <offspring "m" 3 520b1a0>"
-- "thisone : 1"
-- "another : 2"
-- "yetanother : garfle"
***************

现在,我要为对象添加新的属性,而不是仅仅更改属性的值
 setProp (x, #newProp, "Wow!") 
 display x
-- "Me <offspring "m" 3 520b1a0>"
-- "thisone : 1"
-- "another : 2"
-- "yetanother : garfle"
-- "newprop : Wow!"
************************
在display()方法中,如果对象由祖先脚本,那么就显示祖先脚本的之
下面,创建虚假的对象。Create a new script member and enter the following code.Save this script as "kiddie" 
--->>>>>>>>>>>>>>
property first, second, third

on new me
  
  set first to 99
  set second to "Q"
  set third to "BooBerry"
  
  return me
  
end new

--<<<<<<<<<<<<<<<<<<<<
输入:

setprop(x,#Ancestor,new(script"kiddie"))
display(x)
-- "Me <offspring "x" 3 3f7310c>"
-- "thisone : 1"
-- "another : 2"
-- "yetanother : garfle"
-- "newprop : Wow!"
-- "ancestor : <offspring "kiddie" 3 520b1a0>"
-- "Object's Ancestor:"
-- "Me <offspring "keddie" 4 520b1a0>"
-- "first : 99"
-- "third : BooBerry"
-- "second : Q"

≡≡≡≡≡ 评论(共 条)我要评论
 
Power by 5D Design by wolfprince