Actionscript 3 instance name property not working - actionscript-3

I have a class of a movieclip symbol that is called third_scene_border, I create 12 instances of this class like so:
public var border_1:third_scene_border = new third_scene_border();
public var border_2:third_scene_border = new third_scene_border();
public var border_3:third_scene_border = new third_scene_border();
and so on, I also set the name of the first instance to be "first_border" like so:
border_1.name = "first_border";
Then when I trace its name I get "instance(some numbers)".
Why isn't the name property being set correctly? Ive done it for a lot of other instances and its working just fine. I am trying to see on which border a draggable object is being dropped.
Edit: When I write trace(border_1.name) I get "first_border", but when I add an event listener that listens for clicks and put
trace(event.target.name);
in its function, I get instance(some numbers).
Edit: trace(event.target.parent.name); returns first_border which is correct but when I try to trace the dropTarget in the function of a MouseEvent.MOUSE_UP like so: trace(event.target.dropTarget.parent.name), I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mainClass/up()

It's hard to find a 100% solution for your case, because you didn't attach the part of your code with adding the event listener. But, I'd hazard a guess, that you should try to use the event.currentTarget parameter instead of event.target.
You may read more about differences about theese 2 properites here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#target
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#currentTarget

Related

Error #1056: Cannot create property *** on ***

I have a weird problem, and I don't know why this is happening.
I have a movieClip with the name of wellcomeMenu. It is exported for AS with the name of WellcomeMenu, and in the document class I do this:
public var _welcome:WellcomeMenu = new WellcomeMenu();
public function MainTest()
{
_welcome.x = stage.stageWidth * 0.5
_welcome.y = stage.y
addChild(_welcome);
}
All simple stuff. Then I go into the WellcomeMenu movieClip and make a shape with the name Box, then I make it a movieClip too, and give its Instance Name the name specialItem.
To sum up: I dynamically call a wellcomeMenu movieclip, which contains another movieClip with an instance name of specialItem. Then I compile and get this error:
ReferenceError: Error #1056: Cannot create property specialItem on WellcomeMenu.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at WellcomeMenu()
at MainTest()
what am I doing wrong?
When I remove its instance name, it shows just fine, but I can't manipulate the movieclip within the WellcomeMenu.
I think the problem is in specialItem attribute in the WellcomeMenu class (by the way, you may have an extra l in there if it's English). If the environment is failing to create the attribute, it is probably already there, but with the wrong permission.
If you declared specialItem manually, make sure it's public and not private (public Sprite specialItem) or else the environment won't have permission to set its value.
Another possible issue is that you declared specialItem manually and still enabled the "automatically declare instance", the environment may try to redeclare the attribute and fail. So either remove the manual declaration or disable that option.
The error can happen if you assign an object of one type to another.
var square:Square = new Square();
square.row = 9; //OK, There is a row property in the Square class
var block:Block = new Block();
square = block; //this is not a compiler error, but probably a mistake
square.row = 0; //error if there is no row property on Block

As3 imported custom class won't access components at main stage

I have this situation. I was building all in code, but it's a little painful, so I made a interface with components using the Flash drawing capabilities.
I got a main class, as usual, with the interface in a MovieClip instance called "AreaEdit". In my custom class "EditorHTML" there is a Sprite:
private var dTela:Sprite;
So the constructor is like this:
public function EditorHTML(instEdit) {
this.Parags = new Array();
this.dTela = instEdit;
trace("dTela: "+this.dTela.width+" x "+this.dTela.height);
}
At the main class:
Escrit = new EditorHTML(AreaEdit);
So trace displays the dimensions of the box, as expected. However, at the custom class, if I try to access an instance inside like this:
this.dTela.cxEdit.addEventListener(Event.CHANGE, atualizar);
An error is returned: /Library/WebServer/Documents/as3/bibliotecas_externas/com/gustavopi/txt/EditorHTML.as, Line 49 1119: Access of possibly undefined property cxEdit through a reference with static type flash.display:Sprite.
I did a test and the same instance "cxEdit" is available in main class. So it seams the components instances are not available for a custom class. How do I solve this?
Try to call it like this:
Sprite( Sprite(this.dTela).getChildByName("cxEdit")).addEventListener(Event.CHANGE, atualizar);
In case that cxEdit is a Sprite too.
Edited: cxEdit must be a TextArea. So it can be done like this:
var cxEdit:TextArea = TextArea(Sprite(this.dTela).getChildByName("cxEdit"));
cxEdit.addEventListener(Event.CHANGE, atualizar);
To make it easier for the rest of the code...
From what I can see, you are trying to access the "cxEdit" as a property of Sprite (dTela), which is not a Sprite property, hence the error.
Could you perhaps pass in AreaEdit.cxEdit as the argument instead of just AreaEdit?

As3 Adding MC from Libary and accesing content inside loaded MC

I have a movieClip I am loading from the Libary and I have properly LINKED it to export with a name of myMC. This movieclip contains another movieClip and some properties. Lets call the movieClip inside: insideMC.
Here is my code:
function loadScreen()
{
var newMC:MovieClip = new myMC();
addChild(newMC);
loadButtons();
}
function loadButtons()
{
newMC.insideMC.addEventListener(MouseEvent.CLICK, homeButtons);
}
loadScreen();
HOWEVER, when I call the function loadButtons() within the loadScreen() function then I get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at iRosary_fla::MainTimeline/loadButtons()[iRosary_fla.MainTimeline::frame1:83]
at iRosary_fla::MainTimeline/loadScreen()[iRosary_fla.MainTimeline::frame1:110]
at iRosary_fla::MainTimeline/frame1()[iRosary_fla.MainTimeline::frame1:103]
It is not seeing the insideMC. Perhaps because it's calling to fast or not loaded yet. It is calling and loading the newMC tho. Just the function loadButtons() is not working because it is not seeing the insideMC movieClip. I am sure this is an easy fix but I can't find it anywhere. Thanks
newMC is a local variable in your loadScreen() method, therefore it has no scope in your loadButtons() method.
Declare newMC as a class member variable and it will have scope in loadButtons()
for example :
// in class declarations
public var newMC:MovieClip;
function loadScreen()
{
newMC = new myMC();
addChild(newMC);
loadButtons();
}
It's important to understand that :
var newMC:MovieClip = new myMC();
Creates a local variable. From your comments, it sounds like you did have newMC as a class variable. So you assumed that the above line was assigning the new instance to your class member newMC, and not the local variable you created.
Not completely sure this is your problem. But to access a movie clip within a movie clip you have to give that "insideMC" an instance name within the first movie clip. Otherwise you'll reference an object that you haven't added to the stage - a null object.
Tutorial on instance names here

AS3 instance name strange behaviour

I have number of movieclips which I load on start, they all are instance of their respective class definations, adding MOUSE_UP listener to trace their name is behaving differently for different class objects. What possible issues can be?
var ClassDefinition:Class = purchasedItems.item as Class;
var item:MovieClip = new ClassDefinition();
item.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent){trace(e.target.toString());});
It output the name of MovieClip like this "[object bluelamp]"(required) but some other as "[object MovieClip]"(not required), while every object I create is using class defination and they load successfully.
They all suppose to output their name in a similar way
e.target will point to the item that is clicked even if it is the item in your class (child of). So if your bluelamp object contains other mouse enabled items like MovieClips, Sprites etc those can "hijack" the mouse event.
you can try with:
item.mouseChildren = false;
or use e.currentTarget in your trace statement
best regards

