Calling a function on maintimeline from class (AS3)


#1

How do i go about calling a function in frame 1 of my timeline from a class file?

my function TEST on frame one of main timeline is just using a trace.

This is my error:

1061: Call to a possibly undefined method TEST through a reference with static type flash.displayisplayObject.


#2

Stop putting AS3 code on your timeline. It’s not a good AS3 coding practice. Use external AS files. Use linkage option in your movie clips.


#3

im just getting into as3 and have majority of my code already placed on timeline so for times sake this isnt feasible…

do you know a quick work round or just ability to tell me my coding practices are wrong.


#4

import class;

var cls:class = new class();

cls.function;


#5

senshi

From which file are you trying to access that function?

Did you put the function on your main timeline or on a timeline of a movie clip?

It’s really hard to tell something without seeing your code and project structure.


#6

cast it as a movieclip for example if your class is added to the main stage do this:

MovieClip(parent).test();


#7

ok guys heres my code:

Frame 1 on .fla:

function test()
{
trace(“it worked!”);
}

Class function:

public function executeTest() : void {
test();
}


#8
senshi

From which file are you trying to access that function?

Did you put the function on your main timeline or on a timeline of a movie clip?

It’s really hard to tell something without seeing your code and project structure.

Im accessing the function from my class file. The function is on my maintimeline…when i ‘trace(root)’ it returns MainTimeline so i thought root.test(); may work but no luck?


#9

Did you try what michelsteege said?

Depending on where your movie clip is located. Try;

MovieClip(parent).test();
or MovieClip(parent.parent).test();
even MovieClip(parent.parent.parent).test();

:slight_smile: I don’t really know where your class is located.


#10

MovieClip(parent).test(); didnt work.
I will try the other method listed when i get back to the office just out grabbing some lunch. thanks for the help will let u no how i get on. cheers


#11

TypeError: Error #1009: Cannot access a property or method of a null object reference.

I get this dude…argh this is confusing… i tried all of the .parent methods.


#12

Ok…try this if the above methods don’t work…

my class file looks like this:

package{
	import flash.display.MovieClip;
	
	public class MyClass{
	
		public function MyClass(mc:MovieClip){
			mc.myFunction();
		}
	}
	
}

First frame of my flash file looks like this:

function myFunction(){
	trace("worked");
}

var cl:MyClass = new MyClass(this);

It should trace “worked”.


#13

http://www.sendspace.com/file/j3a45a

dude i uploaded the .fla and class you used in your example. it doesnt work?


#14

Use Events.

dispatch an event from your class…

 dispatchEvent(new Event("CALL_TEST")); 
or
this.dispatchEvent(new Event("CALL_TEST")); 

Add a listener to your timeline code, listening for the event CALL_TEST on the class…

class.addEventListener("CALL_TEST", eventHandler);

Then your event handler…

function eventHandler(e:Event):void
{
  switch(e.type)
  {
    case "CALL_TEST":
      test();
    break;
  }
}

Using a switch statement allows you to have many events listened for on many classes and deal with them all within the one handler :slight_smile: Just add more cases for more events…

Make sure your class imports Event and extends eventDispatcher, Sprite or MovieClip so you can dispatch events :slight_smile:

Its also best to use enumeration classes rather than hand typing the strings as I did but it should work :slight_smile:


#15
Use Events.

dispatch an event from your class…

 dispatchEvent(new Event("CALL_TEST")); 
or
this.dispatchEvent(new Event("CALL_TEST")); 

Add a listener to your timeline code, listening for the event CALL_TEST on the class…

class.addEventListener("CALL_TEST", eventHandler);

Then your event handler…

function eventHandler(e:Event):void
{
  switch(e.type)
  {
    case "CALL_TEST":
      test();
    break;
  }
}

Using a switch statement allows you to have many events listened for on many classes and deal with them all within the one handler :slight_smile: Just add more cases for more events…

Make sure your class imports Event and extends eventDispatcher, Sprite or MovieClip so you can dispatch events :slight_smile:

Its also best to use enumeration classes rather than hand typing the strings as I did but it should work :slight_smile:

dude that is sick but im even having trouble just getting your snippets of code to work there, any chance u could whip a working version up for me in a zip file…would be much appreciated


#16
http://www.sendspace.com/file/j3a45a

dude i uploaded the .fla and class you used in your example. it doesnt work?

The reason it is not working is that you assigned MyClass as the document class of the FLA. If you are assigning a document class, then you are not supposed to put your code in the timeline. Only in the document class.


#17
Stop putting AS3 code on your timeline. It's not a good AS3 coding practice. Use external AS files. Use linkage option in your movie clips.
I didnt know this- Thanks for the tip. quick question tho, so if i put all AS in its own AS folder can i put that folder in an assets folder in my library? many Thanks, Luciano

PS . also say i am encoding an AS3 mp3player which I am doing currently, Is it still best practice to include the AS as seperate files? Or In certain cases is it best practice to still use the timeline?
again Thankz In Advanze.
Luciano


#18
http://www.sendspace.com/file/j3a45a

dude i uploaded the .fla and class you used in your example. it doesnt work?

The reason it is not working is that you assigned MyClass as the document class of the FLA. If you are assigning a document class, then you are not supposed to put your code in the timeline. Only in the document class.

shit dude your right. thanks allot for the help man.


#19

This is personally the best way I think…

Uses Events, Enumeration and Classes :slight_smile:

“Class Demo”:http://msfx.co.uk/temp/ClassDemo.zip

and you get to learn more about events :slight_smile:


#20
This is personally the best way I think...

Uses Events, Enumeration and Classes :slight_smile:

“Class Demo”:http://msfx.co.uk/temp/ClassDemo.zip

and you get to learn more about events :slight_smile:

can u save as cs3 mate?