Actionscript 3: Class constructor in function variable? - function

I want to make a variable hold a class constructor, but it gives this error:
var myFunction:Function
function someFunction() {}
myFunction = someFunction //works
myFunction = MovieClip //doesn't work - 1067: Implicit coercion of a value of type Class to an unrelated type Function.
Is there a way to do it?

The correct type to hold a class is Class, not Function.
var myClass:Class = MovieClip;

Related

Please help me solve this actionscript 3 Error 1067

Please help me correct my mistake. I received this Error:
1067: Implicit coercion of a value of type of void to an unrelated type flash.display:MovieClip.
import flash.events.Event;
import flash.display.MovieClip;
var vidLc:MovieClip = null;
var vidLc1:MovieClip;
//--Play Count MovieClip
function countingPlay():void
{
vidLc = new CountingVid();
//--
vidLc1 = vidLc.play();
//--
addChild(vidLc1);
vidLc1.x = -1.80;
vidLc1.y = 2.10;
}
//-- stop count MovieClip
function countingStop():void
{
if (vidLc){
vidLc1.stop();
removeChild(vidLc1);
vidLc = null;
}
}
According to your posted code, that error comes from this line :
vidLc1 = vidLc.play();
Here you are using the MovieClip.play() function on your vidLc MovieClip (vidLc.play()), which did return nothing, to initialize your vidLc1 MovieClip, and that's why the error #1067 is fired, but in the case where you've overridden that function in your CountingVid class, then it should return a MovieClip object.
Hope that can help.

Function.call( thisObject ) not working on AS3

I have a very simple code here that explains the difference I found between AS2 and AS3 when using the call method of a function.
var a = {name:"a"}
var b = {name:"b"}
function c()
{
trace(this.name)
}
c() // AS2: undefined AS3: root1
c.apply(a) // AS2: a AS3: root1
c.apply(b) // AS2: b AS3: root1
How can I force AS3 to respect the thisObject argument in AS3?
Here is Adobe Documentation
"http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html#apply()"
Thanks
It's important to recognize that Functions are different from Methods. Methods are bound to the objects that they are defined in, whereas Functions are not bound to any object.
When you are using apply or even call on a method you are extracting it from its instance, but it will always be bound to the object.
So in your example if c() is inside an object, that is why you are not seeing thisObject change.
From adobe on Methods:
Methods are functions that are part of a class definition. Once an
instance of the class is created, a method is bound to that instance.
Unlike a function declared outside a class, a method cannot be used
apart from the instance to which it is attached
Now if you want to be able to change the thisObject you can create a function outside of the object and pass the new thisObject parameter. Here's a very basic example:
class myClass {
public function myBoundFunction():void {
trace( "Bound to: " + this );
}
}
//declared outside the class
function unboundFunction():void {
trace( "Unbound: " + this.name );
}
Then instantiating and applying the functions with thisObject parameter:
var c:myClass = new myClass();
//bound function call
c.myBoundFunction.apply( this );
//unbound call
unboundFunction.apply( this );
Output:
Bound to: [object myClass]
Unbound: root1

AS3 #1067 Implict Coercion of a value of type flash.utils.timer to an unrelated type Function

I've been getting this error and I have no clue how to fix it, it works in another class so it should work here, right? (I moved it from that class to this one) The only difference is that this class extends 'Game' and the other class extends 'MovieClip'
1067: Implicit coercion of a value of type flash.utils:Timer to an unrelated type Function.
public static var timeLeft;
public function GamePlay() {
// Start timer
var timeCounter:Timer = new Timer(1000, timeLeft)
timeCounter.addEventListener(TimerEvent.TIMER, timeCounter);
timeCounter.start();
}
// Handle time counter
public function timeCounter(e:TimerEvent):void {
timeLeft--;
trace(timeLeft);
}
You need to give Timer object and listener function different names:
public static var timeLeft:int;
var timer:Timer = new Timer(1000, timeLeft)
timer.addEventListener(TimerEvent.TIMER, timeCounter);
timer.start();
public function timeCounter(e:TimerEvent):void {
timeLeft--;
trace(timeLeft);
}
I assume timeLeft is set somewhere else?
Your function and your Timer are both called timeCounter, so it thinks you're trying to pass the Timer as a function (hence the error). You should rename one of the two, here I've renamed the function:
public static var timeLeft;
// Start timer
var timeCounter:Timer = new Timer(1000, timeLeft)
timeCounter.addEventListener(TimerEvent.TIMER, timeCountHandler);
timeCounter.start();
// Handle time counter
public function timeCountHandler(e:TimerEvent):void {
timeLeft--;
trace(timeLeft);
}

As3 Vector<T> parametric TYPE

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? :-)

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.