Reputation: 57
I was attempting to use this and this to figure out how to set up some math classes (the sin/cos functions I use for anything top down.) but I have it set to import package.class (the names of course.) but It said that "defintion package:Class not found". This confuses me and I am going to assume I have to do something with folders. However I'm not sure what folders to put it in/what to do to enter things specifically in a folder.
//import sthreets.CustomFuncs;
private function Movement()
{
if(LEFT==true)
{
rotation=rotation-6;
}
if(RIGHT==true)
{
rotation=rotation+6;
}
if(UP==true)
{
//x=x+CustomFuncs.TopDownMove("x", rotation, 0);
//y=y+CustomFuncs.TopDownMove("y", rotation, 0);
}
if(DOWN==true)
{
//x=x-CustomFuncs.TopDownMove("x", rotation, 0);
//y=y-CustomFuncs.TopDownMove("y", rotation, 0);
}
}
Commented out stuff was giving me errors, here's the CustomFuncs code.
package sthreets
{
public class CustomFuncs
{
public function CustomFuncs()
{
}
public function TopDownMove(xy:String, rot:Number, offSet:Number):Number
{
if(xy=="x")
{
return cos(DegreesToRadions(rot)+offSet)
}
if(xy=="y")
{
return sin(DegreesToRadions(rot)+offSet)
}
}
public function DegreesToRadions(rot:Number):Number
{
return rot*Math.PI/180;
}
}
}
Upvotes: 0
Views: 140
Reputation: 8149
Package names are determined by the location from src. src/my/package/name
is my.package.name
. The package you import at the top of any class must match the actual package, otherwise the compiler will not find the class.
So... say you have this structure in your folders:
src
->my
->package
->name
->ClassName
The class would be set up as so:
package my.package.name {
public class ClassName {
public function ClassName(){
//construct
}
}
}
And you would import it using:
import my.package.name.ClassName;
or
import my.package.name.*; //only use the wildcard if every class in that package is being used, otherwise you will include code you may or may not need to in your project
Hopefully that helps explain packages and imports a bit.
I also noticed that you are using the functions in your CustomFuncs
class as if they were static (as you would with Math.cos()
or similar). Your class is not set up to work this way. To do that, you need to use this static
access modifier.
package sthreets
{
public class CustomFuncs
{
public static function TopDownMove(xy:String, rot:Number, offSet:Number):Number
{
if(xy=="x")
{
return cos(DegreesToRadions(rot)+offSet)
}
if(xy=="y")
{
return sin(DegreesToRadions(rot)+offSet)
}
}
public static function DegreesToRadions(rot:Number):Number
{
return rot*Math.PI/180;
}
}
}
The static access modifier means that the object (in this case, the functions) only exist once in any instance of the app, whereas a standard access modifier (public
, private
, protected
, internal
, mx_internal
, final
) create one object for every single instance of the class. Using the static
access modifier allows you to access objects through ClassName.objectName
because the objects belong to the class, not to the parent object. To get to those objects, you never have to instantiate (you will notice in the above code that I removed your constructor because it is unnecessary in this case).
Note: The following isn't necessarily directed at you, but I am including it for any future readers who might visit this question
This blog post gives a fairly good rundown of the standard access modifiers. The comparisons it makes in relation to beer are incredibly good and incredibly easy for someone with limited knowledge about the modifiers to understand.
I also suggest reading this Wikipedia article on "Static Variable" to help you understand what a static object is and where and how to use it.
AS3 strictly follows OOP (object oriented programming) and ECMAScript policies and its syntax is based heavily on Java. For these reasons, nearly every principle used by other OOP or ECMA languages applies to AS3 as well. This means that you can improve your AS3 skills by reading up on these principles, even if they are not specifically for AS3 (AS3 OOP tutorials are fairly limited in both quantity and quality so it can be difficult for someone who is learning AS3 as their first OOP language to learn these principles).
Upvotes: 1