我已经搜索了这个,但我认为答案是如此基本,我无法找到它(我已经做了5天的闪存)
我有一堆物品,当点击一个物品时,我的物品就会上升,而且每当物品> = 1时,我想要一个不同的物体(三叶草)发光。
我使用MouseEvent进行此操作,但除非单击对象,否则无法让三叶草发光...所以很明显MouseEvent.CLICK不是我想要的。我只想让对象在我的项目> = 1后发光。下面的代码(我为了简洁而编辑了它,所以我可能已经遗漏了一些东西,但是当我运行它时我没有错误)
//import greensock
import com.greensock.*;
import flash.events.MouseEvent;
//make a variable to count items
var items = 0;
//add a click event to the clover
clover.addEventListener(MouseEvent.CLICK, payUp); //this is where i think it's wrong...
//click on b1 merch item
merch.b1.addEventListener(MouseEvent.CLICK, grabB1)
function grabB1(e: MouseEvent): void {
//make the item disappear and add 1 to items
merch.b1.visible = false;
items++
merch.b1.removeEventListener(MouseEvent.CLICK, grabB1)
}
//explain the payUp function
function payUp(m:MouseEvent){
if (items >= 1) {
//make the clover glow green
TweenMax.to(clover, 0.5, {glowFilter:{color:0x66CC00, alpha:1, blurX:40, blurY:40}});
clover.buttonMode = true;
clover.useHandCursor = true;
}
}
答案 0 :(得分:0)
如果我理解正确,你有很多项目(代码中只显示1项以保持简单?merch.b1?)。点击其中任何一项后,如果clover
var大于或等于items
,您希望1
发光。
如果我有这个权利,那么这应该回答你的问题(提出一些提示以减少冗余代码)。
//add a click event to the clover
clover.addEventListener(MouseEvent.CLICK, cloverClick);
//disable the ability to click the clover at first
clover.mouseEnalbled = false;
clover.mouseChildren = false;
//click listeners for your items
merch.b1.addEventListener(MouseEvent.CLICK, itemClick);
function itemClick(e:MouseEvent):void {
//make the item disappear
var itemClicked:DisplayObject = e.currentTarget as DisplayObject;
//e.currentTarget is a reference to the item clicked
//so you can have just this one handler for all the items mouse clicks
itemClicked.visible = false;
//if you want the item to be garbage collect (totally gone), you also need to remove it from the display (instead of just making it invisible)
itemClicked.parent.removeChild(itemClicked);
items++
itemClicked.removeEventListener(MouseEvent.CLICK, itemClick);
//now see if the clover needs to grow
playUp();
}
//explain the payUp function
function payUp(){
if (items >= 1) {
//make the clover glow green
TweenMax.to(clover, 0.5, {glowFilter:{color:0x66CC00, alpha:1, blurX:40, blurY:40}});
//make the clover clickable
clover.buttonMode = true;
clover.useHandCursor = true;
clover.mouseEnalbled = true;
clover.mouseChildren = true;
}
}
function cloverClick(e:Event):void {
//do whatever you need to do when the clover is clicked
}