MovieClip extension

I have been trying to develop a CustomButton class that extends MovieClip, however I am having problems. I have got this error:
ArgumentError: Error #1063: Argument count mismatch on
mkh.custombutton::CustomButton(). Expected 2, got 0. at
flash.display::Sprite/constructChildren() at flash.display::Sprite()
at flash.display::MovieClip()
I've tried to debug my code, but it says
"Cannot display source code at this location."
I am not sure where is the problem, but I suppose it's in the constructor:
public function CustomButton( buttonlabel:String, animationAR:Array, active:Boolean=true, animated:Boolean = false, type:String = "free", group:int = 0 )
I would be very grateful if anyone helped me. Thank you.
EDIT2: I think I know why it's not appearing, so nevermind.
Seems like you must be instantiating CustomButton without passing it any arguments.
Like so:
var cBtn = new CustomButton();
However, you constructor has 2 arguments that must be passed - buttonLabel and animationAR (the rest are OK because they are assigned a default value).
So you should be doing something like this:
var cBtn = new CustomButton('Test', someArray);
I think I know what the problem is, now I hope I can explain to you clearly enough (English is not my first language). Did you by any chance make a graphic MovieClip in the Flash program and linked it to your CustomButton class? If so, be careful with the instances you might have on the stage, because when Flash creates the Sprites/Movieclips objects that are on the stage it calls their constructor without any parameters.
To avoid this, either:
Set default values for all parameters in your CustomButtonClass (EDIT: which would solve your problem, but is not very good practice)
Use addChild to put instances of your Button onto the stage (I recommend this one)
Hope this helps!
public function CustomButton(
buttonlabel:String,
animationAR:Array,
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
That's how you defined your constructor. This means that the first 2 arguments (buttonlabel abd animationAR) are required arguments. The rest are optional.
Now if you try to instantiate this like
var cb:CustomButton=new CustomButton();
You are not passing any arguments to the constructot, which will throw that error.
Note that this is what happens when you create the object directly in the UI.
A way to fix this would be to redefine the constructor as:
public function CustomButton(
buttonlabel:String="CustomButton",
animationAR:Array=[],
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
This makes all arguments optional and should work. Of course, you'll be best off putting the default value of the arguments as something you know will work. For example, in my example, the empty array default for animationAR could break your code, in which case you need to add this to the constructor body:
if(animationAR.length==0) {
animationAR.push(new Animation());
//YOU WILL HAVE TO CHANGE THIS LINE TO CORRESPOND TO YOUR CODE
}
OR ELSE, you could instantiate the object as
var cb:CustomButton=new CustomButton("My Crazy-ass CustomButton", animArray);