# New to AS3 and new to classes

**URL:** <https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247>\
**Category:** Uncategorized\
**Created:** [July 6, 2008, 11:23pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247 "2008-07-06T23:23:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![2nd\_day](https://avatars.discourse-cdn.com/v4/letter/2/a4c791/32.png) [@2nd\_day](https://forum.kirupa.com/u/2nd_day)\
**Post date:** [July 6, 2008, 11:23pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/1 "2008-07-06T23:23:06Z")

</div>

Hi there,

when I picked up a flash project recently I could notice that I havent been using Flash / Actionscript for a long time. I took a lot of time for things I used to do much quicker. Now Im busy trying some other flash project and I thought it was a good idea to totally forget AS1.0 and just learn AS3. Now I also havent used classes in Flash, but after some php project where I did use classes i thought that would be handy as well…

Well… I actually got 3 problems…

1. I get an error about arguments (0 expected) This is when I create a new Post object

2. In the main function of a class I have a trace function wich isnt executed

3. some problems with dynamic variables / arrays… In a for function I want to shorten down a array to a variable like this:  
var a:Array = this[“post+i”];  
trace (a[0]);

but that doesnt work…

Here’s the code I use:

```auto

// code from Post.as
package {
	import flash.display.*;
	import flash.events.*;
	
	public class Post extends MovieClip {
		var background_color:String;
		var title_txt:String;
		var post_txt:String;
		var id:Number;
		var order:Number;

		public function Post(colorP:String, titleP:String, postP:String, idP:Number, orderP:Number) {
			background_color = colorP;
			title_txt = titleP;
			post_txt = postP;
			id = idP;
			order = orderP;
			trace("This code runs when the Test class is instantiated");
		}
		function move_up():Void {
			_parent.reorder(this.order, 1);

		}
		function move_down():Void {
			_parent.reorder(this.order, 0);

		}
		function change_order(order:Number):Void {
			trace(this.order);
			this.order = order;
		}
	}
}

// code from my .fla file
var post0:Array = new Array(1, 1, "Testpost", "Dit is een test post");
var post1:Array = new Array(2, 2, "Testpost2", "Dit is nog een test post");
var post2:Array = new Array(3, 3, "Testpost3", "Dit is de laatste test post");
var postarray:Array = new Array(post0, post1, post2);

for(var i = 0; i<postarray.length; i++){
	//var a:Array = this["post+i"];
	//trace (a[0]);
	var newitem:Post = new Post("black", this["post+i"][2], this["post+i"][3], this["post+i"][0], this["post+i"][1]);
	list.addChild(newitem);
}

```

I hope you guys can help me out…

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [July 6, 2008, 11:59pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/2 "2008-07-06T23:59:55Z")

</div>

A few things from a brief glance:

\_parent does not exist in that form in AS3. It’s just parent - no underscore - but you should be avoiding explicit display referencing as you are there if possible.

this[“post+i”] should be this[“post”+ i] - the former is interpreted as a string literal, the latter as a concatenation.

For performance, you should do something like

```auto
var postLength:uint = postarray.length;
for (var i:uint = 0; i < postLength; i++)
[...]

```

- otherwise postarray.length is evaluated with each iteration. Also, strongly typecasting things even as simple as loop iterators is very much encouraged with AS3 for both code cleanliness and error checking.

Void is no longer capitalised - it’s just void.

So that’s a few things… see how you go 🙂

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 7, 2008, 12:06am UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/3 "2008-07-07T00:06:06Z")

</div>

1. Have you saved the .as file recently? If so, try Control \> Delete ASO Files. (and make sure that you’re using the correct file)

2. If the above doesn’t fix it, try Publish Settings \> Flash \> Omit trace actions.

3. It should be:

```auto
trace(this['post' + i]);

```

1. Use _void_ instead of _Void_.

Edit: Oops, too slow. :trout:

---

<div class="post-metadata">

**Author:** ![2nd\_day](https://avatars.discourse-cdn.com/v4/letter/2/a4c791/32.png) [@2nd\_day](https://forum.kirupa.com/u/2nd_day)\
**Post date:** [July 7, 2008, 7:27pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/4 "2008-07-07T19:27:19Z")

</div>

Thanks a lot guys, learnt some important things.  
I feel really stupid for that [“post”+i]… I knew that one… just a typo i didnt see…

I still got 1 problem left… Flash doesnt recognize my functions in the class. And it also doesnt execute the trace statement in the main function. So I guess there’s something wrong with how I embed my .as with my .fla. Maybe one of you guys can look at it?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 7, 2008, 9:23pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/5 "2008-07-07T21:23:54Z")

</div>

> [@2nd day](#):
>
> And it also doesnt execute the trace statement in the main function.

You should have looked at my second suggestion:

> [@Krilnon](#):
>
> 1. If the above doesn’t fix it, try Publish Settings \> Flash \> Omit trace actions.

In the .fla that you posted, the box is checked when it shouldn’t be checked.

For the Post methods to be callable from your .fla, they should be in the public namespace:

```auto
package {
	import flash.display.*;
	import flash.events.*;
	
	public dynamic class Post extends MovieClip {
		public var background_color:String;
		public var title_txt:String;
		public var post_txt:String;
		public var id:Number;
		public var order:Number;

		public function Post(colorP:String, titleP:String, postP:String, idP:Number, orderP:Number) {
			background_color = colorP;
			title_txt = titleP;
			post_txt = postP;
			id = idP;
			order = orderP;
			trace("This code runs when the Test class is instantiated");
		}
		public function move_up():void {
			//parent.reorder(this.order, 1);

		}
		public function move_down():void {
			//this.parent.reorder(this.order, 0);

		}
		public function change_order(order:Number):void {
			trace(this.order);
			this.order = order;
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [July 7, 2008, 9:30pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/6 "2008-07-07T21:30:29Z")

</div>

Heh, that’d be a nasty trick to do to someone’s FLA while they weren’t looking. _OMIGAWD NOTHING WILL TRACE._ :fight:

---

<div class="post-metadata">

**Author:** ![2nd\_day](https://avatars.discourse-cdn.com/v4/letter/2/a4c791/32.png) [@2nd\_day](https://forum.kirupa.com/u/2nd_day)\
**Post date:** [July 7, 2008, 11:03pm UTC](https://forum.kirupa.com/t/new-to-as3-and-new-to-classes/265247/7 "2008-07-07T23:03:42Z")

</div>

Thanks Krilnon!  
I wasnt sure what omit trace actions meant, so I check and unchecked it several times… Didnt know it had to be unchecked…  
Thanks again for the information!
