AS3 Add event listener to a movieclip within a movieclip - actionscript-3

I currently have two movieclips one called mcInvFrame, and one called btnCloseInv (It is a movieclip, and I know the naming convention is wrong). btnCloseInv is located inside mcInvFrame. I have two files Inventory.as and my main document class. I can load the mcInvFrame just fine to the stage and everything works as expected. However when I try to access the btnCloseInv movieclip i get errors. Here is the code for Inventory.as I have commented out my most recent failed attempt
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Inventory extends MovieClip
{
public var inv:MovieClip = new mcInvFrame;
public function Inventory()
{
addChild(inv);
/*var invClose:MovieClip = inv.btnCloseInv;
invClose.addEventListener(MouseEvent.CLICK, CloseInventory);
function CloseInventory($e:MouseEvent):void
{
this.parent.removeChild(inv);
}*/
}
}
}
What I need to know is can/should i create a variable within inventory.as for the button that I can access from the main document? If so how?
P.S. I have been searching the forums and trying various solutions but I either didn't understand the implementation or they were not suitable for this situation. The most common error I receive is "Error #1009: Cannot access a property or method of a null object reference." Occasionally I will receive an error stating that an object has no properties.

you cant register event on stage.movieclip.movieclip2 , i have tried to do the same thing before, but it wont work, try to create btnCloseInv outside, then use this code
btnCloseInv.x = mcInvFrame.x + numberHere;
btnCloseInv.y = mcInvFrame.y + numberHere2;
if you doesn't want to use this code, AS3 - Button inside MovieClip triggers MC's event
EDIT: if you set mcInvFrame.buttonMode = true it will not work

Related

TypeError: Error #1009: Cannot access a property or method of a null object reference. The object isn't null

Okay, so my game codes are not problematic at all and don't effect the game UNLESS I declare the level "OneManager" as a variable.
OneManager is the class for my level. The level is a movieclip containing all the level's components. And Main is the document class.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at OneManager()[C:\Users\Jay\Creative Cloud Files\Subject 51 Experimental\OneManager.as:38]
at Main()[C:\Users\Jay\Creative Cloud Files\Subject 51 Experimental\Main.as:16]
I don't think it makes any sense. I deleted line 38, then the problem indicated until line 39, then I deleted them over and over. Then practically until my code was useless, that's when I FINALLY got no error...
These codes don't even look problematic at all. I even tried adding them manually by adding the class's movieclip to the stage myself and it worked completely fine. But what I'm trying to do is add it to the stage through code by making this class's movieclip a variable and when the button is clicked, but if I did that - I would get these random errors from other classes.
I'm not sure what's wrong here. There is no trouble compiling, so I get no compile errors. Only errors from the output. These lines of codes aren't null, but the output says it is. I'm confused.
Please help, thanks!
Code for Main Document Class:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
var mountains: Mountains;
var homePage: HomePage;
var oneManager: OneManager;
public function Main()
{
mountains = new Mountains;
homePage = new HomePage;
oneManager = new OneManager;
addChild(homePage);
homePage.playButtons.addEventListener(MouseEvent.CLICK, onPlayButtonsClick);
}
function onPlayButtonsClick(event:MouseEvent):void
{
//var level1Page = new Level1Page;
removeChild(homePage);
addChild(oneManager);
}
}
}

MovieClip button In Class

