Simple question/problem from a newbie to 3 (please help me!)

I’m sure this’ll take no time at all for someone a little more versed but it’ll help me no-end in learning the dreaded ‘as3’.

I’ve an fla that defines a doc class as Main and the following two classes in the same directory:

[SIZE=1][COLOR=dimgray]Main.as:[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]package {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]import flash.display.Sprite;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]import flash.display.MovieClip; [/COLOR][/SIZE]

[SIZE=1][COLOR=navy]public class Main extends Sprite {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]public function Main():void {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]trace(“hello”);[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]doBuild();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy][COLOR=darkred]MainText();[/COLOR][/COLOR][/SIZE]
[COLOR=navy][SIZE=1]}[/SIZE][/COLOR]

[SIZE=1][COLOR=navy]public function doBuild():void {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]//build the main page[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var Navigation2:Sprite=new rightPanel2();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]addChild(Navigation2);[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]trace("NAV2 = " + getChildIndex(Navigation2));[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]Navigation2.x=832;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]Navigation2.y=2.4; [/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var Navigation:Sprite=new rightPanel();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]addChild(Navigation);[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]trace("NAV = " + getChildIndex(Navigation));[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]Navigation.x=832;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]Navigation.y=2.4; [/COLOR][/SIZE]

[SIZE=1][COLOR=navy]}[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]}[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]}[/COLOR][/SIZE]

[SIZE=1][COLOR=dimgray]MainText.as:[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]package {[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]import flash.display.MovieClip; [/COLOR][/SIZE]

[SIZE=1][COLOR=#000080]public class MainText extends MovieClip {[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]public function MainText() { [/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]trace(“ok”);[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]

When I test the app I get an error 1112: Arguement count mismatch?!?

All I’m trying to do is call the MainText function from the Main function - both public classes - should be simple enough to do right? The doBuild function works (but this is in the same package as the Main func) so it’s something to do with communicating/calling between the packages but I just can’t seem to get it working.

Please help me - this’ll be a small thing that opens up a great deal to me!

Many thanks in advance,

Partimer

Your MainText.as contains a MainText class

A public function with a name identical to the class name is the constructor. The constructor should be run on instantiation of the class and is not called directly as a method.

If you want to instantiate the MainText class you need to declare a variable to reference it and create an instance using the ‘new’ keyword. (Though technically you can do it without declaring a var but you won’t be able to reference the class!)

So instead of


MainText();

Call


var mainText = new MainText();

Then you should get your output trace.

But I could be wrong, because that error message is a little obscure - unless MovieClip needs parameters in it’s constructor and your class extending movieclip doesn’t pass these…?

Well that certainly works and your explanation makes perfect sense.

Can’t thank you enough Charleh - I owe you one for that!

Sorry to be a pain but I’m lost again…!

So I’ve got the above working thanks to Charleh’s help - now rather than tracing a response I’m adding some dynamic text to the page - the results throw no errors but displays no text (although if this were the main document class it works fine?).

The new MainText.as is as follows:

[SIZE=1][COLOR=navy]package {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]import flash.display.MovieClip; [/COLOR][/SIZE]
[SIZE=1][COLOR=navy]import flash.text.*;[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]public class MainText extends MovieClip {[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]public function MainText() { [/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var css:StyleSheet = new StyleSheet();[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var body:Object = new Object();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]body.fontFamily = “Verdana”;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]body.textIndent = 0;[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var heading:Object = new Object();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.fontSize = 16;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.textIndent = 0;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.leading = 10;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.letterSpacing = 0;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.fontWeight = “bold”;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.color = “#333333”;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]heading.textAlign = “center”;[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]var byline:Object = new Object();[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]byline.fontSize = 14;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]byline.leading = 20;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]byline.fontStyle = “italic”;[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]byline.textAlign = “right”;[/COLOR][/SIZE]

[SIZE=1][COLOR=navy]css.setStyle(".heading", heading);[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]css.setStyle(".byline", byline);[/COLOR][/SIZE]
[SIZE=1][COLOR=navy]css.setStyle(“body”, body);[/COLOR][/SIZE]

[SIZE=1][COLOR=#000080]var txtFld:TextField = new TextField();[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.x =830;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.y =10; [/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.width = 100;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.multiline = true;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.wordWrap = true;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.selectable = false; [/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.embedFonts = true;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.autoSize = TextFieldAutoSize.LEFT;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.styleSheet = css;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.htmlText = “<body>”;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.htmlText += “<span class=‘heading’>Text</span>”;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]txtFld.htmlText += “</body>”;[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]addChild(txtFld);[/COLOR][/SIZE]

[SIZE=1][COLOR=#000080]trace(“done”);[/COLOR][/SIZE]

[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]
[SIZE=1][COLOR=#000080]}[/COLOR][/SIZE]

[SIZE=1][COLOR=#000080][SIZE=2][COLOR=black]It’s a steep learning curve from AS2 but I’ll be ■■■■■■ if I don’t make it up there![/COLOR][/SIZE]

[SIZE=1][COLOR=#000080]Many thanks in advance once again![/COLOR][/SIZE]

[SIZE=2][COLOR=#000000]Ptmr[/COLOR][/SIZE][/COLOR][/SIZE]

You are calling addChild() here within the MainText class - which is attempting to attach the object as a child to the MainText class, not the document class

Have you added the MainText instance to the document class using addChild?

Do you want to add the child directly to the document class or to MainText (i.e. child->docclass or child->child->docclass)?

If the former then pass in a reference to the document class in the MainText constructor and call addChild on this reference instead of on MainText

i.e. in MainText class


public function MainText(stage:Main) {
  stage.addChild(yourTextObject);
}

and in Main class


var mainText:MainText = new MainText(this);

Got me there… I’m basically just trying to add the formatted text to the stage (already added a couple of sprites with the doBuild). I think I’m falling down on this new display object - targetting where to point things to?

I added your above comments to both as files and again, no errors but no text being diplayed. I kind of understand what you’re saying here - passing in the reference but am at a little loss.

This may be very cheeky of me but I’ve uploaded a zip file here (7.5k) - if you could have a look and comment on just what I’m doing wrong I’d owe you a good few pints? If I’m out of line - let me know!

Many thanks again,

Partimer

Ignore the above - I’ve changed the font used from Verdana to Arial and it now appears - pretty dumb of me I know but I was only exporting the latter…

I’m making that curve steeper for myself!

Have a cracking weekend!