Make function global? (Scope question)

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?


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!

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

document class:


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


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 ; )

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.

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

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().

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);


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);
        }
    }
}