View Full Version : i got a problem in addEventListener
kittrash
August 26th, 2007, 10:46 PM
here is my code, i draw two ciricle on the stage. Each circle can controlled by another circle on the stage. That's mean if i click circle1, then some action will be taken on circle 2. but the following code doesnt work, how can i control any other object using addEventListener ??
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class testing extends Sprtie {
public function testing() {
var cir1:Sprite = new Sprite();
cir1.graphics.beginFill(0x000000);
cir1.graphics.drawCircle(0,0,100);
cir1.graphics.endFill();
addChild(cir1);
var cir2:Sprite = new Sprite();
cir2.graphics.beginFill(0x000000);
cir2.graphics.drawCircle(200,200,100);
cir2.graphics.endFill();
addChild(cir2);
cir1.addEventListener(MouseEvent.MOUSE_DOWN, dnHandler);
}
private function dnHandler(event:MouseEvent):void {
cir2.x = 500;
}
}
}
kittrash
August 26th, 2007, 11:20 PM
i cannot pass a variable to cir2 by using cir1.addEventListener stated above . why ?? any mistake ?
McGuffin
August 26th, 2007, 11:38 PM
It's a scoping issue. the cir2 variable is only defined in the testing() function and is not available elsewhere. You need this:
public function testing() {
var cir1:Sprite = new Sprite();
cir1.graphics.beginFill(0x000000);
cir1.graphics.drawCircle(0,0,100);
cir1.graphics.endFill();
cir1.name = "cir1";
addChild(cir1);
var cir2:Sprite = new Sprite();
cir2.graphics.beginFill(0x000000);
cir2.graphics.drawCircle(200,200,100);
cir2.graphics.endFill();
cir2.name = "cir2";
addChild(cir2);
cir1.addEventListener(MouseEvent.MOUSE_DOWN, dnHandler);
}
private function dnHandler(event:MouseEvent):void {
var cir2:Sprite = getChildByName("cir2");
cir2.x = 500;
}
You could do it other ways, but that'll be the ticket.
kittrash
August 27th, 2007, 02:23 AM
Thanks, McGuffin.
But i face another problem after executing the following code
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class kit extends Sprite {
public function kit() {
var cir1:Sprite = new Sprite();
cir1.graphics.beginFill(0x000000);
cir1.graphics.drawCircle(0,0,100);
cir1.graphics.endFill();
cir1.name = "cir1";
addChild(cir1);
var cir2:Sprite = new Sprite();
cir2.graphics.beginFill(0x000000);
cir2.graphics.drawCircle(200,200,100);
cir2.graphics.endFill();
cir2.name = "cir2";
addChild(cir2);
cir1.addEventListener(MouseEvent.MOUSE_DOWN, dnHandler);
}
private function dnHandler(event:MouseEvent):void {
var cir2:Sprite = getChildByName("cir2");
cir2.x = 500;
}
}
}
the error msg is about:
1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:Sprite.
soulwire
August 27th, 2007, 11:57 AM
Try declaring cir1 and cir2 as sprites in your class constructor.
or
Humour Flash and change
var cir2:Sprite = getChildByName("cir2");
to
var cir2:DisplayObject = getChildByName("cir2");
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.