As3 Vector<T> parametric TYPE - actionscript-3

I have one vector instance and I'm exporting swf with Flash Player 10/10.1.
I want initialise it with a parametric type. I tried as follow:
var someType:Class = MyCustomClass;
var v:Vector.<someType> = new Vector.<someType>();
But it doesn't work!!
There is a way to do this?
I hope question is clear :-)
Thanks in advance!

someType is the instance of the class type; whereas Vector is a container of that type.
This should be:
var v:Vector.<MyCustomClass> = new Vector.<MyCustomClass>();
Otherwise, I've noticed Haxe would compile this as:
var v:Vector.<Object> = new Vector.<Object>();
Flash polymorphism is lacking, if you had class A and class B, and attempted to push them to a vector of type Class you would receive an error:
Example
package
{
import flash.display.Sprite;
public class test extends Sprite
{
public function test()
{
var v:Vector.<Class> = new Vector.<Class>();
var a:A = new A();
var b:B = new B();
v.push(a);
v.push(b);
}
}
}
Error:
TypeError: Error #1034: Type Coercion failed: cannot convert A#43a2ff1 to Class.

Jason's right. You can't do this. I'm sorry. I ran into the same problem a while back.
Dynamically instantiate a typed Vector from function argument?
Sucks, doesn't it? :-)

Related

GetDefinition in as3 using sprite class

i have a problem. I have been able to run my game successfully using as3isolib library (IsoSprites), but when i search on the internet, the IsoSprites don't have the hitTestObject or hitTextPoint features. So, i changed the IsoSprites to the Sprite, but when i already changed the IsoSprites to the Sprite, i am getting this error:
1119: Access of possibly undefined property sprites through a
reference with static type flash.display:Sprite.
and it is pointed in:
Constant.dudeEfis.sprites = [efisFrontClass];
dudeEfis is Sprite
i am aware that the Sprite don't have the feature of:
dudeEfis.sprites
but the problem is, if i change the "dudeEfis" to the IsoSprite, i am not be able to get the hitTestObject or hitTestPoint. And if i change to the Sprite, i am getting that error. How do i solve it? The code that i am giving to you is for movement faces direction (when the player is facing south, so do the character)
Here is the code:
public static var loaderEfis:Loader;
public static var dudeEfis:Sprite; //public static var dudeEfis:IsoSprite;
public static var _numXEfis:Number = 0;
public static var _numYEfis:Number = 0;
Constant.loaderEfis = new Loader();
Constant.loaderEfis.load(new URLRequest("efis.swf"));
var efisFrontClass:Class = Constant.loaderEfis.contentLoaderInfo.applicationDomain.getDefinition("EfisFront") as Class;
if (Constant._numXEfis == 0 && Constant._numYEfis == 0)
{
Constant._numXEfis = Constant.dudeEfis.x;
Constant._numYEfis = Constant.dudeEfis.y;
Constant.dudeEfis.sprites = [efisFrontClass];
}
Any help will be appreciated! Thank you in advance!
Sounds like you already know what the problem is. One way you can solve this is to create your own class that extends the IsoSprite class then add the hitTestObject and hitTestPoint methods yourself, they're pretty easy to implement.
package {
import flash.display.DisplayObject;
import flash.geom.Point;
public class MyOwnSprite extends IsoSprite {
public function hitTestObject (object:DisplayObject):Boolean {
//some logic here
}
public function hitTestPoint (point:Point):Boolean {
//some logic here
}
}
}
This way you get to keep all the functionality of IsoSprite while having those two methods that you need.

AS3 OOP inheritance and functions

