How do I create a instance of a class using a variable? - actionscript-3

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

Related

actionscript 3 - error #1009 issues calling function from another class

Im having trouble calling functions from other classes. I want to call a function in one class which updates a score display in another class. The error code for this is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[code\functions\EnemyYellow.as:18]
at code::Main()[\code\Main.as:27]
Would appreciate if someone could help me out, I set up 2 basic files with the code which is causing an issue. Its normally not set up like this, I just made this for testing and so I can clearly explain the problem.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
public class Main extends MovieClip {
public var _enemy:EnemyYellow;
public var playerHP:Number;
public function Main() {
playerHP = 10;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
_enemy = new EnemyYellow;
}
public function lowerHP ():void
{
playerHP = playerHP - 1;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
}
}
}
Second File:
package code.functions {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class EnemyYellow extends MovieClip {
public var _main:Main;
public function EnemyYellow() {
_main.lowerHP();
trace ("done")
}
}
}
I also tried adding _main = new Main; in the second file but the game just loads with a blackscreen and an error about invalid data.
First of all, you surely need to "instantiate" the Main class, which means basically to create it.
public var _main:Main;
This line just declares that there will be a variable of type Main. But for now, the value of _main is null. So you are right, that you need to call:
_main = new Main();
After you've done this, the first error will disappear. But then you have things in that MovieClip that are still vague. Like _playerHPdisplay. Where's that from? Is it an instance from stage or what? You just created a brand new object, and it does not have any reference to other objects, TextFields or whatever.
So this basically answers your current question and problem, but for sure you will have more :)

AS3 Accessing a variable from an external include ActionScript File .AS not Class

i am loading in a external AS file directly into the main timeline IDE using the "include" method with some variables and calling another Class within the AS file for example;
include "vars.as"
contents of "vars.as":
var test:* = "test?!";
var foo:FOO = new FOO();
addChild(foo);
contents of "FOO.as":
package {
import flash.display.*;
public class FOO extends MovieClip {
public function FOO() {
trace("test= "+test);
}
}
}
error;
1120: Access of undefined property test.
how can i access the "test" variable inside "vars.as" from the "foo" class, is that possible?
Included actionscript files, during compilation, are treated as though they were right in the body of the actionscript you included them in. You can just access them normally as though you defined them in the same place.
trace(test); //test
ok so i found a solution, hope this helps someone else out there.
this is how you should call it from the main timeline;
var foo:FOO = new FOO(this, stage);
addChild(foo);
and here is the updated FOO class:
package {
import flash.display.*;
public class FOO extends MovieClip {
var _stage:Stage;
var _root:*;
public function FOO(root:*, stage:Stage){
this._stage = stage;
this._root = root;
trace(this._root.test);
}
}
}

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?

AS 3.0: Calling a method, which is defined in another class, from Main class gives Error 1120

I have two classes. The Main class calls a function, which is defined in a Second class. I'm getting the following error:
Error 1120: Access of undefined property myFunction
Basically, I am creating buttons in the Main class that will add a corresponding Child to an Object in the Second class (if you click one button, child x1 will be added, if you click another button, child x2 will be added, and so forth).
Here's the relevant code for the Main.as file:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
private var x1:X1 = new X1();
private var x2:X2 = new X2();
public function Main():void {
addPlayers();
}
public function addPlayers():void {
addChild(x1);
addChild(x2);
x1.x=325;
x1.y=5;
x2.x=366;
x2.y=5;
x1.label = "dog";
x2.label = "cat";
x1.addEventListener(MouseEvent.CLICK, selectPlayer);
x2.addEventListener(MouseEvent.CLICK, selectPlayer);
}
}
}
The Second.as file code is:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
public class Second extends MovieClip
{
public var myVar:MyVar = new MyVar();
public function Second():void
{
addChild(myVar);
}
private var mc_x1:Mc_x1 = new Mc_x1();
private var mc_x2:Mc_x2 = new Mc_x2();
public function selectPlayer(event:MouseEvent):void
{
if (Game(this.parent).turn == 0) {
myVar.addChild(mc_x1);
} else {
switch (event.currentTarget.label) {
case "dog":
myVar.addChild(mc_x1);
break;
case "cat":
myVar.addChild(mc_x2);
break;
default:
myVar.addChild(mc_x1);
}
}
}
}
}
i've tried defining a new variable as a public static var and that hasn't worked. i've also tried to import the Second class in the Main class and that didn't work either. any thoughts?
thank you!
If I'm understanding what your code says, you can't do what your doing. Even though you are listening to events on X1 and X2, you are listening to them FROM main. In other words, main is attempting to handle the event, and it's not finding the function you specified (selectPlayer). If you want all event handling to happen in main, then main will have to call into X1 and X2 when it receives an event.
I agree with ThatSteveGuy , in Main you are calling a function that doesn't exist, namely selectPlayer(). Following your code the first thing you should do is add a selectPlayer() function in Main.
Then you would need to add an instance of the Second class in Main in order for the following to work
private function selectPlayer(event:MouseEvent):void
{
second.selectPlayer(event);
}
Now it's a bit difficult to advise you any further because this way of doing things looks a bit convoluted but then again I would need to see the big picture. This is just an explanation about why your code doesn't work. Also, in the Second class , selectPlayer may throw an error because Game(this.parent ) will return a null value so you won't be able to access the turn property...
Is there a good reason why you need to add the two buttons in Main as opposed to adding them in Second?
try this
public function Second():void
{
this.addEventListener(MouseEvent.CLICK, selectPlayer);
addChild(myVar);
}
(and don't forget to remove x1.addEventListener, x2.addEventListener from main)

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