将 Math.round 应用于文本字段时出现问题 - AS3
Problems applying Math.round to a text field - AS3
我正在尝试制作一个使用基本方程式的简单游戏:
‘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
等...到ctwentyfive
FAIL 按钮上的 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 成功 link 按钮:
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 动画片段中应用 Math.round?
Math.round 是否应该应用于所有 all_int_circles_master.cone / 两个 / 三个... 电影片段?
谢谢
你试过了吗
percent_text.text = (Math.round(int(linkCount) / int(clickCount) * 100) + "%").toString();
percent_text.text = (int(linkCount) / int(clickCount) * 100 +
"%").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();
}
}
我正在尝试制作一个使用基本方程式的简单游戏:
‘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
等...到ctwentyfive
FAIL 按钮上的 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 成功 link 按钮:
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 动画片段中应用 Math.round?
Math.round 是否应该应用于所有 all_int_circles_master.cone / 两个 / 三个... 电影片段?
谢谢
你试过了吗
percent_text.text = (Math.round(int(linkCount) / int(clickCount) * 100) + "%").toString();
percent_text.text = (int(linkCount) / int(clickCount) * 100 + "%").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();
}
}