Pass class as constructor's argument - actionscript-3

I'm trying to pass my object class as constructor argument. I have something like this:
package myclass {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Example extends MovieClip {
public var _stageImg:Bitmap;
public var _stageData:BitmapData;
public var _stageClass:Class;
private var _stage:Object;
public function Example(stageClass:Class) {
_stageClass = stageClass;
}
public function createStage():Object {
_stageData = new _stageClass(0,0);
_stageImg = new Bitmap(_stageData);
_stage = addChild(_stageImg);
return _stage;
}
}
}
Now, when I try to create my Example:
import myclass.Example;
var example:Example = new Example(myObjClass);
I get message, that I'm not passing any variable (Error #1063). Why is that? What is wrong with it?

You are passing an instance of your class instead of the definition of the class itself.
In ActionScript it's a bit clunky, but you can get the class definition of an instance like this:
//first get the fully qualified classname, i.e. including the package
var className:String = getQualifiedClassName(myObjInstance);
//then get the class definition for that name
var Klass:Class = getDefinitionByName(className) as Class;
//and finally pass it to your constructor
var example:Example = new Example(Klass);
(note: I named the variable 'Klass' because 'Class' is a reserved keyword)

Related

access public var of an instance from a different object in actionscript3

how do you access property from one class instance to another, assuming both MCButtonA and MCButtonB instances are defined in the main class. This is without using static var.
I keep on getting:
1120: Access of undefined property varA
package {
import flash.display.MovieClip;
public class MCButtonA extends MovieClip {
public var varA:String = "abc";
public function MCButtonA() {
// constructor code
}
}
}
package {
import flash.display.MovieClip;
public class MCButtonB extends MovieClip {
public var varB:String = "abc";
public function MCButtonB() {
// constructor code
trace( ?????varA)
}
}
}
Main class:
var aButton:MCButtonA = new MCButtonA();
var bButton:MCButtonB = new MCButtonB();
trace(this.parent.aButton.varA);
upd
will work only after both buttons are added to stage

How do I create a instance of a class using a variable?

I am trying to pass a variable to a method in one of my Classes so I can use it to create the correct movieClip (image)
My Class code looks like this:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class SlideShow extends MovieClip{
public function SlideShow()
{
//does something on start
}
//This function should take the string and use it as the class name below.
public function addImages(BackGround:String):void
{
trace(BackGround);
var main_bg:BackGround = new BackGround();
addChild(main_bg);
}
}
}
and when I call the method from my maintimeline it looks like this:
var shoeSlide:SlideShow = new SlideShow();
shoeSlide.addImages("menPant");
SO "menPant" is acually the name I assigned to a class of a movieclip that has some images in it.
I am getting the following error:
SlideShow.as, Line 30 1046: Type was not found or was not a compile-time constant: BackGround.
make sure you import getDefinitionByName at the top of your class code if flash doesn't do it for you automatically. This should work.
public function addImages(BackGround:String):void
{
var symbol_class:Class = getDefinitionByName(BackGround);
//EDIT: removed data type :BackGround -- this will give an error.
var main_bg = new symbol_class();
addChild(main_bg);
}

Error #1034, with a MouseEvent

I am making a basic point n' click game and I came upon this error:
TypeError: Error #1034: Type Coercion failed: cannot convert 3 to cem.mouvement.
Here's my script:
package cem {
import flash.events.Event;
import flash.display.MovieClip;
import cem.microjeux.events.InfoJeuEvent;
import cem.mouvement;
import flash.events.MouseEvent;
public class monterJeu extends MovieClip
{
private static var pType:String = "type";
private static var pNom:String = "testNom";
private static var pCourriel:String = "test#hotmail.com";
private static var pDifficulte:int = 0;
private static var pLangue:int = 0;
private static var pTitre:String = "Veuillez sortir";
private static var pVersion:String = "1.5";
private static var pCoordonnees:Number;
private var environnementJeu:environnement = new environnement();
private var personnageJeu:personnage = new personnage();
public function monterJeu():void
{
jouer(pNom,pDifficulte,pLangue);
dispatchEvent(new InfoJeuEvent(pType,pNom,pCourriel,pTitre,pVersion));
stage.addEventListener(MouseEvent.CLICK, test);
}
public function jouer(PNom:String,PDifficulte:int,PLangue:int):void
{
addChild(environnementJeu);
addChild(personnageJeu);
}
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}
}
}
And on mouvement();
package cem
{
public class mouvement {
public function mouvement(blabla) {
trace(blabla);
}
}
}
I searched everywhere I could, and didn't find anything. I have no instances on the stage. Everything is imported on the first frame. I am kind of a beginner (let's say i'm no good at programming), so you can notify at the same time if you something that needs to be corrected. (BTW, the strange words are in french ;D)
Thanks!
The error is due to you trying to cast 3 to mouvement.
I think what you want is something like
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
var mouve:mouvement = new mouvement(3);
}
Notice that you have to have new in order to create a new instance of a class.
On another note, you should capitilize classes so they stand out better. So I would name the class Mouvement.
You are trying to cast 3 to the class mouvement into the test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
new mouvement().mouvement(3); // <-- here your error
}
If you have only a function into your class you don't need to create a class but you can put on the function alone:
package cem
{
public function mouvement(blabla):void {
trace(blabla);
}
}
and now you can call the mpuvement function normally into you test function:
function test(e:MouseEvent){
pCoordonnees = stage.mouseX;
trace(pCoordonnees);
mouvement(3);
}

Problem with create object when know object's class name

I'm tring to create instance of class, when I got name of this class.
I think better to explain my problem will be this code:
package
{
import flash.utils.getDefinitionByName;
public class SomeClass extends ParentClass
{
[Embed(source='../assets/gfx/levelImg/level0.png')]
public static const Level0Img:Class;
public function someFunction():void
{
var imgString:String = "Level0Img";
var imgClass:Class = getDefinitionByName(imgString) as Class;
}
}
I invoke someFunction, and I get error: [Fault] exception, information=ReferenceError: Error #1065: Variable Level0Img was not defined.
What can be wrong with this ?
}
You are declaring a nested Class. The definition can't be found by the name you provided.
Try this:
(...)
public class SomeClass extends ParentClass
{
[Embed(source='../assets/gfx/levelImg/level0.png')]
public static const Level0Img:Class;
public function someFunction():void
{
var imgString:String = "SomeClass_Level0Img";
var imgClass:Class = getDefinitionByName(imgString) as Class;
}
(...)
why don't you just write var imgClass:Class = Level0Img;?That's better than classname guessing...

AS 3.0 reference problem

I am finding it hard to fugure out the reference system in AS 3.0.
this is the code i have (i have trimmed it down in order to find the problem but to no avail)
package rpflash.ui {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import nowplaying;
import flash.text.TextField;
public class RPUserInterface extends Sprite{
var np:nowplaying;
public function RPUserInterface(){
}
public function init(){
var np:nowplaying = new nowplaying();
this.addChild(np)
}
public function updateplayer(xml:XML){
var artist: String = xml.nowplaying.artist.toString();
var title: String = xml.nowplaying.title.toString();
trace("UI:update");
trace(this.np);// this gives me a null reference
}
}
}
and still i cannot access np!!! trace this.np gives me a null reference. i am not even trying to access it from a subling class. (btw i def want to know how to do that as well.)
In your init() function, you are instantiating a local variable called np. Try this instead:
public function init() {
// var np:nowplaying = new nowplaying();
np = new nowplaying();
this.addChild(np);
}
Also, make sure init() is getting called before updateplayer(). Hope that helps.