# Trouble Instantiating Class

**URL:** <https://forum.kirupa.com/t/trouble-instantiating-class/262405>\
**Category:** flash\
**Created:** [June 5, 2008, 5:35pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405 "2008-06-05T17:35:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhadaway](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bhadaway](https://forum.kirupa.com/u/bhadaway)\
**Post date:** [June 5, 2008, 5:35pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/1 "2008-06-05T17:35:44Z")

</div>

I’m trying to instantiate a custom class from the first frame of an .fla using the code:

```auto
var cd:Countdown = new Countdown();

```

This doesn’t work. However, when I set Countdown as the document class, the class is properly instantiated? Why is this happening? I don’t want to set Countdown as the document class. My class file is below. Help!

```auto
package {

    import flash.events.*;
    import flash.utils.*;
    import flash.text.*;
    import flash.display.*;

    public class Countdown extends MovieClip {
        private var tf:TextField;
        private var deadline:Date;
        private var millTimer:Timer;
        private var today:Date;
        private var diffDays:Number;
        private var diffHours:Number;
        private var diffMin:Number;
        private var diffSec:Number;
        private var diffMill:Number;

        public function Countdown() {
            deadline = new Date(2008,5,7,13,44,00,00);
            millTimer = new Timer(1);
            millTimer.addEventListener(TimerEvent.TIMER,onMillTimer);
            millTimer.start();
            initTextField();
        }
        private function initTextField():void {
            trace("called");
            tf = new TextField();
            tf.x = tf.y = 50;
            tf.width = 500;
            tf.autoSize = TextFieldAutoSize.LEFT;
            addChild(tf);
        }
        private function onMillTimer(e:TimerEvent):void {
            today = new Date();
            diffDays = (deadline.time - today.time)/86400000;
            diffHours = (diffDays - Math.floor(diffDays)) * 24;
            diffMin = (diffHours - Math.floor(diffHours)) * 60;
            diffSec = (diffMin - Math.floor(diffMin)) * 60;
            diffMill = (diffSec - Math.floor(diffSec)) * 1000;
            diffMill--;
            if (diffMill < 0) {
                diffMill = 999;
                diffSec--;
                if (diffSec < 0) {
                    diffSec = 59;
                    diffMin--;
                    if (diffMin < 0) {
                        diffMin = 59;
                        diffHours--;
                        if (diffHours < 0) {
                            diffHours = 24;
                            diffDays--;
                        }
                    }
                }
            }
            //trace(Math.floor(diffDays)+":"+getHour(diffHours)+":"+getMin(diffMin)+":"+getSec(diffSec)+":"+getMill(diffMill));
            tf.text = Math.floor(diffDays)+":"+getHour(diffHours)+":"+getMin(diffMin)+":"+getSec(diffSec)+":"+getMill(diffMill);
        }
        private function getHour(diffHour:Number):String {
            if (diffHour < 10) {
                return "0" + Math.floor(diffHour).toString();
            } else {
                return Math.floor(diffHour).toString();
            }
        }

        private function getMin(diffMin:Number):String {
            if (diffMin < 10) {
                return "0" + Math.floor(diffMin).toString();
            } else {
                return Math.floor(diffMin).toString();
            }
        }

        private function getSec(diffSec:Number):String {
            if (diffSec < 10) {
                return "0" + Math.floor(diffSec).toString();
            } else {
                return Math.floor(diffSec).toString();
            }
        }

        private function getMill(diffMill:Number):String {
            if (diffMill < 100 && diffMill > 9) {
                return "0" + Math.floor(diffMill).toString();
            } else if (diffMill < 10) {
                return "00" + Math.floor(diffMill).toString();
            } else {
                return Math.floor(diffMill).toString();
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![jrutter](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jrutter/32/2004_2.png) [@jrutter](https://forum.kirupa.com/u/jrutter)\
**Post date:** [June 5, 2008, 6:03pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/2 "2008-06-05T18:03:58Z")

</div>

You need to import the class first, then you can instantiate it.

```auto

import Countdown;
var cd:Countdown = new Countdown();

```

---

<div class="post-metadata">

**Author:** ![bhadaway](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bhadaway](https://forum.kirupa.com/u/bhadaway)\
**Post date:** [June 5, 2008, 6:08pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/3 "2008-06-05T18:08:14Z")

</div>

Oops… The class IS being instantiated but the TextField tf isn’t displaying on stage despite the fact that I’m using addChild(tf). Suggestions?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 5, 2008, 6:14pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/4 "2008-06-05T18:14:43Z")

</div>

[QUOTE=jrutter;2337425]You need to import the class first, then you can instantiate it.

```auto

import Countdown;
var cd:Countdown = new Countdown();

```

[/QUOTE]

imports are only necessary when classes are defined in pacakages (and your script is not being called from within the context of the package in which the class is defined)

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 5, 2008, 6:15pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/5 "2008-06-05T18:15:06Z")

</div>

Have you used _addChild_ to add your Countdown instance to the stage?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 5, 2008, 6:15pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/6 "2008-06-05T18:15:27Z")

</div>

[QUOTE=bhadaway;2337430]Oops… The class IS being instantiated but the TextField tf isn’t displaying on stage despite the fact that I’m using addChild(tf). Suggestions?[/QUOTE]

You may have added the textfield but have you added the Countdown instance?

---

<div class="post-metadata">

**Author:** ![bhadaway](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bhadaway](https://forum.kirupa.com/u/bhadaway)\
**Post date:** [June 5, 2008, 6:20pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/7 "2008-06-05T18:20:05Z")

</div>

senocular you are a legend! thanks! still getting the hang of AS3.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 5, 2008, 6:22pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/8 "2008-06-05T18:22:30Z")

</div>

Krilnon beat me to it 😉

---

<div class="post-metadata">

**Author:** ![bhadaway](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bhadaway](https://forum.kirupa.com/u/bhadaway)\
**Post date:** [June 5, 2008, 6:55pm UTC](https://forum.kirupa.com/t/trouble-instantiating-class/262405/9 "2008-06-05T18:55:58Z")

</div>

In that case, thanks Krilnon. I believe this is the second time you’ve helped out of bind. Much obliged.
