开发者

When doing a cast conversion in Flex 4, can you cast convert "anything" to "anything?"

I'm building a custom component and I need to convert the event that is passed to the method into a mouse event. I can do this but it is telling me that I'm getting a null reference of an object. Here is how I am calling it.

public function dragStart(e:MDIWindowEvent): void {
    var mouse:MouseEvent = (e.currentTarget as MouseEvent);
    trace(mouse.localX);//<-----Null Error
}

How can I go by converting the MDIWind开发者_StackOverflow社区owEvent to a MouseEvent? If I can at that.


You are casting the Event target not the event it won't work. Also you can't cast an MDIWindowEvent as a MouseEvent.

What you can try is:

public function dragStart(e:MDIWindowEvent): void {
    trace(e.currentTarget.mouseX);
}

Rob


if MDIWindowEvent doesn't extend MouseEvent, this won't work.


The as returns the object casted or null if the object cannot be casted. It cannot be casted if it's not a subclass of what you're trying to cast to. This way you can recover if the cast does not work as planned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