Why function files in package are not recognized? - actionscript-3

I have a package common.geometry with 2 files: calculateAngle and normalizeAngle. These files contain a single function, respectively:
package common.geometry {
import Graphics.Hexagon;
public function calculateAngle(hex1:Hexagon, hex2:Hexagon):Number {
var diffY:Number = hex2.center.y - hex1.center.y;
var diffX:Number = hex2.center.x - hex1.center.x;
var radians:Number = Math.atan2(diffY, diffX);
return normalizeAngle(radians * 180 / Math.PI);
}
}
and...
package common.geometry {
public function normalizeAngle(angle:Number):Number {
if (angle < 0) {
angle += 360;
}
if (angle > 360) {
angle = angle % 360;
}
return angle;
}
}
Edit
In other source files where I import common.geometry.* and I call calculateAngle the source compiles. But where normalizeAngle is called, I get a compiler error: Call to a possibly undefined method normalizeAngle.
I have tried explicitly importing common.geometry.normalizeAngle but this doesn't seem to work. What do I need to do so I can group global functions in packages?

no problem for me with this:
import common.geometry.calculateAngle;
calculateAngle();
as normalizeAngle and calculateAngle located in same location, and we only calling calculateAngle from out of the package, no need for importing normalizeAngle
Edit
I modified the original functions for clarity, removed arguments and replace bodies with simple trace commands.
normalizeAngle was called from calculateAngle body correctly

The problem is the reference to the Hexagon class. It was causing a circular reference I believe. Since it is the center property of the Hexagon, I changed the parameters to take Point objects instead and it is working.

Related

1120 Access of undefined property Ibar & Ipc

stop();
import flash.display.*;
this.stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, PL_LOADING);
function PL_LOADING(event: ProgressEvent): void {
var pcent: Number = event.bytesLoaded / event.bytesTotal * 100;
lbar.scaleX = pcent / 100;
lpc.text = int(pcent) + "%";
if (pcent == 100) {
this.gotoAndStop(2);
}
}
You'll do well to enable "Permit Debugging", however, the issue is on lines 14 & 15. It means you don't have an object in memory by those names. If you're working in the Flash IDE, and these are symbols in your library, add them to the stage, the and issue will be resolved.
Well, this code structure looks a bit disorganized. I'm not sure whether a lot of these changes will matter (since I currently don't have access to flash), but here is how I would typically write this:
stop();
import flash.display.*;
//no need for this.stop() if you've already stopped
this.loaderinfo.addEventListener(ProgressEvent.PROGRESS,PL_LOADING);
function PL_LOADING(e:ProgressEvent):void{
//possibly part of the problem is that the var keyword was on the wrong line
var pcent:Number = e.bytesLoaded/e.bytesTotal*100;
ibar.scaleX=pcent/100;
ipc.text=String(int(pcent)+"%");
//typically you want to make it clear that it's a string when dealing with text fields
if(pcent==100){
gotoAndStop(2);
}
}
Small changes like these as well as thoughtful organization could save your code. If you're still having problems with your code just notify me and I'll try to help you out.

how to change private var in imported action script

Hi guys am trying to change a var in an imported as file. Can anyone help point me out.
This is the original code in externalfile.as
private function SetNewPosition()
{
this.newX = this.GetRandomXPosition();
this.newY = this.GetRandomYPosition();
this.totalDistance = this.GetDistance();
var time:Number = this.totalDistance / this.speed;
speedX = (this.newX - this.x)/time;
speedY = (this.newY - this.y)/time;
}
Am trying to change the newX and newY from the Main.as Do share how i can fix this thanks!
I suppose, newX and newY variables are private in your .as file.
To change a variable by another .as you must follow one of two ways:
First:
Change the modifier of your variable (from private to public)
Second:
Define property get/set on your variable, so your variable remains private and you can manage the result sent by another .as file. For example if your newX can't be negative and in your Main.as you try to put a value less than zero, in your set property you can decide to put its value to zero, or apply an absolute value and so on.
You might want to ask yourself WHY you need Main to have access to the newX and newY variables. If Main is resetting the location of an external.as instance, you could add a 'reset' method to external.as which would keep newX and newY encapsulated. The aim is to try and keep any procedural coding with regard to external.as 'inside' that Class. If actions involving external.as can be run 'in private' inside the Class, that's where the code should be added. But if Main.as absolutely NEEDS access, add setter and getter methods for both newX and newY, eg...
public function set newX(value:Number):void {
this.newX = value;
}
public function get newX():Number {
return this.newX
}

