0
votes

test.fla's document class is Test.as test.fla's library has two MovieClips ThingMovieClip and Thing2MovieClip

I tried accessing the Thing.as public var thingState's property; directly, using something called Casting (??), assigning an instance name, but something that seemed so simple doesn't work, I commented the errors I got next to each way I tried,

conversely how would I access the MovieClip Thing2MovieClip from Thing.as (which is a child of Test.as because that's where it's instantiated), it just doesn't recognise an object of its parent exists, here are the 2 classes:

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

}

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

}

I think i'm missing some basic but fundemental understanding of ActionScript 3 and I have no other knowledge of other scripting/programming languages either. when I follow tutorials exactly they seem simple enough, but then start applying what I think is logical usage then it throws up errors.. any recommendations of books for a simple understanding, i.e technical but in normal language of AS3, since I have Foundation AS3.0 Animation, Making Things Move (great book, keeps to concepts; wish there was a book like this about AS3 itself) and The Essential Guide to Flash Games (very technical, can barely follow as-is)??

thanks,

2

2 Answers

0
votes

In your Test.as, just use the member variables:

trace (thing.thingState);

In your Thing.as, cast the parent.parent object (parent of the container!) to Test:

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

Here's my interpretation of your commented errors.

  1. Instead of container.thing.prop, try accessing the property through thing.prop.
  2. Instead of container.MovieClip(thing), try var newThing = thing as MovieClip.
  3. getChildByName expects a String (think instance names which also work). You're feeding it a custom class that extends the Sprite class.
  4. Add the MOUSE_DOWN listener inside the parent class to the thing variable.
//(inside Test.as)

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

private function mousePressed(evt:MouseEvent):void
{
    thing2Graphic.play();
}