display lines in AS3 - actionscript-3

I am baffled by this function, which is called prior to this with parameters 22 and 58 for xVal ad yVal respectively. It doesn't display anything when the swf is compiled and tested, and it's error free. The code is in the document class:
private function mLine(xVal : int, yVal : int) {
var rCol = 0x0000FF;
var incr = Math.round((Math.random() * 20) + 8);
lns.push(new Shape());
var i = lns.length - 1;
this.addChild(lns[i]);
lns[i].graphics.moveTo(xVal, yVal);
lns[i].graphics.lineStyle(10, rCol);
lns[i].graphics.lineTo(xVal, yVal + 20);
lns[i].name = incr;
trace("lns[" + i + "] x is " + lns[i].x); // outputs 'lns[0] x is 0'
trace("xVal is " + xVal); // outputs 'xVal is 22'
trace("yVal is " + yVal); //outputs 'yVal is 58'
trace(stage.contains(lns[i])); // outputs 'true'
}

Assuming you have declared private var lns = []; somewhere, it draws a blue line (20px straight down from the given position).
It doesn't display anything
That means you probably don't have an object of that class on the stage. In your document class, you should use addChild to display an instance of the class containing mLine. mLine needs to be called somehow obviously. You could do this in the class' constructor, but you'd need to remove the last trace statement to avoid a null pointer error, because stage would be null then.
Edit: Missed that you said it is in the Document class. So, try and see if drawing anything else works. The problem doesn't seem to be with this function.

Your code seems like it should work. I have rewrote it to conform better to ActionScript 3 best practices
private function drawLine(xVal:int, yVal:int):void
{
var lineColor:uint = 0x0000FF;
var lineShape:Shape = new Shape();
//lineShape.name = String(Math.round((Math.random() * 20) + 8));
lineShape.graphics.lineStyle(10, lineColor);
lineShape.graphics.moveTo(xVal, yVal);
lineShape.graphics.lineTo(xVal, yVal + 20);
addChild(lineShape);
lines.push(lineShape);
}
The x and y properties of your shape will both be zero because you never set them. you are just drawing lines inside the shape at the xVal and yVal. You could do the same thing like this:
private function mLine(xVal:int, yVal:int)
{
var lineColor:uint = 0x0000FF;
var lineShape:Shape = new Shape();
//lineShape.name = String(Math.round((Math.random() * 20) + 8));
lineShape.graphics.lineStyle(10, lineColor);
lineShape.graphics.moveTo(0, 0);
lineShape.graphics.lineTo(0, 20);
lineShape.x = xVal;
lineShape.y = yVal;
addChild(lineShape);
lines.push(lineShape);
}
Not sure why its not showing up at all for you though.

Related

How can I translate keyword prototype in AS3 to Haxe?

