如何生成1-10之間的隨機整數?Flash CC隨機數
我現在還沒有得到任何代碼,而且我在網上看了一下,但找不到任何東西。此外,我需要它,所以當你打一棵樹時,它會產生一個隨機數,如果它是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;
}
}
}
非常感謝如果解決方案被發現!
Math.ceil(數學式。隨機()* 10); –
Math.random()可以得到0,因此Andrew的結果基本上可以包含0到10之間的數字,而不是1到10. –