In flash with as3.0, I have to call a function on the main stage from a movieClip

旧街凉风 提交于 2019-11-30 09:47:13

问题


I have to call a function that is defined on the main stage of my project, and I have to call it from a MovieClip, what can I do? I'm using flash cs5.5 and AS3.0


回答1:


There are multiple ways to access the MainTimeline from objects on the stage. Probably the most reliable is 'root', but there is also 'parent' (but only if you MovieClip is a direct child of the main timeline).

// root should always work
Object(root).myFunc();

// parent will only work if your movieclip is a direct child of the main timeline
Object(parent).myFunc();

Note that you have to cast these are generic Objects (or MovieClip would work) because they return typed classes that don't have a 'myFunc' function in them.

You'll need this code on your main timeline:

function myFunc():void {
  trace("My function got called!");
}



回答2:


When I read your question it sounds as though you have a function defined in an action frame of your main timeline.

My answer may be out of reach for your current project, and ToddBFisher's answer is perfectly right. That said - I'm going to answer the question differently.

Instead of defining a function on the main timeline, set up a document class, define your functions there, and access the class's functions in your code. Keep as much code off your timelines as possible.

Downloadable files for Document Class example: http://www.isgoodstuff.com/2008/06/06/actionscript-30-documentclass-in-plain-english/

Setting up an AS3 class: http://www.adobe.com/devnet/flash/quickstart/creating_class_as3.html




回答3:


Assuming your movie clip is a direct child of your main stage, in you movie clip you can do:

MovieClip(parent).theFunctionToCall();



回答4:


if your MovieClip has a class, just add it to your main class using like:

var m:MovieClip = new MovieClip(); **addChild(m);

then you can get access into it's public function like typing:

m."functon name";



来源:https://stackoverflow.com/questions/9119794/in-flash-with-as3-0-i-have-to-call-a-function-on-the-main-stage-from-a-moviecli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!