Play movie clip instance inside of button instance - actionscript-3

I placed a movie clip instance inside a button, and I want this movie clip to play when the button is released. I'm using this code on the frame containing the button:
function playMovie(event:MouseEvent)
{
this.theButton.theMC.gotoAndPlay(3);
}
theButton.addEventListener(MouseEvent.MOUSE_UP, playMovie);
When I try to test the flash movie, I get this message:
1119: Access of possibly undefined property theMC through a reference with static type flash.display:SimpleButton.
I can somewhat understand why it doesn't like it, but I do not know how to resolve the issue.

if you are inside of theButton already then you won't need to call "this.theButton" because "theButton" is "this"
try
this.theMC.gotoAndPlay(3);
if you are still unsure of the object parent child relation and you are using the flash IDE, in your actions panel, click the blue target at the top of the actions panel and find the MC you are trying to reference and let the flash IDE figure out the relationships for you.

Give the movieclip in your button an instance name of "theMC". Then use the following code:
function playMovie(e:MouseEvent)
{
this.theButton.getChildByName("theMC").gotoAndPlay(3);
}// end function
theButton.addEventListener(MouseEvent.MOUSE_UP, playMovie);

Related

AddChild with GetChildByName

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.

How do I edit a variable declared in the root contained within the frame of a movie clip?

So I have a main scene called Game. In that scene, I have a movieclip called Shop. Inside that movieclip called shop, I have another movie clip called upgradeweapon2. Inside upgradeweapon2, I have a button called "upgradeweaponpb".
I am working in actionscript, in the frame of the movie clip upgradeweapon2. I am trying to edit a variable called "weaponlvl" that was declared in Game. A picture to show what I mean:
http://gyazo.com/96b04ab89ea4a589bee560d53d165b03.png
I am getting the following error: Access of undefined property weaponlvl.
Please tell me there is a way around this... I know weaponlvl is defined in the root scene, game, but is there a way to make the declaration valid across levels of MovieClips, or at least a way to transfer the values accross?
Here is the code I am trying to add:
stop();
upgradepb.addEventListener(MouseEvent.CLICK, upgradeweapon5);
function upgradeweapon5(event:MouseEvent):void{
weaponlvl++;
}
EDIT: Alright I simplified my code, It's just a movieclip, not two layers. But still the same error. Any idea what I can do?
weaponlvl is on the frame of upgradeweapon2.upgradeweaponpb; it is not on the root layer, so therefore in action script it will not make sense. You have two options:
Get weaponlvl through MovieClip(root).weaponlvl or this.parent.parent (which is also the root).

Why isn't this click event being fired?

Why isn't the myMouseClick event being fired?
myMC:TestMC = new TestMC();
myMC.addEventListener(MouseEvent.CLICK, myMouseClick);
addChild(myMC);
function myMouseClick(e:MouseEvent):void {
trace("clicked");
}
As far as I can tell from the tutorials I've seen, that should work. For a moment, I thought that since I was adding the event listener to myMC, I needed to have the event function inside the myMC class, but that didn't work. Just gave an error about accessing an undefined property.
If it helps any, TestMC is a seperate .as file that extends movie clip.
I'm just trying to make it so when the movie clip itself is clicked, it does something. The movie clip itself will be following the mouse.
The object I was trying to click was made up on vertical lines. Apparently the whole movie clip isn't a collider... just the pixels in it, so when I was clicking, I wouldn't hitting enough of it. Changing it to a box worked. I could probably nest it if I wanted the same design, but that's fine.

AS3: Access button from class

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

Actionscript 3: gotoAndStop

I have a project in Flash CS3 that's giving me a bit of trouble. I have a movieclip, and in that movieclip, I have a button. The movieclip is named bg and the button tohallway_btn. My coding is on the stage on a layer, not on classes or in a package, or anything of that sort. This is my coding:
bg.tohallway_btn.addEventListener(MouseEvent.CLICK, tohallwayClick);
function tohallwayClick(event:MouseEvent):void{
gotoAndStop (141);
}
It seems simple enough, yet when I debug and the button is clicked, the flash player freezes over. I have absolutely no idea what's causing it to do this.
I get a type error in output as well:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Camille_fla::MainTimeline/enterF()[Camille_fla.MainTimeline::frame140:130]
Any help is appreciated.
An onEnterFrame listener was called and not removed that was referencing an object (bg) that was not on the stage after the goto call.
function tohallwayClick(event:MouseEvent):void {
**removeEventListener(Event.ENTER_FRAME, enterF);**
gotoAndStop(141);
}
First make sure your button and your code are on the same frame, they can be on different layers, but make sure they are lined up.
If you want it to go to the frame on your main timeline, or stage, instead of writing:
gotoAndStop (141)
try:
stage.gotoAndStop(141);