# Make function global? (Scope question)

**URL:** <https://forum.kirupa.com/t/make-function-global-scope-question/264416>\
**Category:** flash\
**Created:** [June 25, 2008, 10:59pm UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416 "2008-06-25T22:59:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [June 25, 2008, 10:59pm UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/1 "2008-06-25T22:59:09Z")

</div>

Say I have a function like… updateDatabase(){ //Do stuff} on my main timeline.

Is there any way I can call it from within a class?

```auto

package {
    import flash.display.*;
    public class Module extends MovieClip {
        public function Module() {
            parent.updateDatabase('Database info');
        }
    }
}

```

Edit: Ooops… This belongs in Flash CS3, please move!

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 25, 2008, 11:35pm UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/2 "2008-06-25T23:35:19Z")

</div>

You can use parent if your class is added as a child, and call functions in it’s parent like thus;

document class:

```auto

package {

	import flash.display.Sprite;
	import com.subclass; 
	
	public class dclass extends Sprite {

		public function dclass():void {
			var s:subclass = new subclass();
			addChild(s);
			s.init();
		};
	
		public function helloWorld():void {
			trace("Call!");
		};
	};
};

```

subclass.as

```auto

package com {
	
	import flash.display.Sprite;
	
	public class subclass extends Sprite {
		
		public function subclass():void {
		};
		
		public function init():void {
			dclass(parent).helloWorld();
		};
	};
};

```

Might be a few type-os. Watch out ; )

---

<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 25, 2008, 11:39pm UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/3 "2008-06-25T23:39:11Z")

</div>

You can call it from within a class as long as you have a reference to an instance upon which the function is defined.

What’s up with the code that you posted? You didn’t really put it in any context, so am I supposed to guess that _parent_ is the main timeline? If so, you can cast it to MainTimeline, or to whatever you named your timeline class.

---

<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 26, 2008, 1:11am UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/4 "2008-06-26T01:11:31Z")

</div>

I wouldn’t put code on the timeline anywhere. Write a custom class and import it where you need it.

---

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [June 26, 2008, 4:30am UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/5 "2008-06-26T04:30:24Z")

</div>

I would agree, however my main concern is time right now

Oh, and yes, I was trying to access a function on the root timeline from a class I was adding with addChild().

> [@Fidodo;2349306](#):
>
> I wouldn’t put code on the timeline anywhere. Write a custom class and import it where you need it.

---

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [June 26, 2008, 4:52am UTC](https://forum.kirupa.com/t/make-function-global-scope-question/264416/6 "2008-06-26T04:52:18Z")

</div>

What I actually have is a Bitmap variable on the main timeline… And I need to do hittests on the bitmapData from my class…

And I can’t figure out how to access it from the class, or any functions on the timeline. Argh…

It should be simple… Nothing is working.

I even tried adding it to the class…

newBuggy.bitmapLevel = bitmapLevel;  
gameLevel.addChild(newBuggy);

```auto

package {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.getTimer;

    public class Buggy extends MovieClip {
        public var moving = 'no';
        public var bitmapLevel:Bitmap = new Bitmap();
        //Fire rate is in frames
        public function Buggy() {
            //Rotate them so they fill in the opposite direction
            trace(bitmapLevel.bitmapData);
        }
    }
}

```
