【问题标题】:Random number in Actionscript 3Actionscript 3 中的随机数
【发布时间】:2011-06-14 03:12:21
【问题描述】:

我一直在关注为 Actionscript 2 编写的教程,并已成功将其转换为 AS 3,但是在倒数第二部分我被卡住了。

这里的教程(http://www.cleverpig.com/tutorials/whackapig/whack.htm step 8)有以下代码:

if (_currentframe==1) {
    // randomly choose whether or not to play
    if (random(100)>97) {
      // should we tease or popup?
      if (random(3)<1) {
        this.gotoAndPlay ("popup");
      } else {
        this.gotoAndPlay (1);
      }
    }
  }

它旨在为角色的移动添加一些随机性。经过一番谷歌搜索后,我在 AS 3 中创建了这段代码,希望它能正常工作。

if (currentFrame==1) {
    // randomly choose whether or not to play
    if(Math.floor(Math.random()*99)-97) {
      // should we tease or popup?
      if (Math.floor(Math.random() *3)-1)
   ) {
        this.gotoAndPlay ("popup");
      } else {
        this.gotoAndPlay (1);
      }
    }
  }

当我使用此代码运行程序时,角色的整个动画播放一次(向下、半向上、向上、撞击)。它应该只播放前 3 帧,然后重复。

编辑:

function random (n:int ) : int {
    return Math.floor (Math.random() * n);
}

if (currentFrame==1) {
    // randomly choose whether or not to play
    if(random(100)): 97 {
      // should we tease or popup?
      if (random(3)): 1
      {
        this.gotoAndPlay ("popup");
      } else {
        this.gotoAndPlay (1);
      }
    }
  }

符号“洞”,层“Actionscript”,第 1 帧,第 10 行 1084:语法错误:需要冒号前的标识符。 符号 'hole',图层 'Actionscript',第 1 帧,第 10 行 1008:属性无效。 符号 'hole',图层 'Actionscript',第 1 帧,第 12 行 1084:语法错误:在冒号之前需要标识符。 符号 'hole',图层 'Actionscript',第 1 帧,第 13 行 1008:属性无效。 符号 'hole',图层 'Actionscript',第 1 帧,第 15 行 1083:语法错误:否则是意外的。

【问题讨论】:

    标签: flash actionscript-3 math actionscript random


    【解决方案1】:

    我会给原始教程的作者一个 -1,将一个数字随机表达式放在一个类似的条件中。

    要确定条件表达式的结果,它总是被评估为布尔值。如果你把一个 Number/int 放在那里,只有当它的值为 0(.0) 时它才会是假的。在所有其他情况下(包括负数),它将评估为 true。

    是的,您可以按照@weltraumpirat 的回答制作一个更简单的 random() 函数,但我宁愿建议您将 random() 函数从条件中取出,并将其包装在一个 chance() 方法中。例如:

    function feelingLucky(ratio:Number):Boolean {
        return Math.random() <= ratio;
    }
    

    然后调用此方法将为您提供更好的 x out of y 语法,例如:

    if (feelingLucky(97/99)) {
        // you're lucky.
    } else {
        // not so lucky.
    }
    

    或者你的其他条件,feelingLucky(1/3)。或通过feelingLucky(100/100) 尝试连胜。

    祝你好运:)

    附:由于 if 语句后的冒号和数字 :97,您最近的编辑失败。他们不应该在那里。

    【讨论】:

    • 就是这样!我刚刚进行了更改,它们现在工作得很好。非常感谢您花时间指出原始代码中的缺陷,并提出这个替代方案。
    • @Eric-Paul:你错了:在原始教程中,如果随机数大于 97,则第一个条件评估为真,所以在100 次中有 2 次,这正是作者的意图。
    • @weltraumpirat 哎呀,你是对的。我假设两个比较符号都指向左边。去掉了那部分,但我仍然支持随机条件语句。干杯。
    • @Eric-Paul:我刚刚注意到发生了一些奇怪的事情。我在舞台上有许多同步开始的 mc,随着时间的推移,随机性开始并增加随机性。
    • @Casey 对我来说听起来像是一个新问题。可能与播放头经过带有此脚本的帧有关。有什么细节吗?
    【解决方案2】:

    是否有理由将 more/less 运算符更改为减号运算符? 似乎 if 块将对除 0 之外的所有内容执行。

    格式是

    if (condition) 其中条件的计算结果为真、假。

    我相信您的表达式正在转换为布尔类型。 示例

    if (random(100)&gt;97)
    是 99 > 97 => 是(条件评估为真)
    是 96 > 97 => 否(条件评估为假)

    if (random(100)-97)
    99 - 97 = 2
    表示为布尔值的 2 为真(条件计算为真)
    97 - 97 = 0
    0 表示为布尔值是假的(条件评估为假)

    【讨论】:

    • 嗯.. 你能详细说明一下 Philippe 吗?我有点理解你在说什么。
    • @Casey 您第一次编辑代码非常好,您只是使用减号而不是教程中提供的或多或少的运算符。所以每次 if 块由于错误的原因导致 true。
    【解决方案3】:

    您可以轻松实现自己的 random() 函数并保持其余代码完全相同:

    function random (n:int ) : int {
        return Math.floor (Math.random() * n);
    }
    

    【讨论】:

    • @weltraumpirat。您的公式引入了偏差。正确的公式是Math.floor( Math.random() * n)(这将 n 从可能的结果中排除)。如果您返回正整数,您也可以摆脱 Math.floor,因为截断小数在实践中与对正实数进行下限相同。
    • 你说得对,我希望 Math.random() 返回 0
    • @weltraumpirat。没问题。 +1 顺便发布正确答案。
    • @weltraumpirat 谢谢。我没有正确使用它,因为我收到 5 个编译错误。请参阅编辑。谢谢。
    • 冒号 : 在您的 if 语句中是错误的。就像我说的:保留原始代码,> 和
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 2017-08-07
    • 1970-01-01
    相关资源
    最近更新 更多