Adobe Dreamweaver Forums



Last 10 THreads :         help me with adjustments layers in CS4 (Last Post : Ann_Shelbourne@adobeforums.com - Replies : 4 - Views : 5 )           »          Re: Help authenticating installer (Last Post : Awebus - Replies : 0 - Views : 1 )           »          Site sometimes depends sessions, sometimes not,depending on user! (Last Post : VeryHopeful - Replies : 2 - Views : 3 )           »          Making a photo with rounded corners (Last Post : Linda Rathgeber - Replies : 9 - Views : 10 )           »          Problem initialising quicktime (Last Post : eoinie - Replies : 2 - Views : 3 )           »          image looks different in different browsers? (Last Post : Nancy O - Replies : 6 - Views : 7 )           »          How do I centre Tables ? (Last Post : Buringee - Replies : 2 - Views : 3 )           »          Changing Menu Bar (Last Post : Red67Coupe - Replies : 0 - Views : 1 )           »          Images not showing in IE (Last Post : Nancy O - Replies : 1 - Views : 2 )           »          Re: SFW and date created (Last Post : Ramón_G_Castañeda@adobeforums.com - Replies : 0 - Views : 1 )           »         


User Info Statistics
Go Back   Adobe Dreamweaver Forums > Macromedia Software > Flex
 
Tags:



Reply
  #1 (permalink)  
Old 11-20-2008, 04:55 AM
Roland G
 
Posts: n/a
Diggs:
Default click event not registering on sprite

Here is a simple test code. The generated swf detects a click on the stage but
not on the box. Where is the error?


package sb {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class ClickTest extends Sprite {

public function ClickTest()
{
this.drawBox();
this.stage.addEventListener(MouseEvent.CLICK, this.clickHandler);
this.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void {
trace("Mouse clicked at: " + event.localX + ", "
+ event.localY);
trace("Target of the event: " + event.target);
}

public function drawBox():void {
var box:Shape = new Shape();
this.addChild(box);

box.graphics.lineStyle(2, 0x990000, .75);
box.graphics.beginFill(0xFF0000, 0.5);
box.graphics.drawRect(0,0,100,100);
box.addEventListener(MouseEvent.CLICK, this.clickHandler);
}
}
}



Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2008, 04:23 PM
myIP
 
Posts: n/a
Diggs:
Default Re: click event not registering on sprite

Are you using Flash?s IDE? I can?t seem to get it to run in Flex but in Flash
it runs ok.

At any rate, you have some redundant code. You only need one addEventListener
to accomplish what I believe you want. Remove second addEventListener in the
construct and the one in the drawBox method.

Then replace your clickHandler with the one below and include the import path
for it.

Does this help?

//add this line of code with the rest of the imports
//import flash.events.EventPhase;

private function clickHandler(event:MouseEvent):void
{
if(event.eventPhase != EventPhase.AT_TARGET)
{
trace("Mouse clicked at: " + event.localX + ", "
+ event.localY);
trace("Target of the event: " + event.target);
}
}

Reply With Quote
  #3 (permalink)  
Old 11-20-2008, 05:13 PM
Roland G
 
Posts: n/a
Diggs:
Default Re: click event not registering on sprite

[q]Originally posted by: myIP
Are you using Flash?s IDE? I can?t seem to get it to run in Flex but in Flash
it runs ok.
[/q]

No, I'm using Flex 3 with FlashDevelop. I have a pure AS3 file which is the
one I posted and I compile it. The resulting swf runs fine and displays the box
as expected. Only the events aren't working as expected.

Changing the clickhandler to the version you suggested yields nothing. No
trace. But if I change the condition to:
event.eventPhase == EventPhase.AT_TARGET

I get the click trace again. My conclusion is that for some reason the event
is only registered at the stage and not in the child components(sprite and
shape).


Reply With Quote


  #4 (permalink)  
Old 11-21-2008, 04:23 PM
myIP
 
Posts: n/a
Diggs:
Default Re: click event not registering on sprite

Ok, the attached code below should answer your original question. The problem
was that Shape doesn?t inherit from InteractiveObject. I was under the
assumption that if a subclass inherits from EventDispatcher it would be
interactive. So in your last post, your conclusion was partial right; the
stage was listening as expected but descendant (Shape) was not. Now we know.

So I just added the shape instance to a sprite instance and it works. I also
have a listener for Event.ADDED_TO_STAGE. So when ClickTest is actually
attached to the stage, then listen for the Mouse.CLICK. Listening for
ADDED_TO_STAGE event may be redundant here. I don?t have the experience yet to
know when it is needed.

package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventPhase;
import flash.events.MouseEvent;

public class ClickTest extends Sprite
{

public function ClickTest()
{
super();

this.drawBox();

addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}

private function addedToStage(e:Event):void
{
trace("I am added to the display list. "+e.target);
this.stage.addEventListener(MouseEvent.CLICK, clickHandler);
}

private function clickHandler(e:MouseEvent):void
{
trace(e.eventPhase);
if(e.eventPhase != EventPhase.AT_TARGET)
{
trace("Mouse clicked at: " + e.localX + ", "
+ e.localY);
trace("Target of the event: " + e.target);
}
}

private function drawBox():void
{
var boxContainer:Sprite = new Sprite();
var box:Shape = new Shape();

box.graphics.lineStyle(2, 0x990000, .75);
box.graphics.beginFill(0xFF0000, 0.5);
box.graphics.drawRect(0,0,100,100);

boxContainer.addChild( box );
addChild( boxContainer );
}
}
}

Reply With Quote
  #5 (permalink)  
Old 11-21-2008, 10:02 PM
Roland G
 
Posts: n/a
Diggs:
Default Re: click event not registering on sprite

Thanks for you effort myIP, I figured out to make it work but I suspect there
is something broken with the AS3 event model. Take a look at
http://www.adobe.com/cfusion/webforu...id=60&catid=58
5&threadid=1407571&enterthread=y for more information.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 12:08 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Inactive Reminders By Mished.co.uk