I want to make a clicked function that recognized the item being click, is that possible? Could you teach me ?^^
Here is my try, but It didn't work:
package {
import flash开发者_高级运维.display.MovieClip;
import flash.events.MouseEvent;
public class main extends MovieClip {
private var _s1:s1Mc;
private var _s2:s2Mc;
public function main() {
_s1=new s1Mc();
this.addChild(_s1);
_s2=new s2Mc();
this.addChild(_s2);
_s1.addEventListener(MouseEvent.CLICK,ClickedF(_s1));
_s2.addEventListener(MouseEvent.CLICK,ClickedF(_s2));
}
public function ClickedF(e:MouseEvent,$varMc:MovieClip)
{
if($varMc==_s1)
trace("_s1");
if($varMc==_s2)
trace("_s2");
}
}
}
Here is the Errors:
F:\test\click test\main.as, Line 18 1067: Implicit coercion of a value of type s1Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 18 1136: Incorrect number of arguments. Expected 2.
F:\test\click test\main.as, Line 19 1067: Implicit coercion of a value of type s2Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 19 1136: Incorrect number of arguments. Expected 2.
I understand that I should set 2 things inside the ClickedF of addEventListener but I really don't know how to do it T_T
You can only have one argument in an event handler, but the Event object that comes in has a property currentTarget
, which is that object that dispatched the event.
public function main() {
_s1=new s1Mc();
this.addChild(_s1);
_s2=new s2Mc();
this.addChild(_s2);
_s1.addEventListener(MouseEvent.CLICK,ClickedF);
_s2.addEventListener(MouseEvent.CLICK,ClickedF);
}
public function ClickedF(e:MouseEvent)
{
if(e.currentTarget==_s1)
trace("_s1");
if(e.currentTarget==_s2)
trace("_s2");
}
Also, by convention you should start your function names with a lowercase character. Only Class names should start with an uppercase character. This makes your code easier for others to understand, because by convention ClickedF
looks like you are passing a reference to a class, not a function. When working in a team of developers sticking to these standards is essential for productivity and is usually a workplace requirement, so it's best to get into the habit now.
Try this:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class main extends MovieClip {
private var _s1:s1Mc;
private var _s2:s2Mc;
public function main() {
_s1=new s1Mc();
this.addChild(_s1);
_s2=new s2Mc();
this.addChild(_s2);
_s1.addEventListener(MouseEvent.CLICK,ClickedF);
_s2.addEventListener(MouseEvent.CLICK,ClickedF);
}
public function ClickedF(e:MouseEvent):void
{
var $varMc:MovieClip = MovieClip(e.currentTarget);
if($varMc==_s1)
trace("_s1");
if($varMc==_s2)
trace("_s2");
}
}
}
精彩评论