本文介绍了SimpleButton的不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是试图让为SimpleButton在一个AS3的项目出现在舞台上。出于某种原因,就不会出现。任何人都可以看到,为什么?
在此先感谢。
code:
//主类:
包
{
进口flash.display.Sprite;
进口view.controls.CustomButton;
进口view.controls.Button;
公共类ButtonTest扩展Sprite
{
//私人VAR myCustomButton:按钮=新按钮();
私人VAR myCustomButton:的CustomButton;
公共职能ButtonTest()
{
myCustomButton =新的CustomButton(你好,为0xFF0000);
的addChild(myCustomButton);
}
}
}
//自定义按钮:
包view.controls
{
进口flash.display.GradientType;
进口flash.display.SimpleButton;
进口flash.display.Sprite;
进口的flash.display.Stage;
进口对象类型:flash.events.Event;
进口flash.geom.Matrix;
进口API元素flash.text.TextField;
进口flash.text.TextFormat用于;
进口使用flash.text.TextFormatAlign;
公共类的CustomButton扩展Sprite
{
私人VAR文字颜色:UINT = 0x000000处;
私人VAR myColor:UINT =为0xFF0000;
私人VAR btnWidth:数= 30;
私人VAR btnHeight:数= 18;
公共职能的CustomButton(buttonText:字符串,gradientColor:UINT)
{
VAR颜色:阵列=新的Array();
VAR阿尔法:阵列=新阵列(1,1);
VAR比率:阵列=新的Array(0,255);
VAR gradientMatrix:矩阵=新的Matrix();
VAR myColor:UINT =为0xFF0000;
gradientMatrix.createGradientBox(btnWidth,btnHeight,Math.PI / 2,0,0);
//
VAR ellipseSize:INT = 2;
VAR btnUpState:雪碧=新的Sprite();
颜色= [0XFFFFFF,myColor]。
btnUpState.graphics.lineStyle(3,brightencolor(myColor,-50));
btnUpState.graphics.beginGradientFill(GradientType.LINEAR,颜色,阿尔法,比率,gradientMatrix);
btnUpState.graphics.drawRoundRect(0,0,btnWidth,btnHeight,ellipseSize,ellipseSize);
btnUpState.addChild(createButtonTextField(buttonText,文字颜色));
//
VAR btnOverState:雪碧=新的Sprite();
颜色= [0XFFFFFF,brightencolor(myColor,50)]。
btnOverState.graphics.lineStyle(1,brightencolor(myColor,-50));
btnOverState.graphics.beginGradientFill(GradientType.LINEAR,颜色,阿尔法,比率,gradientMatrix);
btnOverState.graphics.drawRoundRect(0,0,btnWidth,btnHeight,ellipseSize,ellipseSize);
btnOverState.addChild(createButtonTextField(buttonText,文字颜色))
//
VAR btnDownState:雪碧=新的Sprite();
颜色= [brightencolor(myColor,-15),brightencolor(myColor,50)]。
btnDownState.graphics.lineStyle(1,brightencolor(myColor,-50));
btnDownState.graphics.beginGradientFill(GradientType.LINEAR,颜色,阿尔法,比率,gradientMatrix);
btnDownState.graphics.drawRoundRect(0,0,btnWidth,btnHeight,ellipseSize,ellipseSize);
btnDownState.addChild(createButtonTextField(buttonText,文字颜色))
//
VAR为myButton:SimpleButton的=新的SimpleButton(btnUpState,btnOverState,btnDownState,btnOverState);
myButton.name = buttonText;
myButton.height = btnHeight;
myButton.width = btnWidth;
}
私有函数createButtonTextField(文字:字符串,文本颜色:UINT):文本字段{
VAR myTextField的:文本字段=新的TextField();
myTextField.textColor =文本颜色;
myTextField.selectable = FALSE;
myTextField.width = btnWidth;
myTextField.height = btnHeight;
VAR myTextFormat:的TextFormat =新的TextFormat();
myTextFormat.align = TextFormatAlign.CENTER;
myTextField.defaultTextFormat = myTextFormat;
文本=< B>中+文字+< / B>中;
myTextField.htmlText ='<字型=宋体>+文字+'< / FONT>';
myTextField.x =(btnWidth / 2) - (myTextField.width / 2);
返回myTextField的;
}
私有函数brightencolor(颜色:INT,改性剂:INT):INT {
VAR(十六进制):阵列= hexToRGB(彩色);
无功红:INT = keepInBounds(十六进制[0] +修正);
VAR绿色:INT = keepInBounds(十六进制[1] +修正);
VAR蓝色:INT = keepInBounds(十六进制[2] +修正);
返回RGBToHex(红,绿,蓝);
}
私有函数hexToRGB(十六进制:UINT):数组{
VAR颜色:阵列=新的Array();
colors.push(十六进制>> 16);
VAR温度:UINT =十六进制^颜色[0]<< 16;
colors.push(温度>→8);
colors.push(温度^颜色[1];&所述; 8);
返回的颜色;
}
私有函数keepInBounds(编号:INT):INT {
如果(数℃,)数= 0;
如果(数字> 255)号= 255;
返回数;
}
私有函数RGBToHex(UR:INT,UG:INT,UB:INT):INT {
变种uColor:单元;
uColor =(UR&安培; 255)<< 16;
uColor + =(UG&安培; 255)<< 8;
uColor + =(UB&安培; 255);
返回uColor;
}
}
}
解决方案
您需要添加创建的SimpleButton
对象则myButton
到的CustomButton
的显示列表:
// ...
VAR为myButton:SimpleButton的=新的SimpleButton(btnUpState,btnOverState,btnDownState,btnOverState);
myButton.name = buttonText;
myButton.height = btnHeight;
myButton.width = btnWidth;
的addChild(myButton的);
}
虽然你的情况,也许你应该考虑一下做了的CustomButton扩展的SimpleButton代替。
I'm just trying to get a SimpleButton to appear on the stage in an AS3 project. For some reason, it won't appear. Can anyone see why?
Thanks in advance.
Code:
//Main class:
package
{
import flash.display.Sprite;
import view.controls.CustomButton;
import view.controls.Button;
public class ButtonTest extends Sprite
{
//private var myCustomButton:Button = new Button();
private var myCustomButton:CustomButton;
public function ButtonTest()
{
myCustomButton = new CustomButton("Hello", 0xFF0000);
addChild(myCustomButton);
}
}
}
//Custom Button Class:
package view.controls
{
import flash.display.GradientType;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomButton extends Sprite
{
private var textColor:uint = 0x000000;
private var myColor:uint = 0xFF0000;
private var btnWidth:Number = 30;
private var btnHeight:Number = 18;
public function CustomButton(buttonText:String, gradientColor:uint)
{
var colors:Array = new Array();
var alphas:Array = new Array(1, 1);
var ratios:Array = new Array(0, 255);
var gradientMatrix:Matrix = new Matrix();
var myColor:uint = 0xFF0000;
gradientMatrix.createGradientBox(btnWidth, btnHeight, Math.PI/2, 0, 0);
//
var ellipseSize:int = 2;
var btnUpState:Sprite = new Sprite();
colors = [0xFFFFFF, myColor];
btnUpState.graphics.lineStyle(3, brightencolor(myColor, -50));
btnUpState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnUpState.graphics.drawRoundRect(0, 0, btnWidth, btnHeight, ellipseSize, ellipseSize);
btnUpState.addChild(createButtonTextField(buttonText, textColor));
//
var btnOverState:Sprite = new Sprite();
colors = [0xFFFFFF, brightencolor(myColor, 50)];
btnOverState.graphics.lineStyle(1, brightencolor(myColor, -50));
btnOverState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnOverState.graphics.drawRoundRect(0, 0, btnWidth, btnHeight, ellipseSize, ellipseSize);
btnOverState.addChild(createButtonTextField(buttonText, textColor))
//
var btnDownState:Sprite = new Sprite();
colors = [brightencolor(myColor, -15), brightencolor(myColor, 50)];
btnDownState.graphics.lineStyle(1, brightencolor(myColor, -50));
btnDownState.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
btnDownState.graphics.drawRoundRect(0, 0, btnWidth, btnHeight, ellipseSize, ellipseSize);
btnDownState.addChild(createButtonTextField(buttonText, textColor))
//
var myButton:SimpleButton = new SimpleButton(btnUpState, btnOverState, btnDownState, btnOverState);
myButton.name = buttonText;
myButton.height = btnHeight;
myButton.width = btnWidth;
}
private function createButtonTextField(Text:String, textcolor:uint):TextField {
var myTextField:TextField = new TextField();
myTextField.textColor = textcolor;
myTextField.selectable = false;
myTextField.width = btnWidth;
myTextField.height = btnHeight;
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.align = TextFormatAlign.CENTER;
myTextField.defaultTextFormat = myTextFormat;
Text = "<b>"+Text+"</b>";
myTextField.htmlText = '<font face="Arial">'+Text+'</font>';
myTextField.x = (btnWidth/2)-(myTextField.width/2);
return myTextField;
}
private function brightencolor(color:int, modifier:int):int {
var hex:Array = hexToRGB(color);
var red:int = keepInBounds(hex[0]+modifier);
var green:int = keepInBounds(hex[1]+modifier);
var blue:int = keepInBounds(hex[2]+modifier);
return RGBToHex(red, green, blue);
}
private function hexToRGB (hex:uint):Array {
var colors:Array = new Array();
colors.push(hex >> 16);
var temp:uint = hex ^ colors[0] << 16;
colors.push(temp >> 8);
colors.push(temp ^ colors[1] << 8);
return colors;
}
private function keepInBounds(number:int):int {
if (number < 0) number = 0;
if (number > 255) number = 255;
return number;
}
private function RGBToHex(uR:int, uG:int, uB:int):int {
var uColor:uint;
uColor = (uR & 255) << 16;
uColor += (uG & 255) << 8;
uColor += (uB & 255);
return uColor;
}
}
}
解决方案
You need to add the created SimpleButton
object myButton
to the CustomButton
's display list:
// ...
var myButton:SimpleButton = new SimpleButton(btnUpState, btnOverState, btnDownState, btnOverState);
myButton.name = buttonText;
myButton.height = btnHeight;
myButton.width = btnWidth;
addChild( myButton );
}
Although in your case, maybe you should think about making the CustomButton extend the SimpleButton instead.
这篇关于SimpleButton的不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!