我正在尝试制作一个使用基本等式的简单游戏:
'linkCount'/'clickCount'* 100来创建%成功金额,'percentCount'
文本字段和以下as3都位于根级别的Movieclip中,实例名称为“counter_int”:
AS3:
var clickCount:int = 1; //see below* for what controls this number
var linkCount:int = 1; //see below* for what controls this number
var percentCount:int = (100);
percentCount++;
percent_text.text = (int(linkCount) / int(clickCount) * 100 + "%").toString();
此工作正常,并在正确的字段中显示%数量。但是,我的问题是截断%我删除小数点后的任何内容。我已经尽了一切努力让它发挥作用,但它没有它。
*
现在,这是我认为可能导致我的Math.round问题的棘手问题...我基本上只是不知道在哪里或如何应用Math.round指令?!我也怀疑使用'int'可能会出现问题并且尝试使用'Number'但它仍然显示小数位。
我在25个不同的动画片段中使用2个按钮...
按钮位置:
all_int_circles_master.cone.FAILlinkbutton
all_int_circles_master.cone.linkbutton
all_int_circles_master.ctwo.FAILlinkbutton
all_int_circles_master.ctwo.linkbutton
等等......来自ctwentyfiveFAIL按钮上的as3:
FAILlinkbutton.addEventListener(MouseEvent.CLICK, addClick1);
function addClick1(event:MouseEvent):void
{
Object(root).counter_int.clickCount++;
Object(this.parent.parent).counter_int.clicked_total.text = Object(root).counter_int.clickCount.toString();
}
成功链接按钮上的as3:
linkbutton.addEventListener(MouseEvent.CLICK, onClickNextSlide2);
function onClickNextSlide2(event:MouseEvent):void
{
Object(root).counter_int.clickCount++;
Object(this.parent.parent).counter_int.clicked_total.text = Object(root).counter_int.clickCount.toString();
}
当前返回%,例如:
74.334753434
但我需要它只是:
74
非常感谢任何帮助。如有必要,我可以提供.fla。这是我一直在尝试的但到目前为止没有运气:
应该以某种方式在根级别/全局应用Math.round!?
应该在counter_int movieclip中应用Math.round吗?
应该在所有all_int_circles_master.cone / two / three ... movieclips中应用Math.round吗?
由于
答案 0 :(得分:1)
你试过吗
percent_text.text = (Math.round(int(linkCount) / int(clickCount) * 100) + "%").toString();
答案 1 :(得分:0)
percent_text.text =(int(linkCount)/ int(clickCount)* 100 + "%&#34)的toString();
我认为你的clickCount和linkCount是向后的,在添加百分号之前交换那些并舍入结果:
percent_text.text = (Math.round((int(clickCount) / int(linkCount) * 100)).toString() + "%";
纯AS3示例:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class Main extends Sprite {
var clickCount:int = 0;
var linkCount:int = 30;
var clicked_total:TextField;
var button:CustomSimpleButton;
public function Main() {
button = new CustomSimpleButton();
button.addEventListener(MouseEvent.CLICK, onClickNextSlide2);
addChild(button);
clicked_total = new TextField();
clicked_total.text = "0%";
clicked_total.x = 100;
addChild(clicked_total);
}
function onClickNextSlide2(event:MouseEvent):void {
clickCount++;
if (clickCount < linkCount) {
clicked_total.text = (Math.round((clickCount / linkCount) * 100)).toString() + "%";
} else {
clicked_total.text = "Done";
}
}
}
}
import flash.display.Shape;
import flash.display.SimpleButton;
class CustomSimpleButton extends SimpleButton {
private var upColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var size:uint = 80;
public function CustomSimpleButton() {
downState = new ButtonDisplayState(downColor, size);
overState = new ButtonDisplayState(overColor, size);
upState = new ButtonDisplayState(upColor, size);
hitTestState = new ButtonDisplayState(upColor, size * 2);
hitTestState.x = -(size / 4);
hitTestState.y = hitTestState.x;
useHandCursor = true;
}
}
class ButtonDisplayState extends Shape {
private var bgColor:uint;
private var size:uint;
public function ButtonDisplayState(bgColor:uint, size:uint) {
this.bgColor = bgColor;
this.size = size;
draw();
}
private function draw():void {
graphics.beginFill(bgColor);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}
}