在Sprite中访问MovieClip的属性(反之亦然)

时间:2011-01-11 22:16:55

标签: flash actionscript-3

test.fla的文档类是Test.as test.fla的库有两个MovieClip ThingMovieClip和Thing2MovieClip

我尝试访问Thing.as public var thingState的属性; 直, 使用一种叫做Casting(??)的东西, 分配实例名称, 但似乎很简单的东西不起作用, 我评论了我尝试的每种方式后面的错误,

相反如何从Thing.as访问MovieClip Thing2MovieClip(这是Test.as的子代,因为它是实例化的地方),它只是不识别其父对象存在,这里是2个类:

包com.hello.test.test {     import flash.display.Sprite;     import flash.events.Event;     import flash.display.DisplayObject;     import flash.display.MovieClip;

public class Test extends Sprite
{
    public var container:Sprite;        
    public var thing:Thing; // a Class that extends Sprite that creates a thingGraphic:ThingMovieClip in it from test.fla library
    public var thing2Graphic:Thing2MovieClip; // base class is MovieClip, in test.fla library

    public function Test()
    {           
        container = new Sprite();
        addChild(container);            

        thing = new Thing();
        container.addChild(thing);

        thing2Graphic = new Thing2MovieClip();
        addChild(thing2Graphic);  // adds a Thing2 to stage I presume

        addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }

    public function update(e:Event):void
    {
        //  trace(container.thing.thingState);              // Access of possibly undefined property thing through a reference with static type flash.display:Sprite
        if(container.thing.thingState == container.thing.thingState.OFF_)
        {
            trace("container.thing.thingState = OFF_");
        }  //1119:Access of possibly undefined property thing through a reference with static type flash.display:Sprite
        //  trace(container.MovieClip(thing).thingState);     //1061: Call to a possibly undefined method MovieClip through a reference with static type flash.display:Sprite
        //  var child:DisplayObject = container.getChildByName(thing);  //Implicit coercion of a value type com.hello.test.test:Thing to an unrelated type String
        //  trace(child);
    }
}       

}

包com.hello.test.test {     import flash.display.Sprite;     import flash.events.MouseEvent;

public class Thing extends Sprite
{
    public var thingGraphic:ThingMovieClip;
    public static const ON_:String = "on_";
    public static const OFF_:String = "off_";
    public var thingState:String;

    public function Thing()
    {
        thingState = OFF_;
        buttonMode = true;
        useHandCursor = true;

        thingGraphic = new ThingMovieClip();
        thingGraphic.stop();
        addChild(thingGraphic);

        this.addEventListener(MouseEvent.MOUSE_DOWN, mousePressed, false, 0, true);
    }

    public function mousePressed(e:MouseEvent):void
    {
        trace("pressed");
        thingState = ON_;
        trace("thingState " +thingState);
        thingGraphic.play();

        //this.parent.thing2Graphic.play();     // how can I access the thing2 at Test.as (this Class's parent)
        // 1119: Access of possibly undefined property thing2 through a reference with static tpe flash.display:DisplayObjectContainer
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased, false, 0, true);
    }

    public function mouseReleased(e:MouseEvent):void
    {   
        thingState = OFF_;
        trace("thingState " +thingState);
        stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);
        thingGraphic.stop();
    }

    public function update():void
    {
        trace("updated");
    }   
}

}

我认为我缺少对ActionScript 3的一些基本但有趣的理解,我也没有其他脚本/编程语言的其他知识。 当我完全按照教程看起来它们看起来很简单,但是然后开始应用我认为是逻辑用法然后它会引发错误.. 任何有关简单理解的书籍推荐,即技术但用AS3的正常语言,因为我有基础AS3.0动画,制作动作(伟大的书,保持概念;希望有一本关于AS3本身的书)和Flash游戏基本指南(非常技术性,几乎不能按原样)??

感谢,

2 个答案:

答案 0 :(得分:0)

以下是我对您评论错误的解释。

  1. 尝试通过container.thing.prop
  2. 访问该属性,而不是thing.prop
  3. 而不是container.MovieClip(thing),请尝试var newThing = thing as MovieClip
  4. getChildByName期望String(认为实例名称也有效)。您正在为它提供一个扩展Sprite类的自定义类。
  5. 将父类中的MOUSE_DOWN侦听器添加到thing变量。
  6. //(inside Test.as)
    
    thing.addEventListener(MouseEvent.MOUSE_DOWN, mousePressed, false, 0, true);
    
    private function mousePressed(evt:MouseEvent):void
    {
        thing2Graphic.play();
    }
    

答案 1 :(得分:0)

在Test.as中,只需使用成员变量:

trace (thing.thingState);

在你的Thing.as中,将parent.parent对象(容器的父对象)强制转换为Test

var p:Test = parent.parent as Test;
p.thing2Graphic.play();