What I want to do is to add button as a child to a movie clip which is on stage. Get input in a text field and then save that input into String variable. This button should trigger this actions. Only problem I find is that I can't reach the button which is in library. I have set button AS linkage and I am trying to reach it trough that linkage.
This is the line of code which dosen't work:
enterName.addChild(nextButton);
Error I get is: Line 254 1061: Call to a possibly undefined method addChild through a reference with static type Class.
Since your button has a linkage, you should instantiate it, which means:
enterName.addChild(new nextButton());
An even cleaner version would be to have your linkage name with a first letter upper case (this is commonly how classes are named). And you could cast this instance as a DisplayObject to tell the compiler you know this class extends something you can add to a container:
enterName.addChild(new NextButton() as DisplayObject);
Related
Complete AS3 noob here - I've tried Googling this, but I can't seem to find what I'm looking for (I stumbled across this, http://ub4.underblob.com/as3-naming-elements-dynamically/, but it doesn't weem to work for me).
I'm trying to dynamically add a Movieclip inside another Movieclip through an external AS3 class
Something like this:
var bullet:Bullet = new Bullet(x, y, "right");
var stageBackground:MovieClip = (stage.getChildByName("back") as MovieClip);
stageBackground.addChild(bullet);
However, while this compiles correctly, at run time, I get error #1009 - Cannot access a property or method of a null object reference.
The debug panel tells me the problem is with this line:
stageBackground.addChild(bullet);
But I can't seem to figure out what's wrong with it. I've tried recasting stageBackground as a Sprite, but that didn't change anything. I know the MovieClip back exists - when I reference it through near identical code in my document class, it works perfectly.
You are accessing stage here to find your container, which is very likely the problem.
You are probably thinking that the stage property refers to "the stage" in Adobe Flash authoring environment.
That's not true.
If you placed a MovieClip on "the stage" in the Flash IDE, it ends up on the main time line, this however, is not the thing the stage property is referencing. stage is the topmost DisplayObjectContainer in the display list. It only exists at runtime. It more or less represents the FlashPlayer window, the runtime environment that executes your .swf file.
In short: you are simply looking for your back MovieClip in the wrong place.
The property of a container that represents the main timeline is root.
Do not use root either.
As you can see, your code becomes dependent on the display list
structure of your application. You are already struggling to find the
container that you are looking for. If you change the structure, your code breaks. Even changing the name of the container (for example to something like "background") will wreak havoc.
Instead, use Events.
You are in another class and you want to fire a bullet.
So you create that bullet same as you do now:
var bullet:Bullet = new Bullet(x, y, "right");
Next, dispatch an Event to notify the rest of your code that a bullet was created and it should be placed in the appropriate container:
dispatchEvent(new BulletEvent(BulletEvent.CREATED, bullet));
(Create a custom event class BulletEvent that extends Event, with the apropriate setter and getter for a Bullet object)
Register a listener on the object of your class that creates the bullets, you will catch this event and place the bullet in the container:
var object:YourClass = new YourClass();
object.addEventListener(BulletEvent.CREATED, addBulletToContainer);
function addBulletToContainer(e:BulletEvent):void
{
// adding the bullet to the container
back.addChild(e.bullet);
}
This code would be placed in the parent of your back MovieClip.
The Flash IDE automatically creates variables behind the scenes that have the same names as the instance names. That's why the variable back is available here.
Using events here allows you to literally fire the bullet into your code with somebody else taking care of it, where it's easy to figure out the container it belongs into.
I am adding a widget to someone elses movieclip.
On stage is another movieclip which has an action inside the movieclip (getting to this by pressing F9 in Flash).
function onClicked()
{
parent.onOldBtnClick("go");
}
What I need to do is to get rid of this behaviour and add my own action.
How can I override this action?
If it's ActionScript3, just removeEventListiner on the MovieClip, that triggers onClicked() and addEventListener for your own action. If you don't know the exact listener, you can trace it with hasEventListener. For more info on Events and how they work, you can read on ActionScript3 referance
To override, just delete that line in the onClicked function and write your own. Care must be taken while writing your block of codes, mainly scope. Because your are writing the code inside the movieclip, if you are trying to access the another movieclip in stage(withour specifying parent or root or its actual scope) then it will throw error.
So im quite new to AS3 but have worked with AS2 a lot before.
I have created a button and placed it on my stage then inside my class i have added this:
test.addEventListener(MouseEvent.CLICK, buttonClicked);
function buttonClicked(ev:MouseEvent):void
{
trace("Clicked");
}
Now this does not work as it can't find the stage, the only way i can get this to work is if i put the listener on the same frame as the button & not in the class.
But there must be away around this.
Thank you.
Eli
Update - adding Error messages
If I keep the above code all in the external class these are the errors i get.
Line 22 1120: Access of undefined property test. Line 22 1120: Access
of undefined property myButtonClick.
If you have created a document class with timeline then your "test" button must be in first frame. Because document class starts executing from first frame. You can access your button instance only when its available in stage.
Oh, I forgot to mention. You have to declare those instances as public variable in your document class.
public var test:SimpleButton;
Please go thru below and let me know which of the way you were having.
1) Are you having Document class?
There is a field Class in the Document Properties under the Publish tab of the flash IDE, if you are giving your class name in that field then it is called as Document Class
If you are having document class then you can create listener to your button even in the constructor button. Flash won't throw any errors like you got.
2) If you are instantiated your class in the first frame, it won't have the properties of stage till you add that to the stage using addChild. Also it won't have access to your button. And so it will throw the error, The access of undefined property.
Have you assigned the instance of the button on the stage the name "test"? The error message you posted seems to say there is nothing with the name "test" to assign the event listener to.
To check, click on the button the stage and look at the 'Properties' tab: will show in a text box near the top if it needs assigning.
Now the second error you posted means you're referring to something called "myButtonClick" without first declaring/initialising a variable/function with that name. You will either need to declare it or correct it if you meant to refer to something else.
Fixed.
I was being rava stupid as normal, forgot to put them inside the init :|
For the people who might come across this problem.
Working Code:
public function Main()
{
// constructor code
test.addEventListener(MouseEvent.CLICK, myButtonClick);
}
function myButtonClick(ev:MouseEvent):void
{
trace("button Clicked);
}
Anyway thanks guys for the help sometimes its just the simplest answers are the correct ones.
Eli
In document class, constructor, following line produces following error:
name = "hello world";
Error: Error #2078: The name property of a Timeline-placed object cannot be modified.
Is this doable or hackable?
I did some test, and it seems you can't.
"MainTimeline is the default class used for the document class. The document class is the display object which is added to the stage when the SWF movie is loaded. Underneath it is a normal class which extends MovieClip." (found here on SO)
Probably the implementation of this class blocks you to change its name. As you're not instantiating it, probably flash assign a name to this instance and doesn't want you to change it :)
I'm trying to call a movieclip called mcMain that's already on the stage. I'm calling it from a class and I've tried googling a whole bunch of possible solutions, none of which appear to work. I've tried stage.mcMain, this.stage.mcMain, MovieClip(root).mcMain, but nothing seems to work. Anyone got any ideas? I don't even get an error message. Just nothing happens.
I don't think that the root of your document timeline is actually the stage. However, you shouldn't be doing this. If the "Class," as you call it, is a DisplayObject, it should not know about anything outside its own scope (unless you've exposed properties or methods on it that would allow for this information to be passed in. If the Class is a data class it shouldn't know about the View at all. If it's a controller class, you'll need to pass a reference to it.
However, given the code you said you tried, I'm guessing your Class instance is some sort of DisplayObject. What you should do is dispatch a bubbling event from your Class, and then in your main Document class, listen for that event. In the event handler function, do whatever you need to do with your mcMain instance, such as adding ketchup. This should work just fine, because your main document Class can receive events from anywhere in the display list, and mcMain is its own instance.