I am hoping someone could help me with a problem I am having trying to integrate as3 code from a puzzle I have created into my game FLA file.
In Game.fla I have a main class called Engine that contains that calls a number of empty movie clips from the library to the stage and then populates each movie clip with assets from the library. Each movie clip has its own class associated with it.
I have created each puzzle in a separate file with its own main class to test and make sure the puzzles work, when I try to add the code for a puzzle to a movieclip class I am getting a number of errors
output error
**Warning** The linkage identifier 'feedback2' was already
assigned to the symbol 'wrong_box', and cannot be assigned
to the symbol 'graphics/scrambleAssets/wrong_box',
since linkage identifiers must be unique.
and compiler error
Line 132 1136: Incorrect number of arguments. Expected 1.
line 132 is this:
if(ques_num==words.length){removeChild(checker);
f3=new feedback3;
addChild(f3);
f3.x=100;
f3.y=100;
}else{
getword();}
Main Class
public function show_level1Puzzle(){
level1Puzzle_screen = new level1Puzzle(this);
remove_levelChooseBoy();
addChild(levelPuzzleBoy_screen);
level1Puzzle_screen.x=510;
level1Puzzle_screen.y=380;
}
** Class for level1Puzzle**
package actions {
import flash.display.MovieClip;
public class level1Puzzle extends MovieClip {
public var main_class:Engine;
// variables used in puzzle
var words:Array = new Array;
Var rand1:int;var rand2:int;
var i:int; //variable used for loop iterations
// more variables
public function level1Puzzle(passed_class:Engine) {
main_class = passed_class;
public function getword(passed_class:Engine) {
main_class = passed_class;
words=["cat","dog"];
current_word=words[ques_num];
setTiles(current_word.length);
ques_num++;
}
public function setTiles(a) {tileArray=[ ];
for(i=0;i<a;i++){
var tempclip:Tile =new Tile;addChild(tempclip);
tempclip.x=300+(i*180);tempclip.y=200;tempclip.tag=i;
tempclip.original_posx=tempclip.x;
tempclip.original_posy=tempclip.y;
tileArray[i]=tempclip;
var tempclip2:Placeholder =new Placeholder;addChild(tempclip2);
tempclip2.x=300+(i*180);tempclip2.y=400;
targetArray[i]=tempclip2;
}//for i
scramble_word(a);
}
//MORE FUNCTIONS FOR PUZZLE
This function has a parameter :
public function getword(passed_class:Engine) {
main_class = passed_class;
words=["cat","dog"];
current_word=words[ques_num];
setTiles(current_word.length);
ques_num++;
}
On line 132, you are not passing a parameter, so that is the reason for the error message.

Flash Builder not recognizing certain keywords

I am trying to follow this tutorial
http://tv.adobe.com/watch/starting-with-starling/starting-with-starling-welcome-screen/
When I try to make the Assets.as class (which starts at around 7 mins) certain keywords are not being recognized by FB. Specifically Dictionary, Texture, and Bitmap. Hears the code...
private static var gameTextures:Dictionary = new Dictionary();
public static function getTexture(name:String):Texture
{
if (gameTextures[name] == undefined)
{
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap(bitmap);
}
return gameTextures[name];
I've closely followed the tutorial series up until this point yet I am still getting these errors.
Call to possibly undefined method for Dictionary
Type was not found or was not a compile-time constant: Texture, Dictionary, and Bitmap
Access to possibly undefined property: Texture
I'd appreciate any help I can get fixing this, thank you.
I think you forgot to import the named classes. Before you can use a class from the framework, you have to import it into your class:
package
{
import flash.display.Bitmap;
import flash.display3D.textures.Texture;
import flash.utils.Dictionary;
public class Assets
{
private static var gameTextures:Dictionary = new Dictionary();
public static function getTexture(name:String):Texture
{
if (gameTextures[name] == undefined)
{
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitMap(bitmap);
}
return gameTextures[name];
}
}
}
Also, in Flash Builder you can type CTRL + SHIFT + O (CMD + SHIFT + O on Mac OS) to clean imports. This will add any required import statement and ask you when there is a conflict (such as with Texture). It will remove any unnecessary imports as well.
When typing the name of a class in your code, you can pressCTRL + SPACE for auto-completion. The import for the selected class is then made automatically too.

stage.loaderInfo.parameters works but LoaderInfo(this.root.loaderInfo).parameters doesn't

I am passing Flashvars in my game.html file.
MyClass is the Document class name for my game.swf
public class MyClass extends MovieClip {
public function MyClass() {
loaderInfo.addEventListener(Event.COMPLETE, _Init);
}
public function _Init(e:Event) {
var parameters:Object = LoaderInfo(this.root.loaderInfo).parameters;
// There is nothing in parameters object.
}
}
My game.fla contains various layers on the main timeline. I have another game which has only one layer and that game loads the parameters correctly. I am finding this very strange.
Note:
trace root.name in the above code -> instance8 (there are 8 layers in the time line)
trace root.name in the other game (in which flashvars is working) -> root1 is the traced output
Am I missing something very basic here?
Solved this but couldn't understand the reason
So, I changed the line
var parameters:Object = LoaderInfo(this.root.loaderInfo).parameters;
to
var parameters:Object = stage.loaderInfo.parameters;
Why did the first approach didn't work?

pass class as parameter, then instantiate from class

I've done this before, but i can't quite remember the syntax.
What i've got (simplified):
function createText(clazz:Class)
{
var font:Font = new clazz(); //throws Instantiation attempted on a non-constructor.
}
I believe this can be done without using getQualifiedClassName, but its been a long time. Any help appreciated.
You are probably passing null to the function.
package
{
import flash.display.Sprite;
public class ClassTest extends Sprite
{
function ClassTest()
{
makeObject(Object);
makeObject(Sprite);
makeObject(null);
}
private function makeObject(type:Class):void
{
trace(typeof type);
var obj:* = new type();
trace(typeof obj);
trace("");
}
}
}
This outputs:
object
object
object
object
object
TypeError: Error #1007: Instantiation attempted on a non-constructor.
at ClassTest/makeObject()
at ClassTest()
how are you passing the class to the function ?
the calling line should have trown an error in the first place if the wanted class wasn't available, this is weird.
can you post the real code ?
here a trick for loading a class compiled in an external swf
var clazz:Class = this.yourLoader.contentLoaderInfo.applicationDomain.getDefinition("yourClassName") as Class;
Turns out I hadn't given the font a class name in CS3. So yes,I was passing null.