Casting To Integer?
Hi,
I am new to actionscript . How do I cut off the decimal part of a Number to get only an integer.
I want to display % complete of an upload, and I want for example 75.43% to read 75%
So there is no integer type in ActionScript 2? only Number? Is there some way to cast the darn thing ? Or how do i do it?
-thanks
ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0
Posted on: 07-12-2006, 10:49 PM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Casting To Integer - What's Going On?
Look at this piece of code:
---------------------
alint = "0060";
trace (int(alint));
---------------------
this gives the value 48.
-48!?!? Where does that come from?
Help appreciated!
AS3: Casting
What is the difference between the following two statements?
ActionScript Code:
oBj1 = XML(oBj2);
and
ActionScript Code:
oBj1 = oBj2 as XML;
TIA, Eugene
Casting
Hi. Why does the attached lines return true, all three of them?
I understand the first returning true, I could even understand the second (strictly from the documentation it's ok to return true, but I think the "as" word is misleading), but I definitely don't understand why the third one is true.
Could some one explain me the exact semantics of those thing?
Thanks,
FaQ
Attach Code
var a:Sprite = new Sprite();
trace(a is Sprite, " = a is Sprite");
trace((a as DisplayObject) is Sprite, " = (a as DisplayObject) is Sprite");
trace(DisplayObject(a) is Sprite, " = DisplayObject(a) is Sprite");
Type Casting
I have an input box where I want to put a number value that begins with zero. "016" I then put this value into an array and it drops the zero. I want the number value to be treated like a string. I have tried using the myText.toString() function, and the String(myText) constructor, but it still takes off the zero at the begining. does anyone have any ideas, or has anyone experienced this before?
Web Casting And Forums?
Hi folks, I need a pointer in the right direction here!
I have to setup a live webcam and a stills webcam on 10min refresh, aslo I need to make a forum page all in flash mx2004, can anyone point me in the direction of some good tutorials that I can just dive into?
Any help greatly appriciated.
Cheers.
Casting An Array To An Int
I have this trace statement that reads out a array of an sql database-
trace(re.result._items[row][name]);
It prints this
2
8 *
3
1
5 *
2
0
5 *
1
I am trying to pull out the numbers with a * and make them seperate int's. Whats the best way to do this? Thanks in advance!
Type Casting
Is there any way to convert String to Number? Flash doesn't convert it automaticly in my code. I used .valueOf but I didn't work.
Instanceof And Casting; Anyone?
I'm setting up a nice event-handling class for taking care of the WebServiceConnector. I'm trying to do a clean cast of the result object, so I can get it back into type safety before handing it off to whatever class asked for it.
This (and variations thereof) is what I've been trying for a while now:
Code:
private function resultListener(res:Object)
{
if(res.target.results instanceof Boolean)
{
var return:Boolean = Boolean(res.target.results);
}
else if(res.target.results instanceof String)
{
var return:String = String(res.target.results);
}
else if(res.target.results instanceof Number)
{
var return:Number = Number(res.target.results);
}
else if(res.target.results instanceof Array)
{
var return:Array = Array(res.target.results);
}
else if(res.target.results instanceof Object) //(should) always true
{
var return:Object = Object(res.target.results);
}
}
None of these if-clauses are ever true. I would suspect - at the very least - that "instanceof Object" would be a catch-all, but it isn't. So... what type is results?
Do you have any idea as to how I can create this function - casting the result to a known AS-type?
Casting To Boolean From XML
How would you get Flash to recognise a value passed via XML as a boolean true/false...
Ive tried casting it to force it to be Boolean, which I think ive done correctly as:
ActionScript Code:
var run:Boolean = Boolean(xmlData.run.text())
Ive also tried removing the .text() however it always goes true, anyone know what i'm doing wrong...?
XML Node And Casting To Int
Just came across a strange behaviour in my Flex app when casting a xml node value to int.
Code:
//The attibute is set to '2'
var selectedCodeId:int = int(selectedFilterTreeNode.@codeId); //gave 2
var selectedCodeId:int = selectedFilterTreeNode.@codeId as int; //gave 0
Anyone seen this before?
SharedObject Casting
I'm storing a custom class object in a local SharedObject file, and then later trying to retrieve it. When I try to retrieve it, I get the following error:"Type Coercion failed: cannot convert Object@10547d59 to AS3.accounting.UserAccount."
The "@10547d59" changes every time obviously since it is just a temporary type name.
The 'basic' code to save the object is this:
ActionScript Code:
public function createUser(name:String, password:String):Boolean{
var users:Object = userAccounts.data.users;
if(!users) users = new Object();
if(users[name]) return false;
user = new UserAccount(name, password);
users[name] = user;
userAccounts.setProperty("users", users);
userAccounts.flush();
return true;
}
The 'basic' code to load the object is this:
ActionScript Code:
public function findUser(name:String, password:String):Boolean{
var users = userAccounts.data.users;
if(!users || !users[name]) return false;
user = users[name];
return true;
}
The object being sent-to the file is of type UserAccount, and the data retrieved is attempting to be placed in a UserAccount object, but the retrieved data doesn't seem to remember what type it was, and since Object is higher in the inheritance hierarchy it won't naturally convert it for me.
If you have any ideas, please feel free to share them. Otherwise, when I figure out the best solution I'll post it below.
FLVMovie Casting
Hi there...
I'm new to the hole AS3 stuff... so sorry if the question is kind of silly.
For the Question:
I created my own FLVMovie Class:
Code:
package {
import flash.events.MouseEvent;
import fl.video.FLVPlayback;
public class FLVMovie extends FLVPlayback{
public function FLVMovie(){
this.addEventListener(MouseEvent.CLICK,clickHandler);
}
public function clickHandler(btn){
trace('movie click');
this.stop();
}
}
}
No for the question itself.... I have a existing FLVPlayback on the Stage... is there a way to convert / cast it into a FLVMovie so it has the Click event handler?
thx...
Casting MC To FLVPlayback
I need to create a FLVPlayback component dynamically, but it seems I can't cast the MovieClip returned by attachMovie to FLVPlayback class, it always return null. If I create a new document, it works well. I think this problem happen when I tell Flash to export classes at frame 2, but my component is being attached at frame 3. Is there a way to export classes for frame 2 and still being able to cast the Component?
Casting Error In CS 3
Hi guys,
I maintain an API for which have a huge chunk of its code loaded dynamically from the server. This is all fine and dandy until I try to parse costume class instances back and forward between the current file and the loaded remote API file.
Basically I get a cast error as below:
TypeError: Error #1034: Type Coercion failed: cannot convert Nonoba.api.net::Connection@408e47e1 to Nonoba.api.net.Connection.
My initial ida for a solution was to simply just accept :* and do
var conn:Connection = parsedObject as Connection
but this results in a null value
Any ideas?
Casting Woes
Imagine a quick scenario;
_thumbs is a Sprite, inside it is a custom class called "thumb1" (extends MovieClip) and inside that is a sprite called "nail". Nail is a libraryitem. Nail has a child called blocker (that's in the library item already).
Now, this works fine;
ActionScript Code:
var t = _thumbs.getChildByName("thumb"+i);var sx = t.getChildByName("nail");TweenMax.to(sx.blocker,1,{yada});
Wheras this works fine, I can't for my life figure out how to cast these variables. If I try making them DisplayObjects or Sprites or MovieClips or whatnot it all throws error with either implicit coercion or reference to a undefined method getChildByName
Grateful for any insight.
Casting Vs. Converting
hey, thought it might be useful to some to see these tests i ran:
ActionScript Code:
private function testTypeConversions () :void {
var i:int = 10;
trace("int as String:"+(i as String)); //null
trace("String(int):"+String(i)); //"10"
trace("int.toString():"+i.toString()); //"10"
var num:Number = 0.5;
trace("Number as String:"+(num as String)); //null
trace("String(Number):"+String(num)); //"0.5"
trace("Number.toString():"+num.toString()); //"0.5
var str:String = "2.3";
trace("String as int:"+(str as int)); //null
trace("int(String):"+int(str)); //2
trace("parseInt(String):"+parseInt(str)); //2
trace("String as Number:"+(str as Number)); //null
trace("Number(String):"+Number(str)); //2.3
}
the moral of the story here is that AS2-style casting:
ActionScript Code:
Type(value)
can actually convert primitives to a different datatype, while AS3-style casting:
ActionScript Code:
value as Type
will not convert (and will return null if the cast is invalid).
while it may be tempting, then, to just stick with AS2-style casting, i'm pretty sure it's preferable to use AS3-style casting and conversion functions like toString, parseInt, and parseFloat. that's more in keeping with other OO languages.
also, as a side note, the AS3 language reference actually refers to AS2 casting functions as conversion functions. that's what they do...don't (do what i did and) confuse them with casting via 'as'!
Casting A String To An Object
I'm having trouble passing an object parameter using a string concatenation in a for loop...
in the for loop, I duplicate movieClips, copying a movie called "glass" and try to register it's speeds with the register() function below.
for (i = 1; i <= numObjects; i++) {
duplicateMovieClip(glass,"glass" + i, i);
register("glass" + i,20,20,20);
}
function register(object, xspeed, yspeed, rspeed) {
objects.push(object);
object._xspeed = xspeed * (2 * Math.random() - 1);
object._yspeed = yspeed * (2 * Math.random() - 1);
object._rspeed = rspeed * (2 * Math.random() - 1);
}
unfortunately, when passing tmp as "glass" + i, register thinks its a string, not an object. The object appear on stage, but doesn't move. If I hard code register() passing in as register(glass2,20,20,20) it does move.
How do I cast the string I pass in as an object?
Casting (like In Java?) Or Help In This Thing
hi guys,
how do you cast, like you do in Java?
string = (String)(skaljeasjkle)
..
or, you can just help me to do this
you see, i have a class Q. i named my instances of these q0, q1, q2 to qn.
in this class Q, i have vars question, a0, a1, a2, a3.
i want flash to read the Q (q0, q1, q2, qn) depending on the counter at a moment in time. for example...
var temp = (Q)(q + "" + counter);
//so i can access the qn's vars..
trace(qcounter.a1);
i THINK you can do something like that in Java but even there i'm already rusty.
if i don't use this method, i'll have a HUGE if statement.. or switch. and of course, i wouldnt want to do that.
Type Casting Problem
Hello gurus,
I have a problem:
while loading a number from a variable in a text file, into a script, i found that the number is not really a number but a string or a character. But i need it as a number to perform some operation. Can you please help me on how to convert that??
Type Casting To A Class Name
I'm trying to type cast an item in an array (inventoryList) to a Class, so I can addChild the movieClip to the stage. The items in the inventory 'carrotIcon and passportIcon' - I also have movieClips of the same name in the library with their own classes.
It works when I just change the line to 'addIconName = new carrotIcon()' for example. Maybe the answer is that I should use something else than 'Class' as the type cast. I've tried MovieClip to no avail.
var itemName:*;
var inventoryList:Array = new Array();
inventoryList.push("carrotIcon");
inventoryList.push("passportIcon");
for (var i:int = 0; i < (inventoryList.length); i++)
{
itemName = Class(inventoryList[i]); //this is the line I'm having problems.
addIconName = new itemName();
addChild(addIconName);
}
Thanks for looking!
Joe
Type Casting Problems
Quick Rundown of What I am doing so you can understand the logic is:
Building multipage Form In first Frame I hove no movies just initialized a bunch of Variables to store Data filled in from Forms on various Pages (frames 2 thru 7)
Page/Frame 2 is First Page which asks for Basic Details like Name Phone #s etc
I have a function that checks (when next page button is clicked) that checks Page form inputs are all filled correctly and if so updates the Variables (from frame 1) with the entered Data. Then on Return to the Page I prefill fields from the stored Variables.
Now the Problem:
For 4 different Phone numbers on the Page I have 3 inputs each (AreaCode, Prefix, Suffix)
When I go to store the entered values to 1 Variable each (Frame one/ Variable of type int/ to store the 10 digit number before sending to php serverside script) I concat the 3 input fields for each number and cast to int then on reenter page to preload the values again I cast the 10 digit int back to string and do a substr to divide them up and put them in the inputs as text.
However weird things happen Company Phone and Personal phone seem to usually work fine but Cell Phone and Fax get all muffed up the first time I renter (after leaving and returning they stay the same as first muffed up appearance)
Here is the portions relating to the Events please advise if you have any ideas how I can stop this behaviour. Thanks
Setting the Variable before leaving page
PHP Code:
cphone = int(CompanyPhoneAC.text + CompanyPhonePF.text + CompanyPhoneSF.text);
cfax= int(CompanyFaxAC.text + CompanyFaxPF.text + CompanyFaxSF.text);
pphone = int(PersPhoneAC.text + PersPhonePF.text + PersPhoneSF.text);
pcell = int(PersCellAC.text + PersCellPF.text + PersCellSF.text);
Setting the Inputs when Returning to Page
PHP Code:
var PersCAC:String = String(pcell);
PersCAC = PersCAC.substr(0,3);
if(PersCAC.length<3){
PersCAC = "" ;}
PersCellAC.text = PersCAC;
var PersCPF:String = String(pcell);
PersCPF = PersCPF.substr(3,3);
if(PersCPF.length<3){
PersCPF = "";}
PersCellPF.text = PersCPF;
var PersCSF:String = String(pcell);
PersCSF = PersCSF.substr(6,4);
if(PersCSF.length<4){
PersCSF = "";}
PersCellSF.text = PersCSF;
var PersPAC:String = String(pphone);
PersPAC = PersPAC.substr(0,3);
if(PersPAC.length<3){
PersPAC = "";}
PersPhoneAC.text = PersPAC;
var PersPPF:String = String(pphone);
PersPPF = PersPPF.substr(3,3);
if(PersPPF.length<3){
PersPPF = "";}
PersPhonePF.text = PersPPF;
var PersPSF:String = String(pphone);
PersPSF = PersPSF.substr(6,4);
if(PersPSF.length<4){
PersPSF = "";}
PersPhoneSF.text = PersPSF;
var CompanyPAC:String = String(cphone);
CompanyPAC = CompanyPAC.substr(0,3);
if(CompanyPAC.length<3){
CompanyPAC = "";}
CompanyPhoneAC.text = CompanyPAC;
var CompanyPPF:String = String(cphone);
CompanyPPF = CompanyPPF.substr(3,3);
if(CompanyPPF.length<3){
CompanyPPF = "";}
CompanyPhonePF.text = CompanyPPF;
var CompanyPSF:String = String(cphone);
CompanyPSF = CompanyPSF.substr(6,4);
if(CompanyPSF.length<4){
CompanyPSF = "";}
CompanyPhoneSF.text = CompanyPSF;
var CompanyFAC:String = String(cfax);
CompanyFAC = CompanyFAC.substr(0,3);
if(CompanyFAC.length<3){
CompanyFAC = "";}
CompanyFaxAC.text = CompanyFAC;
var CompanyFPF:String = String(cfax);
CompanyFPF = CompanyFPF.substr(3,3);
if(CompanyFPF.length<3){
CompanyFPF = "";}
CompanyFaxPF.text = CompanyFPF;
var CompanyFSF:String = String(cfax);
CompanyFSF = CompanyFSF.substr(6,4);
if(CompanyFSF.length<4){
CompanyFSF = "";}
CompanyFaxSF.text = CompanyFSF;
Casting A String Into A Function
Hi,
I would like to convert a String to a function:
Code:
public function Init(command:String)
{
//i need to call the handleHowTo() function from here base on the command String
}
public function handleHowTo()
{
trace("yes");
}
Init("handleHowTo");
How can I convert the command:String to a function to call the function handleHowTo() ? is there a way to cast a String into a Function ???
Thanks ;)
Dynamic Type Casting - Is It Possible?
Is there a way to do something like the following...?
Code:
var myClass:String = "blah";
var inst = new[myClass]();
I've invented the above syntax--though I'd be delighted if it turned out to work! For clarity, I would hope for something like the above code to be synonymous with the following:
Code:
var inst:blah = new blah();
...So is it possible to dynamically specify a class name when creating a new instance?
Help Casting A String To A Number
I've got a String value I'm trying to pass into a beginFill() function, but it's not happy with a String and gives me this error: "1067: Implicit coercion of a value of type String to an unrelated type uint. I've tried casting using "hexString as Number" but that turns my pretty "0xFFFFFF" into something like 106425. Any suggestions on how to give beginFill a value that's originally a String?
ActionScript Code:
private function createBG(hexString:String):void
{
// this doesn't work - it turns my "0xFFFFFF" format into a numeral
//var bgColor:Number = hexString as Number;
var bg:Shape = new Shape();
// pre-defined colored bg, starting at point 0, 0
bg.graphics.beginFill(bgColor);
bg.graphics.moveTo(0, 0);
bg.graphics.lineTo(stage.width, 0);
bg.graphics.lineTo(stage.width, stage.height);
bg.graphics.lineTo(0, stage.height);
bg.graphics.lineTo(0, 0);
addChild(bg);
}
Type Casting Classes
Alright let's say I have a MovieClip in the library with the linkage set to TestClass and I have this code...
var string1:String = "Test";
var string2:String = "Class";
var testClass:* = new Class([string1 + string2])();
this.addChild(testClass);
so testClass should be an instance of TestClass, this works for me sometimes and sometimes it doesn't.
Does any one have any advice as a better way to do this, and what this tecnique is called ( root["string" + "1"])?
Thanks for all you guy's help!
Casting Loader.content
Hi,
I am trying to dynamically load swf-files. They extend a class TestClass. However, using Loader, flash always considers loader.content as MovieClip, leading to the methods and properties defined in TestClass not being available.
Casting does not seem to work.
Am I taking the wrong approach?
Cheers,
jt
Casting / Concating XML Nodes
Hi,
I am having trouble getting Flash to recognize an XML path if it is cast from strings originally.
Doesn't work:
var nNode:XMLList = input.item[2].project.material;
var target:XMLList = XMLList(nNode+".page");
trace(target is XMLList); // true
trace(target);// input.item[2].project.material.page
Works:
var nNode:XMLList = input.item[2].project.material.page;
var target:XMLList = XMLList(nNode);
trace(target is XMLList); // true
trace(target);// outputs the contents of the XML node (the desired result)
Is there a way to concat an XML name together without using a string or cast the entire name differently so that the node is read properly by Flash?
Thanks in advance,
Bobby
AS3 Calculating And Number Casting
Hi everyone...
Since my project's code majority is some math routine, i have to kno the exact actionscript flow of makin calculations.
My usual formulas are mixes of ints and Number.
I've made severl tests like this:
Code:
var a:int = 2;
var b:int = 3;
var c:int = a/b;
trace(c);
// 0
Code:
var a:int = 2;
var b:int = 3;
var c:Number = a/b;
trace(c);
// 0.6666667
Code:
var a:int = 2;
var b:int = 3;
var c:int = Math.ceil(a/b);
trace(c);
// 1
So once after that I've made some conclusions, that first calculation is made in floats (Numbers), after that it's casted to the type (int or Number) which is requerd by the left hand operator, or function arguments place.
Am I right? Can anyone get me some accurate scheme of how those calculating things are performed? Or link? Thank you!
Type Casting And Equality
TypeError: Error #1034: Type Coercion failed: cannot convert uk.co.andrewrea.brickwall::BrickWall@24b1191 to uk.co.andrewrea.brickwall.BrickWall.
Does anyone know why this would be. I create a variable of Type BrickWall and then assign it a variable of type BrickWall the event data of the loader i.e.
ActionScript Code:
wall = e.target.content.wall;
both wall and e.target.content.wall are of the same type of the same package, but seem to be handled as different types. Anyone know why this is?
Thanks,
Andrew
Casting Data Types From XML To AS 3.0
Hey guys,
I was just wondering if it's possible to cast standard AS 3.0 data types other than Number, int, uint and String from XML. I experimented with this:
ActionScript Code:
var xml:XML = <root_node>
<number_node>12.9876</number_node>
<int_node>321654</int_node>
<string_node>this is a string</string_node>
<boolean_node>false</boolean_node>
</root_node>;
var number_node:Number = xml.number_node;
var int_node:int = xml.int_node;
var string_node:String = xml.string_node;
var boolean_node:Boolean = xml.boolean_node;
trace(number_node); // returns 12.9876
trace(int_node); // returns 321654
trace(int_node * number_node); // returns 4177513.4904
trace(string_node); // returns this is a string
trace(boolean_node); // returns true (AS 3.0 default)
If it's only numbers and strings, would it be possible to use remoting and and something like AMFPHP, WebOrb, Zend Framework, etc. with MySQL to store other datatypes? Is there an easy solution?
How Strong Is Type Casting
I have a variable saved in the first frame of my fla file
var question:Number = 0;
I want to make it possible for sombody to jump around in the exersice so I built kind of a jumper and saved it in its own mc. In the mc I have a button called jump with the following code.
_root.question = q.text;
Now that question will work but when I tried to add one to my question variable it did something weird. Say I want to jump to question 2. When it tries to add one to my question variable I get 21. It seems that by saving q.text to my question variable, it gets recasted to a String. Has anybody else bumped into this problem.
Side note: I did fix this with parseInt. I am just curious why this happened in the first place, is type casting really not that strong?
Number Casting Problem
Hi there,
I'm trying to add two numbers. One is a variable I keep on the server, the other is one I get from a client..
Seeing as the calls from the client are all casted to strings (I think so at least). Is there any way I can cast them to a number in my server so I can add them instead of concatinating them as string...
plan is to make 5+5=10 instead of 55 ;-)
Thanks in advance
Bart
Casting String To Uint
Hello,
Does anyone know how to cast a string into a uint?
I have tried some along the lines of the following:
var myStr:String = "0x000000";
var myUint:uint;
myUint = uint(myStr);
But i keep getting an error stating that there is a mismatch in variable types or i get a value of '65280' when I trace myUint. I understand that uint is a positive integer but i need to accept a string for the colour value and need to convert it to a string for use.
Any suggestions and correct answers are welcome. I will post or indicate the working solution once this problem has been solved.
Thanks in advance to everyone,
Lee
Edited: 11/26/2008 at 03:42:18 AM by leej_w
Flex/AS3 Casting Question
I am having a hard time coming from another programming mentality here. I am trying to reference an object using a variable passed to a handler. the string as it seems (datatype) is correct, but I cant figure out how to define it as an object reference.
The problem here is, event.item.@data when outputed as a string is "networkPanel" which i want. But I need to somehow, and i dont know the command to do so... but if it existed
thisIsaObject(event.item.@data).visible = whatever;
Code:
private var menubarXML:XMLList =
<>
<menuitem label="Applications" data="top">
<menuitem label="Network Status" data="networkPanel"/>
</menuitem>
</>;
// Event handler to initialize the MenuBar control.
private function initCollections():void {
menuBarCollection = new XMLListCollection(menubarXML);
}
// Event handler for the MenuBar control's itemClick event.
private function menuHandler(event:MenuEvent):void{
//If i change event.item.@data to networkPanel - this works.
if (event.item.@data == true)
{
event.item.@data.visible = false;
}
else
{
event.item.@data.visible = true;
}
}
Draggable MC, Class & Casting
Let’s say I wish to drag a panel but only from a strip at the top. I put a MovieClip named mcBar inside a MovieClip named mcPanel.
When I code frame one in the timeline it works.
ActionScript Code:
mcPanel.mcBar.addEventListener(MouseEvent.MOUSE_DOWN, downHandle); mcPanel.mcBar.addEventListener(MouseEvent.MOUSE_UP, upHandle); function downHandle(evtObj:MouseEvent):void{ this.startDrag(); } function upHandle(evtObj:MouseEvent):void{ this.stopDrag(); }
What’s confusing is that it works. Why is the whole thing dragging? I’m assuming it’s event propagation? mcBar’s mouse down is “bubbling” to mcPanel… Even though this is exactly what I want – Don’t completely understand it.
Now, further complication is when I move the code to a class.
ActionScript Code:
package{ //imports import flash.display.MovieClip; import flash.events.MouseEvent; public class DraggableClip extends MovieClip{ //constructor public function DraggableClip(){ this.addEventListener(MouseEvent.MOUSE_DOWN, downHandler); this.addEventListener(MouseEvent.MOUSE_UP, upHandler); } //methods function downHandle(evtObj:MouseEvent):void{ parent.startDrag(); } function upHandle(evtObj:MouseEvent):void{ parent.stopDrag(); } }}
Now, if I assign mcBar via Linkage to DraggableClip.as, only the bar drags. This is the behavior I would have expected when I initially coded the timeline. However, I really do want the entire panel to drag so how do I target it?
As the target for the drag methods:
this refers to the instance of the class(mcBar only) , parent throws a 1061: Call to a possibly undefined method startDrag through a reference with static type flash.displayisplayObjectContainer. error.
I found a drag MC sample from Jen deHaan that had: var thisMC:MovieClip = event.currentTarget as MovieClip;
I barely understand that this is a Casting Operation...Is this what I'm missing?
In Summary:What do I need to do to get mcBar inside mcPanel to drag both mcBar and mcPanel from code in a class?
Also if my issue is with casting, why?
In my deHaan sample, why legacy as operator vs type(expression)?
Thanks for any/all insight.
Dynamic Type Casting - Is It Possible?
Is there a way to dynamically typecast a new instance? Maybe something like the following...?
Code:
var myClass:String = "blah";
var inst = new[myClass]();
I've invented the above syntax--though I'd be delighted if it turned out to work! For clarity, I would hope for something like the above code to be synonymous with the following:
Code:
var inst:blah = new blah();
...So is it possible to dynamically specify a class name when creating a new instance?
Casting Embedded Objects
im trying to import a symbol from my external swf and then cast it as a custom object, but failing miserably!!
heres where i started, successfully instancing the asset as a sprite
ActionScript Code:
[Embed (source = “../fla/library.swf”, symbol = “customCursor”)]
private var cursorSprite:Class;
var customCursor:Sprite = new cursorSprite();
addChild(customCursor);
but now i want to cast customCursor, so tried:
ActionScript Code:
import CustomCursor;
[Embed (source = “../fla/library.swf”, symbol = “customCursor”)]
private var cursorSprite:Class;
var customCursor:CustomCursor = new cursorSprite() as CustomCursor;
addChild(customCursor);
and various variations on the above, all of which failed. any tips would be much appreciated.
AS2 - Casting An Object To A Different Type
Hi all,
I'm trying to convert a String to Slide (mx.screens.Slide). Everytime I'm getting null.
ActionScript Code:
var slide = "slide_11"
_root.presentation.gotoSlide(slide);
I need to do this but it doesn't work. I have a string because slide_11 was in a xml file.
Any idea?
Simple (I Hope) Casting Question
I'm processing some XML, which is working fine except for the fact that some of the parameters are attributes, which in essence, makes them strings by default. The parameters, though, are typed as a uint(s) because they are numeric.
How can you cast a string value to a number (uint) value. I've tried the generic method for casting uint(stringName) and Number(stringName), but that doesn't work.
Any suggestions ? Thanks in advance.
Accessing Parent Properties / Casting
Hi
I have a problem with accessing the properties of a parent movieclip in AS3
I have 2 movieclips on my scene: clip1 and clip2
I have some actionscript inside clip2 that I would like to use to access some properties of clip1
So I tried this code:
Code:
trace((this.parent.parent as MovieClip).clip1.y);
but I get the following error:
Access of possibly undefined property some through a reference with static type flash.displayisplayObjectContainer.
It seems to me the path I use to get the properties of clip1 is correct, isn't it?
Thanks in advance for your help
Casting A Base Or Inherited Class?
Howdy Folks,
I have a number of movieclips on stage. Each of these movieclips has their base class assigned to a generic Character class which gets populated with specific data for each character via an xml file.
When the time comes to generate the next screen i would like to cast the chosen movieclips base Character class back into another Character class but am not exactly sure how that would be done.
So in the interim I have a function that creates a new Character object and walks through properties of the chosen movieclip and assigns them...
Is it possible to cast a base class of a movieclip back into another class without inheriting all the movieclip properties that I don't need?
many thanks,
frank grimes
Accessing Parent Properties / Casting
Hi
I have a problem with accessing the properties of a parent movieclip in AS3
I have 2 movieclips on my scene: clip1 and clip2
I have some actionscript inside clip2 that I would like to use to access some properties of clip1
So I tried this code:
Code:
trace((this.parent.parent as MovieClip).clip1.y);
but I get the following error:
Access of possibly undefined property some through a reference with static type flash.displayisplayObjectContainer.
It seems to me the path I use to get the properties of clip1 is correct, isn't it?
Thanks in advance for your help
Class Casting Strange Behavior
So, I have a class named Node, and several classes named ProjectNode, UserNode, etc that extends Node. I also have a class named GraphNode with a member variable of type Node (and some other stuff). Ideally, I'd like to be able to assign any subclass of Node to this variable, and retrieve the actual class (eg: UserNode) later by using a cast (because of some other variables/methods in Node, it's possible to determine what kind of Node it is using only methods of Node, and thus choose the correct class to cast).
However, when I try to do this, the cast from Node to UserNode fails, returning null.
Any thoughts on why this would be?
Help: Casting String Gotten From Xml To A Number Type
Hi all,
I have a major problem, I have parsed the data from an xml doc to global vars in flash and need to use a few of them as numbers for things the position of "nodes" on the screen. However I can not cast the data stored as a var to a number or int value.
For example in the code below when the set Node button is pressed it traces the "Node's" current X value, then the value in the global var gotten from the xml, which contains the x position of where to place the Node.
Next it creates a new var of Number Type and assigns it the Number value of the string in global var.
It then traces the new value of SNtopLeftX and sets the x pos of the Node's movie clip to the value of SNtopLeftX
However the Node doesn't move and this is the output I get from the traces
Start Node is at x = -734.5
nod_topLeftX is 16
TopLeftX = 16
SN TopLeftX = NaN
I do not understand why I am getting NaN when the string of nod_topLefX is 16, any ides?
-------------------------------------------------------------------------------------------------------------------
Attach Code
butNodeSet.onRelease = function() {
trace ("Start Node is at x = " + mcStartNode._x);
trace ("nod_topLeftX is " + nod_topLeftX);
var SNtopLeftX:Number = Number(nod_TopLeftX);
trace ("StartNode TopLeftX = " + SNtopLeftX);
mcStartNode._x = SNtopLeftX;
}
Help: Casting String Gotten From Xml To A Number Type
Hi all,
I have a major problem, I have parsed the data from an xml doc to global vars in flash and need to use a few of them as numbers for things the position of "nodes" on the screen. However I can not cast the data stored as a var to a number or int value.
For example in the code below when the set Node button is pressed it traces the "Node's" current X value, then the value in the global var gotten from the xml, which contains the x position of where to place the Node.
Next it creates a new var of Number Type and assigns it the Number value of the string in global var.
It then traces the new value of SNtopLeftX and sets the x pos of the Node's movie clip to the value of SNtopLeftX
However the Node doesn't move and this is the output I get from the traces
Start Node is at x = -734.5
nod_topLeftX is 16
TopLeftX = 16
SN TopLeftX = NaN
I do not understand why I am getting NaN when the string of nod_topLefX is 16, any ideas?
------------------------------------------------------------------------------------------------------------------- This is my Code
butNodeSet.onRelease = function() {
trace ("Start Node is at x = " + mcStartNode._x);
trace ("nod_topLeftX is " + nod_topLeftX);
var SNtopLeftX:Number = Number(nod_TopLeftX);
trace ("StartNode TopLeftX = " + SNtopLeftX);
mcStartNode._x = SNtopLeftX;
}
Problems Casting A Remote-loaded SWF
Let's say I have 3 items in the same package:Shouter interface - requires implementers to implement the shout() function
Shell class - implements Shouter...this Shell.swf is to be loaded remotely from:
ShellLoader class - loads Shell from a remote server, casts it as a Shouter and invokes its shout() function...uses allowDomain in order to cross-domain script.
Now let's say ShellLoader.swf lives on a.com and Shell.swf lives on b.com.....I've got a cross-domain policy on b.com. Using LoaderContext's ApplicationDomain and SecurityDomain, I'm able to load and cast the Shell object as a Shouter and invoke the shout() function. Everything ok here.
Now let's say ShellLoader is run locally and is localTrusted. Shell.swf still lives on b.com. I'm now unable to use SecurityDomain property of LoaderContext within ShellLoader because it is now run locally. Shell.swf now exists in a different ApplicationDomain as my ShellLoader and I get the error "Type Coercion failed" when I try to cast the loader content as a Shouter.
Is there anyway to resolve this issue? I believe it will require Shell.swf to be loaded in the same ApplicationDomain as ShellLoader but I don't know any way to do this when ShellLoader is run locally. Perhaps this is prevented in the Flash player on purpose?
Accessing Parent Properties / Casting
Hi
I have a problem with accessing the properties of a parent movieclip in AS3
I have 2 movieclips on my scene: clip1 and clip2
I have some actionscript inside clip2 that I would like to use to access some properties of clip1
So I tried this code:
Code:
trace((this.parent.parent as MovieClip).clip1.y);
but I get the following error:
Access of possibly undefined property some through a reference with static type flash.displayisplayObjectContainer.
It seems to me the path I use to get the properties of clip1 is correct, isn't it?
Thanks in advance for your help
Problem Casting XML Nodes To Numbers - NaN Error
Hell you all,
I was wondering if anyone can help. In an XML file I have numbers stored to be used as image sizes and colors for some components i've built. I can read the number and the components scale fine, but when I try to manipulate the number and also check the type I get a NaN error.
I tried using Number(xmlNode) and parseInt(xmlNode) and neither are working.
I'm totally baffled!
Help please
Code:
var w:Number = parseInt(xml.firstChild.childNodes[currentProject].childNodes[currentImage].attributes.width);
var testInt:Number = w + 20;
trace(testInt);
The trace reads "NaN" ?!?
|