Edit visually created class in AS3
I have some class that I created visually in Flash Professional CS5 by transferring drawn result into a MovieClip and extorting symbol i开发者_StackOverflow社区nto AS code. Now, I want to add some properties to the class. To be more specific, I've created Ball class and want to add to it variables that will represent vertical and horizontal speeds. How can I edit the class in order to do it? Also, when I wanted to add some visible external element to the class, at first I decided to add MovieClip property in the code itself, but then recalled method .addChild() that worked perfectly for me. Now, I can't do the same with non-visible properties. So, what is the difference between adding visible elements using .addChild() and adding them in the code itself? And HOW can I actually add anything in the code itself?
What you need to do is create an external ActionScript file to hold the class definition. Make a file, call it Ball.as, and fill it with
package {
import flash.display.*;
import flash.events.*;
public class Ball extends MovieClip {
public var vx:Number = 0;
public var vy:Number = 0;
// add more variables here
public function Ball() {
}
// add more functions here.
}
}
In that class, you can put all of the functions and variables you want, and be able to access them from Timeline code. Put this in the same folder as your .fla and export your ball MovieClip as Ball, extending flash.display.MovieClip. Flash will find the definition for the class and use it in place of the generated one.
Note:
- if/when you add more functions or variables, don't forget to add access modifiers. (public, private, protected)
- If you change the folder in relation to the .fla, you'll need to change the package too, so, if you made a com/mygame folder for all the source for your game, you'd change the first line to "package com.mygame {" and export the MovieClip as "com.mygame.Ball".
- You might have to change the class to a dynamic class, by adding the keyword "dynamic" between public and class. What this means is that you can add properties to the class at runtime, but for a speed penalty.
I don't understand what you mean by "adding non-visible properties". The addChild() function should work from everything that subclasses DisplayObjectContainer (Sprite and MovieClip, notably) and should work on everything that subclasses DisplayObject (including Sprites, MovieClips, Bitmaps, TextFields, and Shapes)
You should probably avoid mixing physics with the actual visual representation. Try making a ball class which holds the velocityX
, velocityY
and a reference to your MovieClip
. Make the Ball class hold an update()
, which might look something like this:
public function update() : void {
ballMovieClip.x += velocityX;
ballMovieClip.y += velocityY;
}
In your Main class you can use the Event.ENTER_FRAME
-event to call ball.update();
This way you can independently update you Ball graphics and the physics code.
You can modify your Flash-imported class by extending it or wrapping a container Class around it.
This wrapper was my method of choice in 2 of my games. I used it to add various draw-related properties or additional objects.
public class WrapperOverSprite extends Sprite
{
private var instanceFromFlash: SomeClassTypeFromFlashViaSWC: new SomeClassTypeFromFlashViaSWC();
//Add whatever other variables you wish.
public function WrapperOverSprite ()
{
//Do stuff to your object.
}
//Can add whatever functions you wish...
}
Needless to say, this method, I used by exporting objects from Flash via a SWC file which I imported in Flash/Flex Builder.
精彩评论