如何在1-10?
之间生成随机整数我目前还没有任何代码,而且我在网上查看但找不到任何内容。此外我需要它,所以当你点击一棵树它会产生一个随机数,如果它是6那么它会掉落一个苹果。我有点击中树,但没有别的,到目前为止,这是我的所有代码:
var count:Number = 0;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var changeCharacter:Boolean = false;
var still:Boolean = false
HeadBodyLegs1st.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
stop();
HeadBodyLegs1st.gotoAndStop(1)
Tree.gotoAndStop(1)
HeadBodyLegs1st.gotoAndStop(1)
stage.addEventListener(MouseEvent.RIGHT_CLICK,function():void{});
function fl_MoveInDirectionOfKey(event:Event)
{
if (leftPressed)
{
HeadBodyLegs1st.x -= 15;
HeadBodyLegs1st.gotoAndStop(3)
}
if (rightPressed)
{
HeadBodyLegs1st.gotoAndStop(2)
HeadBodyLegs1st.x += 15;
}
if (Tree.hitTestObject(HeadBodyLegs1st))
{
count = count + 100;
Score.text = (count).toString();
Tree.gotoAndStop(2)
}
if (still)
{
HeadBodyLegs1st.gotoAndStop(1)
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = true;
still = false
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
still = false
break;
}
case Keyboard.SPACE:
{
changeCharacter = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = false;
still = true
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
still = true
break;
}
case Keyboard.SPACE:
{
changeCharacter = false;
break;
}
}
}
非常感谢如果找到解决方案!
答案 0 :(得分:2)
以下代码是否会达到目标?
var random:int = Math.floor(Math.random() * 10) + 1;
基本上,Math.random()会生成一个从0到1(不包括1)的数字,乘以Math.random()的结果将返回一个从0到10的数字(但不包括10)。
通过执行Math.floor,会产生一个从0到9的随机数,因此向它添加1会产生一个从1到10的随机数。
答案 1 :(得分:0)
min + (max - min) * Math.random();