okay, with attachMovie I defined which property took each value. With the class, you just kind of listed all of the values, but didn't assign them to the property. Example...
The reason I ask all of this, is that I've been working on a tilemap game that needs to tell each tile exactly where to go, along with difining the characteristics of each creature/sprite/item/etc... I would love to convert the whole thing to Flash 9 if it works as well as everyone is saying, but if this simple task is made to be too complicated then it will have to wait.
If you download the example I posted just change the Square class, although I personally don't like to avoid to put anything into the constructor and recommend to make a separate function.
PHP Code:
package Setstage
{
import flash.display.MovieClip;
public class Square extends MovieClip
{
public function Square (x:Number, y:Number)
{
this.x = x;
this.y = y;
}
}
}
and change the MyStage class
PHP Code:
public function MyStage ()
{
var clip:Square = new Square (100,100);
this.parent.addChild(clip);
//
var clip_2:Square = new Square (300,300);
this.parent.addChild(clip_2);
tField = new TextField();
tField.text = "I am clip_2.";
clip_2.addChild (tField);
}
- The right of the People to create Flash movies shall not be infringed. -
perhaps I just need to study classes a bit more, but how would my movie know what to do with all of those values?
Okay, this is how it works. When you create a new instance, you pass it some values/references, just like when calling a function. For instance :
var myMovieClip:MyMovieClip=new MyMovieClip(1,2);
In your class definition, there will be a constructor-function. This constructor is called when the object is created, with the values you pass above. So for instance, this could look like this :
Code:
package {
class MyMovieClip extends MovieClip {
private var value1:int;
private var value2:int;
public function MyMovieClip (value1:int,value2:int=0) {
// note that the constructor has the same name as the class!
// also, I gave value2 a default value of 0, so you can instantiate this class with only one parameter
this.value1=value1; // assign the first parameter to the property value1
this.value2=value2; // assign the first parameter to the property value1
}
}
}
Thanks for the examples. It seems like a lot of work to do something that was so simple in AS2. I haven't downloaded AS3 yet, but probably will this weekend.
One last question...
If I have say, 500 different creatures in my tilemap game and they all behave the same except for the values that I give them upon entering the stage, would they all still need their own class or could all of them share the same class?
You made multiple instances of a Square, but should I want to make an instance of a Square, and a Circle, and a Triangle, would one class be able to do all that?
so 3 separate, completely different, movieclips, can all use the same class?
sorry if I'm not understanding completely. I think I'll be spending my weekend learning classes and AS3
In former flash versions you put a square movieclip, a circle movieclip etc in the library and use attachMovie. Here you do the same thing and just make a class for each movieclip. Every object is a class.
- The right of the People to create Flash movies shall not be infringed. -
That's a lot of classes. The tiles I can put in one movieclip and have it gotoAndStop("whatever_tile"); but with the sprites & creatures, it seems like my script is going to suddenly double in size.
If it performs that much better though, then I suppose it's worth it.
Well in my game right now, for example, I have an Enemy class, and then a bunch of classes that extend the Enemy class. Most of the code for all of these classes is within the Enemy class, and then each different enemy's class contains different variable's and maybe one or two overrided functions.
That's a lot of classes. The tiles I can put in one movieclip and have it gotoAndStop("whatever_tile"); but with the sprites & creatures, it seems like my script is going to suddenly double in size.
If it performs that much better though, then I suppose it's worth it.
I recommend that you first download the Flash 9 preview, play around and then come back.
- The right of the People to create Flash movies shall not be infringed. -
Thanks for the examples. It seems like a lot of work to do something that was so simple in AS2.
Classes aren't a new thing, and creating classes was about the same amount of work in AS2 as it is now in AS3. Sure, you could do without them if you wanted to, but once you know how to use them properly, you'll see that it's actually simpler, easier and cleaner to get things done. You might have to type a bit more and create more files, but you'll be able to find stuff faster, reuse classes, and structure things better.
If I have say, 500 different creatures in my tilemap game and they all behave the same except for the values that I give them upon entering the stage, would they all still need their own class or could all of them share the same class?
In the current version of Flash9 (which only an apla), you need a class per library item, you can't use the same class on more than one library item. However, you can make multiple classes that extend the same class so the code only has to be written once.
I do hope that in the final version, you can specify an 'extends' property in the linkage settings to use in combination with auto-generated classes. That would be handy.
I recommend that you first download the Flash 9 preview, play around and then come back.
Working on that now, thanks
Thank you too Fall X for your responses. I know classes are in AS2, I just haven't had much need for them until now. I'm sure I'll start using them and then wonder why I hadn't learned earlier.
Ok, great example by the way. Though as i am trying to implement my own class, i am having trouble.
For example:
Frame1 of index.fla
Code:
stop();
import AS.UIMng.Index;
var objIndex = new Index();
Inside MainDir/AS/UIMng/Index.as
Code:
package AS.UIMng
{
import flash.display.MovieClip;
public class Index
{
public function Index ()
{
}
}
}
output:
Code:
**Error** C:\~~\asv301\AS\UIMng\Index.as: Line 1: Syntax error.
package AS.UIMng
**Error** C:\~~\asv301\AS\UIMng\Index.as: Line 4: Attribute used outside class.
public class Index
**Error** C:\~~\sv301\AS\UIMng\Index.as: Line 10: ActionScript 2.0 class scripts may only define class or interface constructs.
}
Total ActionScript Errors: 3 Reported Errors: 3
I've tried many combo's in the AS file.. wish no luck on the proper syntax.
Any tips? I have attached the two files.
*edit*
Fixed.
Aparently (least i assume) i was overlooking the part of the error that says "ActionScript 2.0". I didn't realize that even though the button is not disabled, the syntax checker does not work.
Personally, I think it AS3 is that much better. The two main reasons I've started using AS3:
Firstly performance. AS3 is so much faster - particularly with function calls. In AS2 the function calls were really too slow for game code or 3d stuff. The work around for AS2 is to write really large blocks of inline code which unfortunately, are impossible to maintain or debug.
Secondly, AS3 requires a more disciplined way of working, but in return provides better error checking, which means you're more likely to write code that works exactly as expected. One of the problems with Flash is the poor quality of code most ActionScript developers write - too many projects are launched with bugs; most are impossible to maintain; and forget extending a live project, try to add more functionality and something unrelated stops working; yadda, yadda, yadda...
[edit] I forgot to mention the display list - getNextHighestDepth is dead - long live addChild. At last we have robust z ordering! [/edit]
There really isn't anything I don't like about AS3 - ok you do end up writing more classes but the payoff is worth it.
I'm not so keen on everything about Flex 2 though. I still don't like swc components and they are much more important in the Flex environment. Admittedly you can create stuff in minutes that would take weeks without components. But too many of the Adobe supplied components contain bugs, I don't get why the flash player doesn't use OS UI widgets - so much of the work that goes in to component development is just reinventing the wheel.
i get these errors when I push the 'checkmark' in the ActionScripting window:
**Error** C:\Documents and Settings\XP User\Desktop\Donkey Rocket II - v.9\AS\player\weapons\shots\Bomb.as: Line 1: Syntax error.
package AS.player.weapons.shots{
**Error** C:\Documents and Settings\XP User\Desktop\Donkey Rocket II - v.9\AS\player\weapons\shots\Bomb.as: Line 19: ActionScript 2.0 class scripts may only define class or interface constructs.
}
why does it say its a AS 2.0 script? I changed the publish settings to 3.0...?
Why don't you start with something simpler until you've learnt the new language?
probably what ill end up doing. I just want this project to turn out as good as possible, and the speed increase AS 3.0 gives is definitly worth the work. What I'm doing is not really that complicated. I have a few basic hierarchys for my packages, and not that many sophisticated functions in my files. I'm a computer science major, and I should be able to do these types of things.