1
2
3
4
for(var i:uint=0; i<numChildren; i++){
	var mc:MovieClip = getChildAt(i);
	trace(mc.x, mc.y);
}
This code causes the type mis-match error at compile, because getChildAt() method returns a DisplayObject.
위 코드는 컴파일 시에 다음과 같은 타입 미스매치 에러를 냅니다. 왜냐하면 getChildAt()메소드는 DisplayObject 객체를 리턴하기 때문이죠.

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.

To avoid the compile error, you can cast the returned value as MovieClip like this.
이러한 컴파일 에러를 회피하기 위해 다음과 같이 리턴된 값을 MovieClip로 캐스팅합니다.
1
var mc:MovieClip = MovieClip(getChildAt(i));
You can also use “as”.
또는 “as”를 사용할 수 있습니다.
1
var mc:MovieClip = getChildAt(i) as MovieClip;

  4 Responses to “use “as” to avoid type mis-match error”

  1. 나도 어제 살펴보면서 이상한거야. 그래서 ‘as’를 사용해 보았더니 변환이 되더군. 캐스팅이 이럴때 필요한거 같은데. 아적 확실히 개념은 잡지 못했다. ㅋㅋ
    DisplayObject를 무비클립으로 변환해도 상관이 없는건가? 왜 이렇게 캐스팅을 해야 되는 걸까?

  2. 어차피 MovieClip은 DisplayObject를 상속한 것이니까…
    getChildAt()이 리턴하는 오브젝트는 무비만이 아니라, 비트맵이나 스프라이트일 수도 있고,
    여러가지 디스플레이 오브젝트를 상속한 녀석들이 될 수 있으니 이 모두를 아우를 수 있는 디스플레이 오브젝트를 리턴하는 것이 맞긴 한데… 좀 불편한건 어쩔수 없지. 그래서 as가 나온걸거고…

  3. 윽… 형님 캐스팅 부분 너무 어려운거 같아여 ㅠ_ㅠ;;;

  4. 첨에는 그런데, 막상 몇 번 해보면 뭐 별거 없어.
    문론 깊이 들어가면 더 어려울지도 모르겠지만서도…

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2011 Hangun's World - Blog Suffusion theme by Sayontan Sinha