4 Replies - 2011 Views - Last Post: 04 September 2010 - 08:08 AM

#1 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

AS3.0 info from MouseEvent for generic handler

Posted 03 September 2010 - 03:52 AM

Hey guys,

I recently decided to play around with AS3.0 a bit since I'd never touched any Flash or AS in the past.
What I've ended up with is a vector representing a deck of cards. I've added a MouseEvent listener which I plan to use for each of the cards. This is fine as I've just passed the same function in however my problem is the handler has no way of knowing which card was clicked.

Is there a way to get any info from the event itself so as to use my generic handler?

I was thinking of adding the listener inside my card class which the vector holds instances of? Can listeners work if they're not directly connected to the stage?

I stumbled across 'relatedObject'. Is this of any use for this scenario? I was thinking to use it something like this:

function onCardMouseDown(event:MouseEvent):void
{
	trace(event.relatedObject.type);
}


I get a '1119: Access of possibly undefined property type through a reference with static type flash.display:InteractiveObject.' error when I try this. If I use the trace without the '.type' it outputs null which isn't right unless I'm misunderstanding the use of relatedObject.

Ok so I found out relatedObject only works with MOUSE_OVER or MOUSE_OUT events which is fine but I'm still not able to access members of my Card class such as '.type'. Is there a way to typecast it?

For anyone who's interested I managed to reference the card pointed at using:

function onCardMouseOver(event:MouseEvent):void
{
	var target:Card = event.target as Card;
    	trace(target.type);
}


Card is my class extended from MovieClip FYI.

My only problem now is that if my mouse moves over an object which isn't a Card instance including objects comprising it, I get a:
'1009: Cannot access a property or method of a null object reference.' error

Managed to prevent events being picked up that weren't Card instances using:

if(event.target == "[object Card]")
{
}


FYI the square brackets ARE necessary to work.

SOLVED!

Thanks

Kratos

This post has been edited by Enter_Kratos: 03 September 2010 - 12:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: AS3.0 info from MouseEvent for generic handler

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: AS3.0 info from MouseEvent for generic handler

Posted 03 September 2010 - 03:31 PM

You could also use currentTarget instead of target, and do it as a DisplayObject
event.currentTarget as DisplayObject

Was This Post Helpful? 0
  • +
  • -

#3 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: AS3.0 info from MouseEvent for generic handler

Posted 04 September 2010 - 06:35 AM

What are the benefits of doing it as a DisplayObject?
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: AS3.0 info from MouseEvent for generic handler

Posted 04 September 2010 - 07:48 AM

Well DisplayObject should stop you from getting any null pointers as it refers to any Object. You also get all the functionality of the DisplayObject class.
Was This Post Helpful? 0
  • +
  • -

#5 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: AS3.0 info from MouseEvent for generic handler

Posted 04 September 2010 - 08:08 AM

Thanks that was really helpful. I keep forgetting that pretty much everything is a subclass of something else.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1