如何调用一个类从另一个类的函数? [英] How to call a function in a Class from another Class?
问题描述
更新:修改标题,以更好地反映我的问题的
大家好:)
今天我的问题都是围绕着一个自定义事件我试图从一个子类发送到另一个。
My question today revolves around a CustomEvent I'm trying to send from one sub Class to another.
我用我的自定义事件类从子类的事件传递给我的主类很好,但我不知道是谁做的子类之间。
I've used my CustomEvent class to pass an event from a sub Class to my main class fine, but I'm not sure who to do that between sub classes.
我的自定义事件类
package src.events {
import flash.events.Event;
public class CustomEvent extends Event
{
public static const CHANGE:String="onChange";
public static const COMPLETE:String="onComplete";
public static const IMAGE_LOADED:String="onImageLoaded";
public static const SWF_LOADED:String="onSWFLoaded";
public static const SWF_UNLOAD:String="onSWFUnload";
public static const CLICK:String="onClick";
public static const PAUSE_MOVIE:String="onPause";
public static const INTRO_CLICKED:String="introClicked";
public var params:Object;
public function CustomEvent(type:String, params:Object, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
this.params=params;
}
public override function clone():Event
{
return new CustomEvent(type, this.params, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("CustomEvent","params","type","bubbles","cancelable");
}
}
子类INTRO - 分派事件
package src.display {
import gs.easing.*;
import gs.TweenLite;
import flash.text.*;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
// ☼ ----------------------------------------- Imported Custom Classes
import src.events.CustomEvent;
// ☼ ----------------------------------------- Intro Class
public class Intro extends Sprite
{
private var playBtn:MovieClip;
// ☼ ----------------------------------------- Constructor
public function Intro():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
// ☼ ----------------------------------------- Init
public function init(event:Event):void
{
draw();
}
public function draw():void
{
playBtn = new PlayThumb;
playBtn.buttonMode = true;
playBtn.x = 582;
playBtn.y = 259;
playBtn.alpha = 1;
addChild(playBtn);
playBtn.addEventListener(MouseEvent.MOUSE_UP, clicked);
}
private function clicked(e:MouseEvent):void
{
trace("clicked big play");
dispatchEvent (new CustomEvent(CustomEvent.INTRO_CLICKED, {}));
trace("event = "+CustomEvent.INTRO_CLICKED);
}
}
子类导航 - 在我听该事件 需要调用此函数导航类里面:
Sub Class NAVIGATION - where I'm listening for the event Need to call this function inside of the Navigation class:
function introPlayButtonClick(e:CustomEvent):void
{
trace("introPlayButtonClick called");
TweenLite.to(buttons[0], 1, {y:20, ease:Strong.easeOut});
TweenLite.to(buttons[1], 1, {y:navBtnY, ease:Strong.easeOut});
TweenLite.to(introTab, 1, {y:60, ease:Strong.easeOut});
TweenLite.to(scrollerMov, 1, {y:60, ease:Strong.easeOut});
$btn1 = true;
$btn2 = false;
trace("introPlayButtonClick called --- end");
}
确定,所以我相信我的问题就出在导航类,我该如何正确地在这里添加的addEventListener?我没有得到任何的痕迹在我introPlayButtonClick功能。
Ok so I believe my problem lies in the Navigation Class, how do I correctly add the addEventListener here? I'm not getting any of the traces in my introPlayButtonClick function.
它是如何工作在我的主类与PAUSE_MOVIE事件,是我通过变量名NV附加的事件监听到导航类:
How it worked in my Main class with the PAUSE_MOVIE event, was I attached the eventListener to the Navigation class via the variable name nv:
nv.addEventListener("onPause", pauseMovie); // Inside of MAIN class
但自从我加入了事件侦听器的导航类里面的INTRO_CLICKED事件中,我想我需要的是(这一点)。补充,但至今我没有收到该事件触发。
But since I'm adding the event listener inside of the Navigation class for the INTRO_CLICKED event I thought all I needed was (this.) added, but so far I'm not getting that event to trigger.
任何意见,建议,想法?
Any ideas, tips, thoughts?
推荐答案
我用的公共静态无功这样我就可以参考类,我试图调用,而不是使用事件调度功能直接,
I used public static var so I could reference the Class I was trying to call a function in directly, instead of using event dispatchers.
第一部分: 导航类
public class Navigation extends MovieClip
{
private var // where I put many private vars
private var intro:Intro;
public var // where I put many public vars
public static var instance:Navigation;
// ☼ --------------------------------- Constructor
public function Navigation():void
{
instance = this; //<- so I can reference this Class from another
//other code
介绍类按钮动作:
private function clicked(e:MouseEvent):void
{
trace("clicked play -> launch function in Navigation");
Navigation.instance.introPlayButtonClick(); //Class targeted
}
现在,我可以从简介类中,调用这个函数在我的导航类:
Now I can from inside the Intro class, call this function in my Navigation class:
public function introPlayButtonClick():void
{
TweenLite.to(buttons[0], 1, {y:20, ease:Strong.easeOut});
TweenLite.to(buttons[1], 1, {y:navBtnY, ease:Strong.easeOut});
TweenLite.to(introTab, 1, {y:60, ease:Strong.easeOut});
TweenLite.to(scrollerMov, 1, {y:60, ease:Strong.easeOut});
$btn1 = true;
$btn2 = false;
}
这篇关于如何调用一个类从另一个类的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!