View Full Version : Linkage MC to Class in package?
panel
03-20-2007, 09:23 PM
Hi
I am creating package in File Test.as
package
{
import flash.display.*;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
}
}
public class Test2 extends Sprite
{
public function Test2()
{
}
}
}
I am importing this package
import Test;
I can create Test Object and Test2 Object
t = new Test();
t2 = new Test2();
My point is that flash 'see' booth classes here, but when I try to linkage mc in Library with class flash 'see' only Test Class.
When I try linkage to Test2 I am reciving error :eek:
A definition for this class not to be found on disk, so one will be automatically generated upon export.
Why Flash don't 'see' Test2 class and how to deal with it?
senocular
03-20-2007, 09:46 PM
Technically, Flash shouldn't even "see" Test2. Each class should have its own .as file as that file itself partly defines the class it contains. If you want to use Test2, you should put it in its own file. In fact its good practice to do that anyway.
(also you dont need to import your class unless its in a named package)
panel
03-20-2007, 10:19 PM
I understand but it seems iracional for me in this case :confused:
Packages should be used to group classes. Yes?
I would be nice to have one main class and few 'decorator' clases in one file?
But more confusing for me is fact that
Technically, Flash shouldn't even "see" Test2.
It looks like Bug to me....becouse completly I don't see pioint here...those 'differences' are confusing :confused:
Each class should have its own .as file as that file itself partly defines the class it contains.
In this case do we need packages at all?
senocular
03-20-2007, 11:09 PM
Packages are for organization. They give you a location to store ActionScript files not only on your file system but also in code. You use packages to group like classes and to prevent naming conflicts with other classes. They are not required but are often very helpful, especially in larger projects and when reusing or distributing code.
The fact that Flash 9 Alpha can recognize more than one class in a single package block in a single file is just a "feature" of that app's compiler. Flex 2, however, will not recognize those classes, and since Flex 2 is a production-quality application (and not an alpha) you should be using it as a model for class definitions. There is no assurance that the next release of Flash will support multiple classes in one class block in one class file.
For more information, see a previous discussion of the package block here:
http://www.actionscript.org/forums/showthread.php3?t=128936
panel
03-21-2007, 10:40 AM
Thanks senocular you were very helpfull.
As you said I have split classes into separate files (ClassName.as) and created package in each file.
When package dosen't have name everything is ok
package
{
import flash.display.*;
public class test extends MovieClip
{
public function test()
{
trace("created");
}
}
}
t1 = new test()
this.addChild(t1)
Now when I am trying to add package name
package myPackage
{
import flash.display.*;
public class test extends MovieClip
{
public function test()
{
trace("created");
}
}
}
import myPackage.*;
t1 = new test()
this.addChild(t1)
Flash 'see' test class, becouse constructor is called, but also throws error:
ReferenceError: Error #1065: Variable test is not defined.
I can still linkage my 'library MC', but becouse of this error mc dosen't apear on stage
senocular
03-21-2007, 02:42 PM
is the class saved in the folder myPackage?
panel
03-21-2007, 03:10 PM
is the class saved in the folder myPackage?
Thanks again
:) :) :)
dr_zeus
03-21-2007, 05:24 PM
If your class Test2 is a helper class that should only be used by Test1, you can do something like this:
package
{
import flash.display.*;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
}
}
}
class Test2 extends Sprite
{
public function Test2()
{
}
}
Notice that Test2 is outside the package block and it doesn't have an access keyword like public. This class is now only accessible by Test1, and no other class can use it.
panel
03-21-2007, 05:56 PM
I have another problem - 2 simple classes:
package myPackage
{
import flash.display.*;
public class Fruit extends MovieClip
{
public var _weight:Number;
public function Fruit()
{
}
public function getWeight():Number
{
return _weight;
}
}
}
package myPackage
{
import flash.display.*;
public class Banana extends Fruit
{
public function Banana(weight:Number)
{
_weight = weight;
}
}
}
Everything works fine until I linkage 'Library MC' to Fruit Class. Then all properties/methods in Fruit class act like private :confused:
Is there any way to solve this problem and get access to Fruit methods?
senocular
03-21-2007, 06:23 PM
I dont see anything wrong with that. What error are you getting?
panel
03-21-2007, 06:31 PM
ReferenceError: Error #1065: Variable getWeight is not defined.
at myPackage::Banana$iinit()
at Timeline0_ff20f8689c3138429637e332a82fbea/::frame1()
I have attatched files. Remove Linkage from MC and it will start to work
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.