ActionScript 3: Arguments in empty constructor in class - actionscript-3

I have source code of one software, that have maded in ActionScript 3 (there about 3000).
I'm not familiar with AS3. And I have to understand one problem.
I have 2 class.
Frist of them
package test.test1.test2
{ 
public class ClassName extends Object 
{ 
public function ClassName() 
{ 
super(); 
} 
public static function function1(var1:*) : ClassName 
{ 
var varX:ClassName = new ClassName(); 
varX.class_var = var1.subvar;
return varX; 
} 
public var class_var:Number;
} 
} 
The second class:
package test.test4.test5
{ 
....... 
import package test.test1.test2.ClassName; 
........ 
public static function function2(loc_var1:int, loc_var1:int) : Object 
{ 
var _loc16_:ClassName = null; 
......... 
_loc16_ = ClassName(_loc15_.xxx); 
..... 
I don't know exactly the data type of loc15.
How is it possible??
As I know function2 is used in the program and it works.

Related

How to import class from user actionscript file

I just want to create a class with two Numbers and a Bitmap. You can't have nested classes in a Timeline script so I figured I'd make .as file, namely "node.as". But I cannot for the life of me figure out how to import this class into the Timeline script so that I can use the class in my Timeline script.
Please help!
You should be able to create a package to accomplish this. The basic format is:
// folder specifies the relative folder of the package
package myfolder {
// whatever other classes you need
import flash.display.*;
import flash.events.*;
// base this on whatever class you need (in this case MovieClip)
public class myclass extends MovieClip {
private var myvariable: String;
// constructor
public function myclass() {
}
// functions only for use within the class
private function myprivatefunction() {
}
// functions for use by the rest of the world
public function mypublicfunction() {
}
}
}
The constructor is called when you create a new instance of the Class-- this is where you would do your initialization. Then, in your timeline, just add something like:
import myclass;
var myclassinstance = new myclass();
myclassinstance.mypublicfunction(); // call a function in the class
See adobe's help for more details.
So I tried this:
package {
import flash.display.*;
public class Node {
public var fX:Number;
public var fY:Number;
public var bmp:Bitmap;
public function Node() { }
}
}
And put it in a file called Node.as (note I needed to add the flash.display import, and the capital "N" in the filename). In a FLA file in the same directory, I added this to the first frame script:
import Node;
var node = new Node();
trace("node="+node);
It compiled and ran without error. There must be something else going on? What version of Flash and of ActionScript are you using?

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);
}

Pass class as constructor's argument

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)

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...

Constructor arguments problem ActionScript 3

I have a custom class defined in Actionscript and I want to make an instance of it in the main document of Flash application. However, after calling the constructor with one argument, Flash gives me this error:
Error #1063: Argument count mismatch on coa.application::MenuItem(). Expected 1, got 0.
This is my class:
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}
And this is my attempt to call its constructor:
var menu1:MovieClip = new MenuItem(3);
Any idea, whats wrong?
Apologies, I can't comment yet, or I'd put this in a comment.
Are you sure that:
var menu1:MovieClip = new MenuItem(3);
is the only place that you're constructing a new MenuItem? You don't by any chance have the MenuItem class attached to some instances on the stage?
I changed your code to this (just so I could run it) and it works fine:
package{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class MenuItem extends MovieClip{
var button:SimpleButton;
public function MenuItem(buttonLoc:uint) {
button = new SimpleButton();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
public function mybringToFront(event:MouseEvent):void{
trace('blah');
}
}
}
Like quoo said, most likely you have an instance of the object that the class is attached to on stage. To test for that do this:
public class MenuItem extends MovieClip{
var button:SimpleButton;
// I changed it to int, cuz uint is extremely slow for any math
// other than bitwise operators, int is fast as long as no fractions
public function MenuItem(buttonLoc:int = -1) {
if (buttonLoc == -1)
trace("On stage instance found! Location: "+x+", "+y);
button = new InvBtn();
this.addChild(button);
button.x=-81;
button.y=buttonLoc*33;
button.addEventListener(MouseEvent.CLICK, mybringToFront);
}
}