DIRECTOR 倒计时器代码

本文提供了一个简易倒计时器的代码实例,包括文本精灵和开始计时按钮的代码,适用于教学课件和游戏开发。文章详细解析了代码逻辑及关键点,如计时结束处理和getPropertyDescriptionList的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在教学课件和游戏中,经常用到倒计时器代码,发个简单的例子给大家,仅供参考:

说明:

   1 本计时器可以设定计时时长,并在设定的警示时间内闪烁提示。

   2 为了让初学者很快明白,本代码未做优化

   3 本例中有两个重要的点,请思考,这两处对大家以后的开发益处多多

     重点一 计时结束后的 stime=0

     重点二 getPropertyDescriptionList的使用

 

一 文本(显示时间)精灵的代码

property stime
property timelong
property alertlong
property pblend
on beginsprite me
  stime=0 
  sprite(me.spritenum).member.text=string(timelong)
end

on showtime
  stime=the timer
end


on exitFrame me
  if stime<>0 then
    temp=timelong-(the timer-stime)/60
    if temp>0 then
      if temp<alertlong then
        if sprite(me.spritenum).blend=20 then
          pblend=5
        end if
        if sprite(me.spritenum).blend=100 then
          pblend=-5
        end if
        sprite(me.spritenum).blend=sprite(me.spritenum).blend+pblend
      end if
      member("time").text=string(temp)
    else
      member("time").text="0"
      stime=0
    end if
  end if
end

on getPropertyDescriptionList
  description = [:]
  description.addProp(#timelong, [#default:1, #format:#integer, #comment:"时长"])
  description.addProp(#alertlong, [#default:1, #format:#integer, #comment:"警告"])
  return description
end

二、开始计时按钮代码

on mouseup
  sendsprite(sprite(1),#showtime)
end

 

释义:

   点击开始即使按钮后,通过sendsprite发送给计时器开始计时的命令。

    注意:这里用到了sprite(1),实际应用中,建议大家用精灵通道名称指定精灵,而不是通道号,防止后期编辑中通道号变化引起程序错误。

   本例中对于初学者难度在文本精灵中。

  文本精灵接收到按钮send过来触发showtime时间后,给stime变量赋值为the timer,记录当前时间

  the timer是DIRECTOR中的一个时间命令,单位为1/60秒

  三 主要显示代码在EXITFRAME中,下面分句解释:

  if stime<>0 then  //begin中给stime复制为0,showtime事件中,此变量赋值为起始时间,这里的判断作用是是否开始计时
    temp=timelong-(the timer-stime)/60 //倒计时计算,简单的数学计算
    if temp>0 then //判断倒计时是否结束
      if temp<alertlong then//判断是否到了警示时间

        //下面代码通过改变精灵的blend值,实现计时器闪烁
        if sprite(me.spritenum).blend=20 then
          pblend=5
        end if
        if sprite(me.spritenum).blend=100 then
          pblend=-5
        end if
        sprite(me.spritenum).blend=sprite(me.spritenum).blend+pblend
      end if

    //显示时间    

     member("time").text=string(temp)
    else

     //计时结束
      member("time").text="0"
      stime=0//注意这里,这行代码加与不加,程序都能正常运行,请大家自己思考一下,为什么要加这个。
    end if
  end if

 

四 关于值的设定

   本例中用到了getPropertyDescriptionList,该命令对于大部分D程序员比较陌生,建议大家一定要搞懂这个命令,受益无穷。通过这里的解释,可能无法给彻底将明白。简单提示几点:

   1 用法

    当带有改事件的脚本加载到精灵上后,可以通过精灵属性面板的behavior选项目面板中看到设定的几个属性,比如本例中共设定了两个值,那看到的属性即为时长和警告两个,在这里我们可以设定这两个变量的值。

   2 好处

   假设我们的程序有两个计时器,一个是100秒倒计时,一个30秒倒计时,常规处理办法有两个,一是写两个倒计时代码,二是把alertlong设置为全局变量,单击开始计时按钮的时候,改变这个变量的值。这两个方法,实际上都是不科学的,方法一,增加了代码工作量,不便于代码维护修改,方法二增加全局变量的个数,这个大家都懂。

   如果我们使用了getPropertyDescriptionList来设置,你只要把第一个计时器的精灵拷贝到第二个要出现的位置,在属性面板中修改相应的参数,这样第二个、第三个、第N个都是可以的,不增加代码、不增加变量。

   关于getPropertyDescriptionList的具体用法,请大家先看看帮助,然后在实际开发中多想、多试

把关卡的初始化解耦出来,只要输入当前的关卡树为参数,就能根据每个关卡的设置初始化关卡,以下是每个关卡的设置情况:第一关,在Canvas/Play/shelfs/shelfPos2生成货架,货物种类总共有2种,初始时最上层空格数是3,货物总共有1层,倒计时是60s;第二关,在Canvas/Play/shelfs/shelfPos2生成货架,货物种类总共有4种,初始时最上层空格数是3,货物总共有2层,倒计时是60s;第三关,在Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3生成货架,货物种类总共有8种,初始时最上层空格数是6,货物总共有2层,倒计时是120s;第四关,在Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3生成货架,货物种类总共有12种,初始时最上层空格数是6,货物总共有3层,倒计时是120s;第五关,在Canvas/Play/shelfs/shelfPos1和Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3生成货架,货物种类总共有18种,初始时最上层空格数是9,货物总共有3层,倒计时是180s;第六关,在Canvas/Play/shelfs/shelfPos1和Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3生成货架,货物种类总共有24种,初始时最上层空格数是9,货物总共有4层,倒计时是180s;第七关,在Canvas/Play/shelfs/shelfPos1和Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3和Canvas/Play/shelfs/shelfPos4生成货架,货物种类总共有32种,初始时最上层空格数是12,货物总共有4层,倒计时是240s;第八关,在Canvas/Play/shelfs/shelfPos1和Canvas/Play/shelfs/shelfPos2和Canvas/Play/shelfs/shelfPos3和Canvas/Play/shelfs/shelfPos4生成货架,货物种类总共有40种,初始时最上层空格数是12,货物总共有5层,倒计时是240s;
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值