PDA

View Full Version : Simple Question on Visual Custom Classes


RainGame
09-23-2007, 11:51 PM
To start off getting the feel for AS3, I wanted to make a simple gravity simulator. It would have one ball on the stage bounce around and off the walls. I followed a tutorial on how to do this in AS2 years ago (included concepts of gravity, hitTests on the walls, etc.).

Because of AS3's new structure and all, I figured the best idea would be to make a Ball class. The class would keep track of where the ball is on the stage, its current speeds, color, etc. That way I could add more balls easily, and change each of thier values seperately.

I have followed along with a few different tutorials on making my own Visual Class, and I understand them perfectly and assume they would work fine if I were to complete them. But I have one problem. These custom visual class tutorials only deal will simple shapes, usually circles. And though my shape for my project is indeed a circle, I know in the future I will need to do this for advanced shapes.

These tutorials always draw the shape using code like this:

private function draw( ):void {
graphics.beginFill( _color );
graphics.drawCircle( 0, 0, _radius );
graphics.endFill( );
}
I'd like to store my shape inside my Ball class, but somehow get the graphic from my library. I don't know if it is possible or if it is the right way to do it, but i figure if I can store the graphic in my Ball Class, I can assign and alter the x and y positions of the graphic in the Ball Class as well. I do not want to deal with the x and y positions on my main timeline. Is there any way to do this? Thanks.

senocular
09-24-2007, 02:20 AM
You'd write your class normally, but not include any drawing commands. Make your movie clip symbol in Flash but use its linkage properties (right click on the symbol in your library) and set up your class as the class name for that symbol. Done : )

RainGame
09-24-2007, 02:28 AM
Thanks, Senocular. I've been following the tutorial on your website and I must say it is good. But now I have another small little issue.

This is on the first frame of my .fla:

function Main()
{
var ball1:Ball = new Ball(275, 200);
addChild(ball1);
}

Main();

In my Ball constuctor function in the Ball class, I have this line:

trace(this.name);

I was expecting this to trace "ball1", but instead it gave me "instance2". This confuses me to no end.

senocular
09-24-2007, 02:34 AM
See the 2nd note on page 2
http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=2

eh, at second glance, it doesn't look like that once completely covers what you're experiencing...

At any rate, _name and name are not the same. name is just a variable now. Because display objects are treated like any other object, to set the name you have to... set the name. An object otherwise has no way to know what variable you used to store its reference as. So you're Ball instance can not tell that you've created a variable called ball1 to save it with. To actually set that as its name you have to set the name property by yourself (in AS2, attachMovie kind of did both... though really it was more about the weird behavior in AS2 where the _name value also doubled as a variable reference which is not the case in AS3)

RainGame
09-24-2007, 02:44 AM
Edit: Responded before you edited your post.

Thanks :) I fully understand this now. You are the man!