I have the below AS3 code, and I want to translate it to Haxe. But I don't know how to deal with the keyword prototype. Who can help me? Thanks.
var style = new CSSStyleDeclaration();
style.defaultFactory = function():void
{
this.disabledOverlayAlpha = 0;
this.borderStyle = "controlBar";
this.paddingTop = 10;
this.verticalAlign = "middle";
this.paddingLeft = 10;
this.paddingBottom = 10;
this.paddingRight = 10;
};
if(chain == null) chain = {};
style.defaultFactory.prototype = chain;
chain = new style.defaultFactory();
style.defaultFactory = function():void
{
this.fontWeight = "bold";
};
style.defaultFactory.prototype = chain;
chain = new style.defaultFactory();
style.defaultFactory = function():void
{
this.backgroundSize = "100%";
this.paddingTop = 24;
this.backgroundColor = 8821927;
this.backgroundImage = ApplicationBackground;
this.horizontalAlign = "center";
this.backgroundGradientAlphas = [1,1];
this.paddingLeft = 24;
this.paddingBottom = 24;
this.paddingRight = 24;
};
style.defaultFactory.prototype = chain;
chain = new style.defaultFactory();
Ok, I poked this a bit, and now I kind of figured out, what that piece of code does. This knowledge won't help you to port your code to HAXE, but it will help you understand what it is about and to compose a decent HAXE-style alternative.
First, the part about instantiating, functions and working with prototypes. As it turned out, if you invoke the new operator on an unbound function (does not work on class methods):
The new empty class-less generic Object is created.
Its reference is passed to the said function as this.
The function can add and modify the object's fields and methods.
Ultimately, the reference to that Object is returned.
Then, it works (as I mentioned in my comments above) very much the way classes worked back then in AS1 and Flash 6.
If that function has a prototype and it is too a generic Object, then it is added to the newly created one as a... how to put it... a bottom layer Object which adds its fields to the top layer Object.
I understand that it sounds difficult, so there's an explanatory example that somehow sheds some light on it all:
public class Proton extends Sprite
{
public function Proton()
{
super();
var P:Function;
// Empty.
P = new Function;
create("First:", P);
// Empty with prototype.
P.prototype = {c:3, d:4};
create("Second:", P);
// Non-empty.
P = function():void
{
this.a = 1;
this.b = 2;
};
create("Third:", P);
// Non-empty with prototype.
P.prototype = {a:5, f:6};
create("Fourth:", P);
}
// Instantiates the F and outputs the result.
private function create(prefix:String, F:Function):void
{
var A:Object = new F;
trace(prefix + "\nJSON:" + JSON.stringify(A) + "\nREAL:" + explore(A) + "\n");
}
// Same as JSON.stringify, but also looks into the prototype.
private function explore(O:Object):String
{
var result:Array = new Array;
for (var akey:String in O)
{
result.push('"' + akey + '":' + O[akey]);
}
return "{" + result.join(",") + "}";
}
}
So, the output is:
First:
JSON:{}
REAL:{}
Second:
JSON:{}
REAL:{"d":4,"c":3}
Third:
JSON:{"b":2,"a":1}
REAL:{"b":2,"a":1}
Fourth:
JSON:{"b":2,"a":1}
REAL:{"b":2,"a":1,"f":6,"a":1}
As you can see, JSON.stringify exports only the top layer object, while direct for iteration goes through all the layers, top to bottom, and even processes the duplicate keys (but the top layer value shadows what's below).
Second, how it all is related to your code. These factory and defaultFactory functions are used in some CSS-related class to form an Object representation of the style: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/styles/CSSStyleDeclaration.html
So, you can use that prototype hack to form a generic Object with a chain of layers upon layers of CSS declarations... probably. You saw that JSON doesn't perceive anything but the top layer, I have no idea if CSS classes act differently or not.
I think, working with CSS should be less hack-y and more straightforward.
Good luck figuring it out.

How to addChild with custom name in as3?

in as2 i had this code:
symback.attachMovie("" + syms, "s_" + s);
and later i can change it's position like this:
symback["s_" + s]._y = Ypos;
i tried this but recieve error (A term is undefined and has no properties):
symback["s_" + s].addChild(new syms());
while this works:
symback.addChild(new ImeSimbola());
but i can't access it's position later...any suggestions?
You can access children of a certain container if you know their names.
var aSym:DisplayObject;
aSym = new ImeSimbola;
aSym.name = "s_" + s;
symback.addChild(aSym);
Then.
aSym = symback.getChildByName("s_" + s);
aSym.y = yPos;
You can save it to a variable before adding it to the stage:
var newChild = new ImeSimbola();
symback.addChild(newChild);
newChild._y = Ypos;

Ye olde Pipe Flow Animation

i try to script a flow-animation ( basic color, nothing fancy ) through a grid of "pipes".
( think of a 5*5 tiled screen)
since the pipes are created completely dynamic at runtime, the animation has to be scripted also.
At the moment it escapes my mind on how to do this in actionscript, without pre-generated masks.
thanks for all hints!
You wish to visually 'simulate' the flow of some liquid (for instance water) through pipes in the same style that it is done in pipe games?
http://www.mclelun.com/img/blog/120411_pipe_02.jpg
Alright...
Are you willing to use bitmapData (pixel) to create this effect?
Here is how I would go about it..
create a short script to fill a rectangle (block) of pixels gradually..
ie
var animateOn : Boolean = true;
var startPoint : Point = new Point(beginX , beginY);
var endPoint : Point = new Point(finishX, finishY);
var step : Number = 1 / Point.Distance(startPoint, endPoint);
var currentPos : Number = 0;
onEnterFrame(e : Event):void
{
var p : point = Point.interpolate(startPoint, endPoint, currentPos);
bitmap.drawRect(p.x - 2, p.y - 2, 4, 4, someColor);
currentPos += step;
}
This is just an example of the top of my head (will not compile)
The idea is to keep feeding in the right startPoint and endPoint for each tile..
You could easily animate it like with without using any mask.
You can define each type of tile as a vector of points and then iterate from one point to another by adding the position of the tile to each point.
To fill a curve dynamically you may want to use this closed formula for a Bezier curve:
//start point
var s = new Point(x0, y0);
//cont point
var c = new Point(x1, y1);
//end point
var e = new Point(x2, y2);
var step : Number = 1 / (Point.distance(startPoint, controlPoint) + Point.distance(controlPoint, endPoint));
var t : Number = 0.0;
private function onEnterFrame(e : Event):void
{
var p : Point = new Point();
p.x = (s.x * (1-t) + c.x * t) * (1 - t) + (c.x * (1-t) + e.x * t) * t;
//do the same for y axis
drawSomething(p.x, p.y);
t+= step;
}
this will animate a curve style flow

Reference Shared Object AS3

I am trying to reference a shared object (one that saves data), but whenever I try to, I get a crash.
This code works fine:
var var1:Object = { value:1 };
var varRef:Object = var1;
if(var1.value == 1) {
varRef.value = 50;
}
trace(varRef.value); // outputs 50;
trace(var1.value); // outputs 50;
But when I try to use shared objects, it doesn't work.
import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data.testing;
varRef = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef);
If you're able to figure out the issue, please post a fixed version.
Thanks.
You must come from a programming background where pointers can be dereferenced.
In this case, varRef is not varRef = &iapso. Setting its value does not change the value of iapso.data.testing;
Initially, you set varRef as a pointer to iapso.data.testing:
var varRef:Object = iapso.data.testing;
Then, you immediately change it to a constant value object literal 90:
varRef = 90;
This does not set the value of testing to 90 - this changes the value of varRef.
You could set varRef to iapso.data, then set testing as in:
var varRef:Object = iapso.data;
varRef.testing = 90;
Then, the following would produced expected results:
trace(iapso.data.testing); // 90
trace(varRef.testing); // 90
setting varRef will not update the value in iapso.data - numbers are copied, not referenced.
var bar:Object = { data:1 };
var foo:Object = bar.data;
trace(foo); //"1"
bar.data = 2;
trace(foo); //"1"
If you really want to use a reference, use data instead, eg:
import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data;
varRef.testing = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef.testing);

