我如何为该图片库中的按钮提供翻转效果,并为每个按钮分配单独的标签?
所以我试图让我的图片库中的这些“按钮”具有某种翻转效果(文本改变颜色),也不是说“Linus0”,“Linus1”,“Linus2”,我想为每个按钮分配它的自己的标签。我可以用数组来做到这一点吗?另外 - 我做错了什么,让它认为总共有六张图像?只有 5 个,我只需要 5 个按钮。提前致谢!到目前为止,这是我的代码:
var myPics:Array = new Array ("image0", "image1", "image2", "image3", "image4");
var totalItems: int = myPics.length;
function createLabels () : void {
for (var i:int=0; i<=totalItems; i++){
var btn_label:TextField = new TextField ();
this.addChild (btn_label);
btn_label.name = "image"+i;
var format:TextFormat = new TextFormat ();
format.font = "Verdana";
format.color = 0x000000;
format.size = 12;
btn_label.defaultTextFormat = format;
btn_label.x = 55;
btn_label.y = 50+ (i*20);
btn_label.text = "Linus"+i;
}
};
function makeMenu ():void {
for (var i:int=0; i<=totalItems; i++) {
var myBtn:button = new button;
this.addChild (myBtn);
myBtn.name = "image"+i;
myBtn.x = 55;
myBtn.y = 50+ (i*20);
myBtn.addEventListener(MouseEvent.CLICK, btnPress);
myBtn.addEventListener(MouseEvent.CLICK, btnPress);
trace(i);
}
};
createLabels ();
makeMenu ();
var myFrame: frame = new frame;
this.addChild (myFrame);
myFrame.name = "frame";
myFrame.x = 200;
myFrame.y = 70;
var myLoader:Loader = new Loader ();
function btnPress (event:MouseEvent):void{
this.enabled = true;
myLoader.load (new URLRequest("images/"+event.target.name+".jpg"));
addChild(myLoader);
myLoader.x = 201;
myLoader.y = 71;
};
So I am trying to make these "Buttons" in my image gallery have some sort of rollover effect (text change color) , also instead of saying "Linus0", "Linus1", "Linus2", I would like to assign each one its own label. Am I able to do this with an array? Also - what am I doing wrong thats making it think there are six images total? There are only 5, and I only need 5 buttons. Thanks in advance! Here is my code so far:
var myPics:Array = new Array ("image0", "image1", "image2", "image3", "image4");
var totalItems: int = myPics.length;
function createLabels () : void {
for (var i:int=0; i<=totalItems; i++){
var btn_label:TextField = new TextField ();
this.addChild (btn_label);
btn_label.name = "image"+i;
var format:TextFormat = new TextFormat ();
format.font = "Verdana";
format.color = 0x000000;
format.size = 12;
btn_label.defaultTextFormat = format;
btn_label.x = 55;
btn_label.y = 50+ (i*20);
btn_label.text = "Linus"+i;
}
};
function makeMenu ():void {
for (var i:int=0; i<=totalItems; i++) {
var myBtn:button = new button;
this.addChild (myBtn);
myBtn.name = "image"+i;
myBtn.x = 55;
myBtn.y = 50+ (i*20);
myBtn.addEventListener(MouseEvent.CLICK, btnPress);
myBtn.addEventListener(MouseEvent.CLICK, btnPress);
trace(i);
}
};
createLabels ();
makeMenu ();
var myFrame: frame = new frame;
this.addChild (myFrame);
myFrame.name = "frame";
myFrame.x = 200;
myFrame.y = 70;
var myLoader:Loader = new Loader ();
function btnPress (event:MouseEvent):void{
this.enabled = true;
myLoader.load (new URLRequest("images/"+event.target.name+".jpg"));
addChild(myLoader);
myLoader.x = 201;
myLoader.y = 71;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你得到 6 而不是 5 的原因是你使用的是 <= 而不是 <。因为“i”从 0 开始,这意味着循环计数 6 次。
至于翻转效果,如果您只想更改文本颜色,那么应该这样做:
the reason you are getting 6 instead of 5 is that you are use <= rather than <. Because 'i' starts at 0 this means the loop counts 6 times.
As for a rollover effect if you only want to change the text color then this should do:
+1 奥利弗
对此所说的话
是的,您可以定义一个包含有效名称的字符串数组,然后用它填充按钮标签。
+1 what oliver said
as for this
yes you can define an array of strings containing valid names and then populate your button labels with that.