Tried this,
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
Image
And the same code on frame 1, And there's a MovieClip Button on stage having Instance name "Homebtn".
Imports
import flash.events.*;
Importing all classes from a package that originates in flash has zero impact on compile size because they're already present in the Flash Player runtime environment. It's pure monotony that you're required to explicitly declare these imports, but good practice when dealing with third party packages.
Stage Relationship
Document code (i.e., code in the Flash IDE timelines) have a direct relationship to MainTimeline, whereas class files do not. If you want to add button1.addEventListener(MouseEvent.CLICK, myButtonClick); to your class, you're not going to be able to do so unless you:
A: Pass a pointer to the button/stage/root to the class when instantiating your test class:
var myObj:test = new test(root)
B: Wait to add the event listener until after you've given the test object a parent relationship to the stage from which to traverse to the button:
addChild(test);
inside your class...
public function test() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, registerBtn)
}
private function registerBtn():void {
this.parent.button1.addEventListener(MouseEvent.CLICK, myButtonClick);
}
Turn on Debugging
To find the cause of your bugs, you need to debug your code. If you're using Flash IDE CS6, then you can enable this by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...
null object reference at myDocument/doSomething()
...to a much clearer...
null object reference at myDocument/doSomething() package\myClass.as:20
...which now denotes which line in your code to look for your issue.
Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.
Run by going to the menu "Debug > Debug Movie > Debug", or use the keyboard combo CONTROL+SHIFT+ENTER.
Now that you're armed with the know-how to do this on your own, I'll cover what you'd encounter, and how you'd fix it (since you're new).
First, it's flash.events with an "s". So we'll change that.
Next, compiling it we get the following errors:
So we see on line 7 of our test.as class: you've placed the timeline code into the class.
var myObj:test = new test(root);
addChild(test);
You don't want to instantiate you class from within itself as it'll never get instantiated. Think of your code as a railroad. The train starts with your timeline code, and only runs on the rails set before it. Your class is floating off to the side, ready with all its interesting turns and zigzags, but you have to add it to the rails for it to be actually run. That's instantiation; we're copying that path onto the current track, and the train runs on it.
So, we get rid of lines 6 & 7, and we're left with Access of possibly undefined property Homebtn. Calling this.parent is actually a getter function, and it returns a DisplayObjectContainer. Because AS3 is a strongly datatyped language, the compiler will know that there is no such property "Homebtn" on DisplayObjectContainers (silly compiler). But of course, you know it's there (or at least it will be by the time this code runs). A simple way of getting around that is by making it evaluate the reference at run-time.
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
By encapsulating the button name as a string and within brackets, we've done that.
Now we recompile again, and get the following:
This is because all event listeners receive one argument: an event object. You may not use it, but not having a variable to hold it is a no-no.
private function registerBtn(e:Event):void {
As a final point. All class functions need to be denoted as to what namespace they exist in. myButtonClick needs one, so we'll add it as private since no external (ie., non-class based) functions need access to it.
Here's your revised code:
test.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class test extends MovieClip {
public function test() {
addEventListener(Event.ADDED_TO_STAGE, registerBtn);
}
private function registerBtn(e:Event):void {
this.parent["Homebtn"].addEventListener(MouseEvent.CLICK, myButtonClick);
}
private function myButtonClick(e:MouseEvent):void {
trace("CLICKED");
}
}
}
test.fla (timeline code on frame 1)
import test;
var Homebtn:MovieClip = new MovieClip();
Homebtn.graphics.beginFill(0xFF0000, 1);
Homebtn.graphics.drawRect(0, 0, 150, 25);
Homebtn.graphics.endFill();
addChild(Homebtn);
var testObj:test = new test();
addChild(testObj);

Removing a child of a video class called from a private function

I'm making a quiz type animation for work where on clicking an answer it plays a short animation FLV file relating to what you picked. As everything I read points towards AS3 being OOP I decided to make a MovieClip containing an FLV player and linked it to an AS3 file called FLV_Player.as. That way I can create a new instance of the FLV_Player everytime I need to play a video. Here is the code in that file which seems to work fine:
package
{
import fl.video.VideoEvent;
import flash.events.VideoEvent;
import flash.display.MovieClip;
public class FLV_Player extends MovieClip
{
public function FLV_Player(NextVideo:String)
{
animation_player.source=(NextVideo);
animation_player.addEventListener(VideoEvent.COMPLETE, vcompleted);
}
private function vcompleted(e:VideoEvent):void
{
nextFrame();
}
}
}
Now in the DocumentClass.as file I have this code:
private function NewVideo(videoname:String)
{
var nextvideo:FLV_Player = new FLV_Player(videoname);
addChild(nextvideo);
nextvideo.x = 0;
nextvideo.y = 0;
}
So when you click a button, go to the next frame or whatever the prompt is, it calls the NewVideo function and passes the name of whatever video is to be played next.
NewVideo("Introduction.flv");
Now I'm sure I'm going to run in to other issues later down the line as I really have no idea whether anything I've done is how it should be done, but the only issue I seem to be having at this point in time is removing the video and going to the next (or previous) frame to answer another question. I tried:
nextFrame();
removeChild(newVideo);
But it didn't work. Well, it may have gone to the next frame but with the video taking up the whole window it's hard to see if it did or not.
So how do I remove the video I've created? The main issue seems to be that because I had to create a new instance of the FLV_Player class in a private function the child is defined locally "var", rather than "public" or "private" var so I can't reference it again. It tells me that you can only create a "private var" from within the document class but if I make it there it will create the class on load rather than from the function when I'm ready to pass the video name parameter to it. At load I don't know what video I need it to play?
removeChild() must be called from the same object in which it was added. In this case, your DocumentClass. What you're trying to do now is telling an FLV_Player to remove itself, which won't work due to several reasons and bugs in your code.
The correct way to do things would be to have the FLV_Player object dispatch a custom event that your DocumentClass listens for. You need to create a new class which inherits from Event to create your custom event. I'd call it "PlayerEvent". In DisplayClass function you'd do this:
nextVideo.addEventListener(PlayerEvent.PLAYBACK_FINISHED, onPlaybackFinished);
addChild(nextVideo);
Then you need to create the onPlaybackFinished method:
private function onPlaybackFinished(event:PlayerEvent):void {
nextVideo.removeEventListener(PlayerEvent.PLAYBACK_FINISHED, onPlaybackFinished);
removeChild(nextVideo);
}
Inside the FLV_Player class, the vcomplete function should change to:
dispatchEvent(new Event(PlayerEvent.PLAYBACK_FINISHED));
Alternately, you could pass a pointer of the DocumentClass to the FLV_Player object, but this is very messy, can cause serious problems and not at all in the spirit of OOP. But it's a quick fix if you want to be lazy.
Events are an extremely important part of Actionscript 3 and I recommend you read up on them. Here's some good references:
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fca.html
http://www.blog.mpcreation.pl/actionscript-3-0-basics-custom-events-part-1/
I think you're right that your first problem is simply how to reference the new video, so to expand on my comment a bit: You can declare a variable without also assigning a value, so you don't need to have var nextvideo within your NewVideo function. With a class level variable instead, you can then reference whatever you set nextvideo to when you want to remove the video:
public class DocumentClass {
private var nextvideo:FLV_Player;
private function NewVideo(videoname:String)
{
nextvideo = new FLV_Player(videoname);
addChild(nextvideo);
}
private function removeVideo():void
{
removeChild(nextvideo);
nextvideo = null;
}
}

How do you access script on your ActionScript class from inside a movie-clip?

I want to be able to access a private var from inside a movieclip, does anyone know how I would go about doing that?
I want to be able to access my public function counter(numPoints:int) that is inside the ActionScript class file from inside a movieclip on my main swf. Whenever I try to access it, it comes up as undefined, but how to access it from inside the movieclip is the problem(I know the variables are all defined properly because it works when it isn't inside of a movieclip). Any idea are appreciated ^^;, Thanks!
unlock3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_3);
function fl_ClickToGoToAndStopAtFrame_3(event:MouseEvent):void
{
MovieClip(parent).gotoAndStop(8);
addScore(1);
}
I want to make it so the addScore function works properly, but it always comes out undefined. This script here is inside of the movieclip. The navigation works fine, but I don't know how to target the score.
It's still unclear what exactly you are doing, your function is named counter but you are calling addScore. See if this helps.
In your document class (lets assume it is called Main) you would have something like:
private var _currentScore:int;
public function addScore(scoreToAdd:int):void
{
_currentScore += scoreToAdd;
trace("New Score: "+_currentScore);
}
Then within your object class which extends MovieClip you would have:
function fl_ClickToGoToAndStopAtFrame_3(event:MouseEvent):void
{
Main.addScore(1);
}

How do I access the Document Class' properties from the time line in AS3?

I am building your standard slideshow flash header for a web page.
There are three main parts:
The Slideshow class
A controller class that is used as the projects Document Class
Some linking timeline code.
The slideshow class has all the functionality, so I used the Document class to create a new instance of the slideshow and keep a property variable called slideshow that keeps a reference the Slideshow instance.
import flash.display.MovieClip;
import flash.events.Event;
public class Header extends MovieClip
{
public var slideshow:Slideshow;
public function CSYC_Header()
{
var picturesURL:String = "images/pictures.xml";
var picturesURLFVar:String = root.loaderInfo.parameters.pictures;
picturesURL = picturesURLFVar ? picturesURLFVar : picturesURL;
slideshow = new Slideshow(picturesURL, Slideshow.FADE);
slideshow.init();
addChild(slideshow);
}
public function hello():void{trace("Hello");}
}
My next step now is to use Adobe Flash Professional to draw some play and stop buttons, and then link thier click events to call slidshow.play()/.pause(). This code is just place in the timeline:
import flash.events.MouseEvent;
pause_control_btn.addEventListener(MouseEvent.CLICK, pauseClicked);
play_control_btn.addEventListener(MouseEvent.CLICK, playClicked);
addChild(pause_control_btn);
addChild(play_control_btn);
function pauseClicked(e:MouseEvent):void
{
//the play and pause buttons are on the stage and have the following names as
// thier instance names: pause_control_btn, play_control_btn
pause_control_btn.alpha = 0;
play_control_btn.alpha = 0.37;
slideshow.pause();
}
function playClicked(e:MouseEvent):void
{
pause_control_btn.alpha = 0.37;
play_control_btn.alpha = 0;
slideshow.play();
}
Despite me being able to call regular methods that are in the Doc Class from the timeline, I can not call properties without the following error, for example when i say slideshow.play():
1061: Call to a possibly undefined method play through a reference with static type com.example.test:Slideshow.
So am I missing something obvious, or will I have to make a method on my document class every time I want to wire an event to call an object in my Document Class?
There's no need to put that code for the buttons on the timeline; you can reference those objects by their instance names from within the Document Class. That'd be the easiest solution, avoiding the timeline altogether, I think.
Otherwise possibly a call to parent.slideshow, or root.slideshow (though I think root is AS2, I don't quite remember) would give you access to that instance from the timeline. The former option is probably still the better option, and keeps your code in one place.
Hope that helps.