AS3: Scoping issue with -warn-scoping-change-in-this

I wanted to enable all compiler warnings in Flex to resolve them in my code. But there is one warning which I can't figure out how to solve it. Here is some example code:
package lib
{
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
public class player
{
private function tmp(event:NetStatusEvent):void
{
}
public function player():void
{
super();
var connection:NetConnection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);
}
}
}
On compiling with -warn-scoping-change-in-this I'm getting the following warning:
/var/www/test/src/lib/player.as(16): col: 59 Warning: Migration issue: Method tmp will behave differently in ActionScript 3.0 due to the change in scoping for the this keyword. See the entry for warning 1083 for additional information.
connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);
Putting tmp as function inside player() will work but this is not what I want. I have even tried to use this.tmp as callback but there is no difference. Does somebody know how to solve this compiler warning?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/compilerWarnings.html
This is a code migration warning. This warning is generated when a method of an object is used as a value, usually as a callback function. In ActionScript 2.0, functions are executed in the context they are called from. In ActionScript 3.0, functions are always executed in the context where they were defined. Thus, variable and method names are resolved to the class that the callback is part of, rather than relative to the context it is called from, as in the following example:
class a
{
var x;
function a() { x = 1; }
function b() { trace(x); }
}
var A:a = new a();
var f:Function = a.b; // warning triggered here
var x = 22;
f(); // prints 1 in ActionScript 3.0, 22 in ActionScript 2.0
That warning is only placed there to let you know that the behavior of your code might have changed in case you are migrating your code from AS2 to AS3 (which the compiler has no way of knowing beforehand). You should only enable the compiler option -warn-scoping-change-in-this when you are migrating your code from AS2 to AS3.
So, as I said in the comments, you shouldn't worry about that warning, since obviously seing your code that is not your case and you don't need that compiler option enabled.

Declaring Stage in external class function and using it in other class?

what I'm trying to achieve is making a "centerItem"-method that centers your object on the stage. I would like to add this method in my Misc class file, and use it everywhere in my application. I already tried it like below but this didn't work:
public static function centerItem(item:*):void
{
item.x = (stage.stageWidth - item.width) >> 1;
item.y = (stage.stageHeight - item.height) >> 1;
}
and calling it like this:
Misc.centerItem(myObjectToPlaceOnStage);
But when I try it out, it just gives me an error. I already looked on the web but I didn't find any constructive solutions. I hope someone might find a solution/hack
If your object is already added on the stage, you can access its stage property :
public static function centerItem(item:DisplayObject):void
{
item.x = (item.stage.stageWidth - item.width) >> 1;
item.y = (item.stage.stageHeight - item.height) >> 1;
}
By the way, I've typed your item parameter to DisplayObject, so you are sure that it has width, height and stage properties.

ActionScript Basic Question

i've only ever created external .as files that extended a class such as sprite. now i just want to create one that doesn't extend anything and call it from a frame script.
package
{
public class Test
{
public function Test(val:Number, max:Number)
{
trace(val, max);
}
}
}
from my frame script of an .fla that is in the same folder as Test.as, i'll write this:
Test(50, 100);
this produces the following error:
1137: Incorrect number of arguments. Expected no more than 1.
Your code will be interpreted as cast to Test. It makes no sense to cast 2 numbers as a Test object.
What you want is an instance (an object) of the class Test.
For this, you need the new operator.
var testInstance:Test = new Test(50,100);
Then, you can use your object as needed, for example, calling methods, setting or getting values, etc.
testInstance.someMethod("hello");
testInstance.someNumber = 10;
var n:Number = testInstance.someNumber;
// etc...