# Casting an instance object from an external swf

**URL:** <https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102>\
**Category:** flash\
**Created:** [June 23, 2008, 7:27am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102 "2008-06-23T07:27:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scriptr](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@scriptr](https://forum.kirupa.com/u/scriptr)\
**Post date:** [June 23, 2008, 7:27am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102/1 "2008-06-23T07:27:57Z")

</div>

Hi,

I have a custom class called GoalKeeper.as . In my external swf, i create an instance of this class like

```auto
var myGoalKeeper:GoalKeeper = new GoalKeeper()

```

And the from my main swf, i just make a reference to use myGoalKeeper.

```auto
var rootGoalKeeper:GoalKeeper = externalSwf.myGoalKeeper;

```

And when i run the main swf, i got the following error?  
[COLOR=Red]  
TypeError: Error #1034: Type Coercion failed: cannot convert game::GoalKeeper@35896381 to game.GoalKeeper.[/COLOR]

myGoalKeeper is just an instance of GoalKeeper class, what is wrong?

Note:I develop classes in Adobe Flex and compile my projects in Adobe Flash CS3.

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 23, 2008, 7:34am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102/2 "2008-06-23T07:34:55Z")

</div>

When you import an swf the main swf doesn’t know about the programming or classes of the imported swf. You will need to cast it instead, and I’m not sure you can even access myGoalKeeper like that. I think you need to do

```auto
GoalKeeper(externalswf["myGoalKeeper");]

```

Sloppy I know, but I’m not sure if you can preserve swf typing on import. If there is a way I’d love to know it.

---

<div class="post-metadata">

**Author:** ![scriptr](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@scriptr](https://forum.kirupa.com/u/scriptr)\
**Post date:** [June 23, 2008, 7:47am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102/3 "2008-06-23T07:47:59Z")

</div>

Thanks Fidodo,  
but that didn’t work, got the same error. Because “casting”, called coercion by adobe engineers, is the place where the problem occurs. Flash knows that, i want to reference my instance, it just can not cast it.

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 23, 2008, 8:53am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102/4 "2008-06-23T08:53:25Z")

</div>

Is externalSwf is pointing to the content property of a Loader class? i.e.

```auto

var loader:Loader;
var externalSwf:DisplayObject = loader.content;

```

Also, make sure you are accessing the info after the content is loaded. You can’t access anything until the loader fires an init event.

---

<div class="post-metadata">

**Author:** ![scriptr](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@scriptr](https://forum.kirupa.com/u/scriptr)\
**Post date:** [June 23, 2008, 9:14am UTC](https://forum.kirupa.com/t/casting-an-instance-object-from-an-external-swf/264102/5 "2008-06-23T09:14:29Z")

</div>

thanks again,  
i gave up to fight Adobe’s Flash bugs and re-coding all my classes and swfs.
