ActionScript 3.0 :: Force Array's Elements To Only Be Instances Of A Specific Class?
Feb 18, 2009
I have been looking to see if it is possible to force an array's elements to only be instances of a specific class. Is it possible in AS? If not could the same result be achived with my own custom object?
I'm working with Flash Builder 4.5 on an Actionscript project. I've created the following classes:
package { public class ComplexNumber {
[Code]....
but the code inside this loop generates the following run time error: "ReferenceError: Error #1056: Cannot create property 0 on ComplexArray." Thus, my code for "BXFN_complex[ii] = ~" is incorrect. Anyone know how to achieve what I'm trying to do? Basically, ComplexDivide.v1p0 returns two numbers, and BXFN_complex is an object containing two number arrays, and I want to assign the ComplexDivide two numbers into the ii'th element of arrays in BXFN_complex.
The code works fine, but I am getting unexpected results; the array seems to be shared across multiple instances of the same class? I've coded an example to highlight what is happening:
Code: import myClass; var instance:Array = new Array(); instance[0] = new myClass;
[Code].....
keep all objects/arrays separate across multiple instances of the class.
I'm trying to create a class that contains an array. However i want to require that the items in the array are themselves of a specific class. Im told there is some sort of tag you can add to do this, but for the life of me i cannot find what it is.What i hope for is something like:
public class myClass{ public var foo:String; [ArrayType(class="BarClass")]
Is there way to get the browser to always load any swf files before everything else on a page (exept the external css and swfobject.js). Using Firebug's Net statistics I can see the swf files generally load last.
We have all our external js files inserted at the bottom of the page to help loading times.
I've been trying to move array elements from one array to another using the push method. The problem is that the element pushed should any longer be on the first array where it was taken from. That's where i'm stuck..
If I am a subclass of an Array, how do I access an element?
class ArrayOfFoo extends Array { public function getFooAt(anIndex : int) : Foo { return this[anIndex] as Foo; // <---- looks for an object attribute (named "0", for example) } }
I could wrap an array instead of subclassing it, however lacking a universal IArray interface, that is less than useful for standard and custom functions expecting an Array.
I have a sell function for my towers that when you select them and click the "Sell button" it removes the turret from the screen and stops it from firing. This all works fine.
But my problem comes when i have placed two of the same tower types on the screen at once. What happens is that it deletes everything and not just one tower like i want.
Ive thought about how i could do this and so far i have been unsuccessful in doing this.
deal with deleting a specific movie clip from the stage.
The code in question with dealing with making the Turrets is Below.
function makeTurret(xValue:int,yValue:int):void{ var turret:Turret = new Turret(); turret.x = xValue+12.5;
I have several 1000 MovieClips (representing US counties and states) on the stage after the swf is loaded. Each MovieClip has a different instance name. Currently, a user can mouse over a county in NY, for example, and I only want the boundary for the the county highlighted. However, the state boundary is also highlighted as well.
I added this type of listener at the top: addEventListener(MouseEvent.MOUSE_OVER, someFunction);
The problem is, it is applied to all MC instances on stage. Any suggestions on how to add the event listener to only specific MC instances? I thought I could add the event listener to the layer, but does not seemed to be working. And since I have several 1000 I don't think typing out mc_instance.addEventListener for every different mc is the correct way.
add multiple instances of an object to the stage at different but not random points.
For example - I have a box that is linked to a class file. In the class file I have an "ADDED_TO_STAGE" event listener. The function that is called places the box at a certain position on the stage say.... x=100, y= 200.
In the document class I create an instance of it like so -
_box:box = new box(); addChild(_box);
Now what I want is to create multiple instances of box on the stage every 5 seconds. But I don't want them all to spawn at (100,200) on the stage. But here's the catch, I don't want them to be random spawns either.
for ex- I want the first instance of box to spawn on (100,200), then 5 seconds later, the second instance to spawn at (200, 800), the third at (100, 500) and so on.
Would it be possible for me to create objects on the stage to serve as reference points for where i want the instances to spawn instead of writing the exact co-ordinates?
I want to know how to make class instances into listeners and broadcasters, from inside each class. Here is what I am trying specifically... part of the Proj class... (projectiles, as a matter of fact)
Code: private function listen():Void { u.register(this); onUpdate = function(){ step(); // a function I have which makes the projectile move (not shown) } }
where "u" is another class, the Updater class with this code...
Code: class Updater { // const. --- make this a broadcaster public function Updater(){
[code]....
PS: What is the syntax for using AS2 code tags instead of just "[CODE|"?
I'm slightly new to Flash CS3 + AS3 combo, so its more of a lack of practice question. My question stems from the problem described here http:[url].... . Note, its not the same problem, its a new one.The Problem ,So I have multiple library assets (which are on teh stage) and I want to assign similar functionality to all of them.
Attempt 1: So I create the n assets, create a custom class in AS3, link them to Flash CS3 and obviously enough it gives me the error that multiple assets can't be linked to the same class.Obvious enough to understand (well not completely, because from a programmers background it doesn't make sense).
Attempt 2: Same as attempt 1, but instead of linking all the assets to the same class I make the base class the same class, and let the derived class be dynamically created by Flash on compile time.So the linking works, but the problem is, in my base class I have a method which does something (say an tween) on a child of the library asset. So if the assets on the stage/library are called asset1, asset2, asset3 (same name for asset name, class name, id name), each of them has a sub-movieclip called ... say 'foo'. now if I manually wrote a class for each asset, I could do 'this.foo' to play with the sub-movieclip. However since this functionality is in the parent class, I'm unable to do it.
P.S. If you're form a programming/OOP background, its basically a abstract class problem. My Base class knows how to doSomething(), but it needs aSomething, which is defined in the Derived classes. However I found out that AS3 classes don't support virtual(C++ )/abstract (Java) in the true sense of the word (unless I missed some obvious documentation).
I've been hitting dead ends while programming lately all over, and I finally came to the realization all the code I have that is broken is because I don't know how to interact with instances of one class from within another. Here's the gist of what I am trying to do:
The problem is, if I address a single instance that is on the canvas, I get "access to undefined property targetClip", even though the document class can interact with it fine. If I target the class file of targetClip, I get "Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject". It isn't just a problem with hitTest either, I'm having this problem with other actions, so there is some basic syntax I'm failing to comprehend.
How do you empty an array. I want the array complete empty, so the array length =0. I have tried this code, but that doesn�t take away all elements in an array, just a few.
for (i=0; i<myArray.length; i++) { myArray.pop(); }
Im trying to use an AS3 modification of gskinner's collision detection class, but this being the first custom class i've ever tried to implement, i dont what i'm doing.How do i implement it into my flash? Do I just put it in the same folder as my .fla and .as?Do i have to initiate it in the main class?From what i understand it creates a rectangle, what would i need to turn it into a simple true/false?
In a fla file (flash8) I added an "Input-text" naming the var: "varInput", and selected "Numeral [0..9]" from "Character Embedding".I added another input object, this time of "Dynamic -text" text type naming the var:"varOutput". I also added a button naming its' instance name:"btnDisplay".In "actions" panel I wrote the following [code]....
I´m trying to order elements of an array. When the dates come from dynamics text it´s no problem, bad when come from input text the dates are treated like strings and it´s impossible to order correctly, because 15 is considered less than 3. In both cases I can operate correctly whit this dates like numbers.How is possible to order this dates from input text, like numbers?
I have array with 5 boxes. I show this boxes on the stage + showing it random. Now I want add events for this boxes. For example if I click to box1 I must go to 'p4', if I click to any other boxes I must go to 'p3'.
I try to make the same like for buttons, but its not working: classNames[0].addEventListener(MouseEvent.CLICK, goCorrect); function goCorrect(event:MouseEvent):void { trace("p4"); gotoAndStop('p4'); [Code] .....
is there an easy way, using 1 or more for loops, to hit test every object in an array against every other object in that array. So I've got an array of 6 things
If i have array like var my_Arr:Array = new Array("a", "b", "c");and i have button to use random element of ths array , is there any to remove that element that if i press the button again i won't find the last element ?