views:

202

answers:

3

Hi there,

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.

+2  A: 

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.

Charlie boy
Thank you! I appreciate the additional perspective and will test this out as well.
Structure
+5  A: 

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!");
        }
    }
}
shortstick
Thank you very much, I need to read more about constructors versus methods. I see my mistake but will need to run a few more tests to nail it down.
Structure
on the basic level a constructor is a method call that is used as soon as the class is made, it needs to be named exactly the same as the class name for it to be run correctly. any other function needs to be called specifically, for instance you can do this init function by calling faceInst.init() after the addchild() call instead of using the event handler inside the class.
shortstick
+1  A: 

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.

Amarghosh
Yes, I expected that this method was automatically called when the object was instantiated.I appreciate the clarification between the constructor and method functionality - that was the reason that I did not see my error. I will need to do some trial and error with naming methods and constructors to better grasp how they are called and connected to each other.I will note the casing standards in the future as well.
Structure