AS3 Can't access MC on Stage

I have 54 MC's on stage, generated dynamically. Now I want to get their x and y positions when rolling over but I am having problems getting the path correct e.g.
function copyFlightCellData():void {
var r; var s;
var cellData:Array = new Array ();
for (r = 0; r < 54; r++){
//var copyCellData = new MovieClip();
cellData[r] = Object(root).mc85.name; //["mc"+r+r];
trace("$$$$$$$$$$$$$$$$$$$$$" + cellData[r]);
}
}
I used the list objects in debug and they are listed in _level0 e.g.
Movie Clip: Frame=1 Target="_level0.mc85"
Not sure why I can't access their properties.
This is the code that created the MC's
// Create copies of flightCell for board grid
var my_mc = new flightCell();
my_mc.name = "mc" + i + j;
trace("^^^^^^^^^^^^^^****************" + my_mc.name);
addChild(my_mc);
Answer is pretty simple, use the DisplayObjectContainer object's, in this case root, getChildByName() method, for example:
var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
addChild(sprite1);
trace((root as DisplayObjectContainer).getChildByName("sprite1").name); // output : sprite1
It's probably a better idea to store the movieclips you have on your stage in an array to begin with.
To access it by name you have to assign a name to them when you create them.
mc85.name = "mc85";
As an alternative that I recommend, you can use getChildAt(index) : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#getChildAt()
Also, I highly recomend you to create an empty movieclip or sprite and add all of this mcs to them instead of the root.