0
votes

This problem is probably very simple to solve but it is not clear to me. It may simply be that I am doing something incorrectly. I have studied OOP and AS3 for quite a few hours so I am familiar with the concepts but not the flow. This is a project that I put together in order to reinforce what I have been studying.

The goal here is to load an instance of a pre-created movieclip to the stage from the library then execute a positioning function in the FLA's timeframe ActionScript and execute a function from within the AS files's class ActionScript to both a resize the movieclip and output a trace.

I have two files: smileface.fla smileface.as

In smileface.fla, I have a MovieClip object that resides in my Library. It has the following relevant properties...

Name: faceInst
Class: smileface
Base Class: null

I have one frame (keyframe) and it contains the following ActionScript:

var faceInst:smileface = new smileface();
this.addChild(faceInst);
faceInst.x = stage.stageWidth/2;
faceInst.y = stage.stageHeight/2;

In my smileface.as file I have the following code:

package {
    import flash.display.MovieClip;
    import flash.display.Stage;
    public class smileface extends MovieClip {
        public function smileFunction() {
            this.width = stage.stageWidth/5;
            this.height = stage.stageHeight/5;
            trace("Done!");
        }
    }
}

I expect (with no grounds to do so) that after the movieclip object is loaded that it will resize per the specification and then the trace will be output.

However, what happens instead is that the face is displayed on the stage, centered, but is not resized and the trace is not output at all.

4

4 Answers

5
votes

If you are wanting this as the constructor it needs to have the same name as the file, smileface rather than smilefunction

as you are creating the instance before you are adding it to stage, the call for the stage width in the constructor is going to be null. you should move it onto an added to frame event listener:

package {
    import flash.display.MovieClip;
    import flash.display.Stage;
    public class smileface extends MovieClip {
        public function smileFace() {
        addEventListener(Event.ADDED_TO_STAGE, init);
        }

    public function init(ev:Event){
            this.width = stage.stageWidth/5;
            this.height = stage.stageHeight/5;
            trace("Done!");
        }
    }
}
2
votes

It seems that the function smileFunction() is never called (at least I can't see where it's called). Thecode should be:

var faceInst:smileface = new smileface();
this.addChild(faceInst);
faceInst.x = stage.stageWidth/2;
faceInst.y = stage.stageHeight/2;
faceInst.smileFunction();

That should make the function run and the smileface to center itself on stage and do the trace.

1
votes

I expect (with no grounds to do so) that after the movieclip object is loaded that it will resize per the specification and then the trace will be output.

I think your expectation is based on the fact that you remember seeing some code in which a method was automatically called. That is called constructor and such methods are named after the class name. Thus renaming the smileFunction to smileface should do the trick. Watchout for null errors for accessing stage from the constructor. Use addedToStage event listener instead.

Consider renaming the class to SmileFace - that is the convention. Classes are PascalCased and methods are camelCased.

1
votes

you forget to create constructor function and that must be same as the name of class name . your function is not called by any eventlistner nor any other function .

  package {
    import flash.display.MovieClip;
    import flash.display.Stage;
    public class smileface extends MovieClip {
        public function smileface():void{
         smileFunction();
         }
        public function smileFunction() {
            this.width = stage.stageWidth/5;
            this.height = stage.stageHeight/5;
            trace("Done!");
        }
    }
}