I'm currently programming a kind of point & click game in AS3 with adobe flash cs5.
Whenever I click on a door, I'd like to check if the key is held in order to unlock it. When the key is selected from the inventory, I want the cursor (which is NOT the original cursor, but a specific one I linked with startDrag) to take the key's icon.
Here's my code :
var selectedKey:Boolean = false;
key_obj.addEventListener(Mouse.CLICK, selectKey);
door.addEventListener(Mouse.CLICK, openDoor);
inventory_spot.addEventListener(Mouse.CLICK, drop);//send back key to inventory
function selectKey(e:MouseEvent):void
{
cursor.stopDrag();
removeChild(cursor); //disable the old cursor style
key_obj.removeEventListener(Mouse.CLICK, selectKey);
key_obj.startDrag(true);
selectedKey = true;
addChild(inventory_spot);
}
function openDoor(e:MouseEvent):void
{
if (selectedKey)
// open the door
else
// error : you don't have the key
}
function drop(e:MouseEvent):void
{
key_obj.stopDrag();
key_obj.addEventListener(Mouse.CLICK, selectKey);
selectedKey = false;
addChild(cursor); // enable the old cursor
cursor.startDrag(true);
key_obj.x = inventory_spot.x [...] // position of the key in the inventory
key_obj.y = inventory_spot.y [...]
removeChild(inventory_spot);
}
Here's my problem:
Nothing happens when I click with the key cursor on the door, actually the program doesn't even call openDoor()
, but once I dropped the key back to the inventory and got the old cursor back, then openDoor()
worked just fine.
I don't get it, is the function not called just because I changed my cursor?
Thanks for your help
Because the click is probably going to your key cursor instead of the door. To fix, you want to make your cursor mouseEnabled=false and mouseChildren=false.
I use the following to check the propagation of click events. So in your example, you can check what is being clicked on initially and then where the event propagates. "Never gets called issues" is usually because what you think is being clicked, isn't.
So add this:
stage.addEventListener(MouseEvent.CLICK,Functions.checkMouseEventTrail,false,0,true);
To call this:
function checkMouseEventTrail($e:MouseEvent):void{
// displays the mouse event trail for any selected item
var p:* = $e.target;
trace("\nMOUSE EVENT TRAIL\n time: "+getTimer()+" type: "+$e.type+" target: "+$e.target.name+" cur target: "+$e.currentTarget);
var spacing:String="";
while (p){
var curFrame = "none";
if (p is MovieClip){curFrame = String(p.currentFrame);}
trace(spacing+">>", p.name,": ",p+" vis: "+p.visible+" loc: "+p.x+","+p.y+" width: "+p.width+" height: "+p.height+" frame : "+curFrame+" class: "+getClassName(p));
if (p != null){p = p.parent;}
spacing+=" ";
}
trace("");
}
and you'll see in the output what is happening to your click event.