0

I know the title of this question is quite vague, but it's hard to explain it in a few words.

I have two non-document classes, Animation and SmartItem. SmartItem, which extends MovieClip class, has an Animation variable to access to Animation's functions, just like MovieClip.graphics, which lets you access to flash.display.Graphics.

I would like to do the same thing with SmartItem, but with a function of the class Animation.

Let's say I have the function called LinearTween(x0:Number,y0:Number,xF:Number,yF:Number,seconds:Number,easing:Function);, which creates two Tween objects based on the SmartItem used to call the LinearTween function.

mySmartItem.animation.LinearTween(0,0,10,10,1,None.easeNone)

In this case it will create two Tween objects based on mySmartItem.

The problem is that I don't know how to access to this object inside the Animation class in order to create the Tween object. The resulting Tween objects should look like this:

var myTweenX:Tween = new Tween(/*mySmartItem*/,"x",x0,xF,None.easeNone,seconds,true);
var myTweenY:Tween = new Tween(/*mySmartItem*/,"y",y0,yF,None.easeNone,seconds,true);

How can I reference to mySmartItem in the class Animation? I tried using an instance of Stage in Animation and tried to access to mySmartItem, and gave me runtime error 1009.

Animation.as:

package myStudio.basic {
    import fl.transitions.Tween;
    import myStudio.basic.CoreModus;
    import myStudio.dynamic.SmartItem;
    import flash.utils.*;
    public class Animation {
        import fl.transitions.easing.*;
        import flash.display.Stage;

        public var anim:Tween;
        public var modus:String;
        static var STAGE:Stage;

        public function Animation() {

        }
        public function LinearTween(x0:Number,y0:Number,xF:Number,yF:Number,seconds:Number):void {
            var myTweenX:Tween = new Tween(not_set_yet,"x",None.easeNone,x0,xF,seconds,true);
            var myTweenY:Tween = new Tween(not_set_yet,"y",None.easeNone,y0,yF,seconds,true);
            function play():void {
                myTweenX.start();
                myTweenY.start();
            }
            trace("Linear Tween");
        }
    }
}

Note: I put not_set_yet as a placeholder where I'm supposed to reference to the object.

SmartItem.as:

package myStudio.dynamic {
    import flash.display.MovieClip;
    import myStudio.basic.Animation;
    public class SmartItem extends MovieClip {
        public var animation:Animation;
        static var _instance:SmartItem = null;
        public function SmartItem():void {
        }

    }

}

2 Answers 2

1

If you are trying to do something similar to the Graphics object any time you instantiate an animation object you should pass in a reference of the parent SmartItem class.

That would look something like this in your SmartItem class:

package myStudio.dynamic {
import flash.display.MovieClip;
import myStudio.basic.Animation;
public class SmartItem extends MovieClip {
    public var animation:Animation;
    static var _instance:SmartItem = null;
    public function SmartItem():void {
        animation = new Animation(this)
    }

}

}

And then in your Animation class constructor:

private var _smartItem:DisplayObject;

public function Animation( smartItem:DisplayObject ) {
    this._smartItem = smartItem;
}

At this point you could just use that member variable _smartItem in your Tween functions:

var myTweenX:Tween = new Tween(this._smartItem,"x",None.easeNone,x0,xF,seconds,true);
var myTweenY:Tween = new Tween(this._smartItem,"y",None.easeNone,y0,yF,seconds,true);
Sign up to request clarification or add additional context in comments.

2 Comments

This works for any DisplayObject object, that includes SmartItem objects. Would it be possible to restrict to SmartItem objects?
Sure just change the type to SmartItem in the animation constructor.
0

Unlike inner class in Java, helper classes in ActionScript has no access to member variables of nesting class, regardless of final or not. in order to obtain access to specific instance of them you have to do the same thing you would do to get access to member variables of nesting(or composite) class instance object - (public access modifier, pseudo-singleton static, providing reference in the constructor, etc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.