Copy link to clipboard
Copied
I am trying to learn how to use classes.The book i am using gave me this example.
In Vechile.as
package{
import flash.display.MovieClip;
import flash.events.Event;
public class Vehicle extends MovieClip{
public var _gasMileage:Number;
public var _fuelAvailable:Number;
public var _milesTraveled:Number =0;
public var _go:Boolean;
public function Vehicle(mpg:Number=21, fuel:Number=18.5){
_gasMileage=mpg;
_fuelAvailable = fuel;
this.addEventListener(Event.ENTER_FRAME, onLoop, false,
0, true);
}
public function onLoop (evt:Event):void{
if (_go){
_fuelAvailable--;
_milesTraveled+=_gasMileage;
if (_fuelAvailable < 1){
this.removeEventListener(Event.ENTER_FRAME,
onLoop);
}
trace(this, _milesTraveled,_fuelAvailable);
this.x=_milesTraveled;
}
}
public function go():void{
_go = true;
}
}
}
In the main.fla
In frame 1
var vehicle:Vehicle = new Vehicle(21,18.5);
addChild(vehicle);
vehicle.go();
when I compile it gives an output error.
Error #2136: The SWF file file:///C|/Users/Ravenwind/Documents/Main.swf contains invalid data.
at Vehicle/frame1()
I am stumped
" I then assign Vehicle to document class box in the property box" Do not assign the Vehicle as the document class... remove it from there.
In the main file, if you didn't already create one, create a movieclip symbol (just draw a shape in it for now). Right click the symbol in the library and select the Linkage option. In the interface that appears, select the option to Export for Actionscript, and assign it the Class name "Vehicle" in the field labeled "Class"
Make sure you save the Vehicle
...Copy link to clipboard
Copied
If I recreate the files and scenario that would support what you show, it works fine. Describe what you have done as far as creating/placing the files and the Vehicle object.
Copy link to clipboard
Copied
What i did is created Vehicle.as and typed in the above text. Then I created the Main.fla. I entered next three lines into script editor for frame 1. I then assign Vehicle to document class box in the property box. After that I hit Crtl + enter, and the output dialog box popped up the error.
Copy link to clipboard
Copied
" I then assign Vehicle to document class box in the property box" Do not assign the Vehicle as the document class... remove it from there.
In the main file, if you didn't already create one, create a movieclip symbol (just draw a shape in it for now). Right click the symbol in the library and select the Linkage option. In the interface that appears, select the option to Export for Actionscript, and assign it the Class name "Vehicle" in the field labeled "Class"
Make sure you save the Vehicle.as file after you put the code into it and have that file in the same folder as your Main.fla file.
If you do the things I just described you should see an instance of the movieclip moving across the stage when you run the file.
Copy link to clipboard
Copied
Thank you. That sure would have been a nice step for the book author to put in.
Copy link to clipboard
Copied
You're welcome