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;
Related
I'm trying to create a highscore list.
I have a movieclip ("highscore") and in it i have dynamic textfields with instance names. Now i'm trying to get those textfields so that i can change the text inside. Since i'm getting them in a loop i get them with getChildByName (and this works). But what i get back is a displayObject and therefore i get the error: implicit coercion of a value of type class to an unrelated type...
I know what the error is.. but i have no idea how to fix it.
Here is my code:
private function updateSingleList(result:XML):void
{
if(result['header']['success'] != 'true'){
return;
}
for(var i:uint = 0; i < result['items']['item'].length(); i++)
{
var pos:uint = i+1;
var name:String = 'name_' + pos;
var score:String = 'score_' + pos;
var rowName:TextField = highscore.getChildByName(name);
var rowScore:TextField = highscore.getChildByName(score);
rowName.text = result['items']['item'][i]['name'].toString();
rowScore.text = result['items']['item'][i]['score'].toString();
}
}
thanks!
Specify that the child you want is a TextField :
var rowName:TextField = highscore.getChildByName('name_' + pos) as TextField;
rowName.text = result['items']['item'][i]['name'].toString();
I have 15 sounds called "Sound1", "Sound2", etc set in linkage properties. I want to dynamically refer to them in a loop. For example, instead of
currentMusic = new Sound2();
How can I do something like
currentMusic = new ("Sound" + i)();
Or what would be a better way of doing this?
Store the generated class name to a variable, retrieve the class from the application domain and instantiate the new object. Beware with exceptions!
var soundClassName:String = "Sound" + i;
var soundClass:Class;
var sound:Sound;
try {
soundClass = getDefinitionByName(soundClassName) as Class;
sound = new soundClass();
} catch (re:ReferenceError) {
trace("Class '" + soundClassName + "' not found");
} catch (te:TypeError) {
trace("Unable to instantiate the sound object");
}
Use the same procedure as you would to load a movie clip or font at runtime.
Assuming you have sounds in your library exported as "sound_0", "sound_1", ..., "sound_9", etc:
for(var i:uint = 0; i < 10; i++)
{
var soundClass:Class = getDefinitionByName("sound_" + i.toString()) as Class;
var sound:Sound = new soundClass();
}
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);
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.
I am creating grid based map renderer in AS3 which loads required chunks of PNG images and render them in a container. I've applied scrolling/dragging logic in this app using MOUSE_MOVE event handler. What I do is,
I register bDragging flag in MOUSE_DOWN and position of mouse,
In every MOUSE_MOVE, I check displacement of mouse and move main map accordingly
Unregister bDragging flag in MOUSE_UP and set position to NaN.
My problem is dragging is quite jerky/shaky.
Any suggestion would be appreciated. following is sample code I'm using.
function onMouseDown(e:MouseEvent):void
{
m_bDragging = true;
m_ptPrevPoint = new Point(e.stageX, e.stageY);
}
function onMouseUp(e:MouseEvent):void
{
m_bDragging = false;
m_ptPrevPoint = null;
}
function onMouseMove(e:MouseEvent):void
{
if(!m_bDragging || null == m_ptPrevPoint)
return;
var nDiffX:Number = int(e.stageX - m_ptPrevPoint.x);
var nDiffY:Number = int(e.stageY - m_ptPrevPoint.y);
//Make movement smoother
//nDiffX = nDiffX * 4) / 4;
//nDiffY = nDiffY * 4) / 4;
if(nDiffX != 0 || nDiffY != 0)
{
trace("X : " + nDiffX + ", Y : " + nDiffY + ", points-Old " + m_ptPrevPoint + ", New " + new Point(e.stageX, e.stageY) );
m_oCircle.x += nDiffX;
m_oCircle.y += nDiffY;
m_ptPrevPoint = new Point(e.stageX, e.stageY);
e.updateAfterEvent();
}
else
trace("not moved - X : " + nDiffX + ", Y : " + nDiffY+ ", points-Old " + m_ptPrevPoint + ", New " + new Point(e.stageX, e.stageY)) }
Please check FLA file here...made in Flash CS3. Please note if mouse is on circle it will jerk like hell but if you drag it from outside of circle, It will go smoother!
Sample FLA
Thanks guys, I figured out the problem!
actually this might be some bug of adobe, I was strangely getting some odd coordinates and that was the reason movement was not smooth. I converted stageX and stageY using localToGlobal and everything became smoother!!!
now, I dont know why should I be needed to convert stageX/stageY to global. :) my brief changes are as below.
var curPt:Point = DisplayObject(e.currentTarget).localToGlobal(new Point(e.stageX, e.stageY));
var nDiffX:Number = int(curPt.x - m_ptPrevPoint.x);
var nDiffY:Number = int(curPt.y - m_ptPrevPoint.y);
Thanks guys, every help appreciated.
Does my answer in this one help?
Problems replicating drag-and-drop with mouse events
Basically, when you move the mouse, the coordinates of the mouse can sometimes be from a different context that expected, depending on the target of the event. You need to translate the coordinates.