okay, i have this code on frame 1
addEventListener(Event.ENTER_FRAME, changeframe);
function changeframe(event:Event):void
{
btsakhir.y -= 6;
if (btsakhir.y <= 56)
{
nextFrame();
}
}
but when i test it whenever it goes to the next frame it show an Cannot access a property or method of a null object reference. but if in the frame 2 i add the instance btsakhir its not error. but all i want in the frame 2 is, there is no btsakhir, can someone give me solution for this so there is no error when going to frame 2 without btsakhir
What is happening is that when flash goes to the next frame, the Event.ENTER_FRAME event handler is still running. And it expects that there is an object called "btsakhir" with a y property. You can test this by adding a trace in your changeFrame() method and you should see it still tracing, even if it is on the next frame.
Two things you could do:
1.) change your event handler (your changeframe() method) to check and account for when "btsakhir" no longer exists on the next frame. This may get a little messy and not recommended depending on how you choose to do it.
Or
2.) remove the event listener so that it isn't running at all when you go to the next frame. If all the changeframe() method does is check "btsakhir"'s y property, this is would probably a better way to go. To remove the event listener you can add:
this.removeEventListener(Event.ENTER_FRAME, changeframe);
You would add it just before you call nextFrame();
Of course if your object "btsakhir" is supposed to exist on the next frame, (and is an object on the timeline/stage), just add frames to it.
Related
I am not a programmer, so please forgive me if my question is too noobish.
I created a MovieClip in my library called "skill". Then I added three instances of that MovieClip into my stage called "skill_01", "skill_02", and "skill_03" respectively. I selected all three instances and created another MovieClip called "diagram" to nest them inside it. I will later add more skill instances inside this MovieClip called "diagram" but for now, I need the basics to work with three instances.
The main timeline or stage only has 1 frame, and so does the "diagram" MovieClip instance. However, the skill instances all have two frames: in frame 1 I have an PNG image showing them as being locked/offline, and in frame 2 I have another PNG showing them as unlocked/online. I need to be able to toggle them by clicking and right clicking on them, just like in the example below (see link)
I am trying to make it so that when you left click the skill instances they toggle to Online/Unlocked by switching to the PNG image in frame 2, and when you right click them, it goes back to frame 1 (Offline/Locked). I've tried several different lines of code, including the ones recommended by Adobe itself, and others, and I can't figure out what I'm doing wrong. It will go to frame 2 and refuse to go back to frame 1 when right clicked.
This is the code I have in the MovieClip:
stop();
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
function fl_ClickToGoToNextFrame(event:MouseEvent):void
{
gotoAndStop(2);
}
This part of the code doesn't work:
skill_01a.addEventListener(MouseEvent.RIGHT_CLICK, fl_ClickToGoToPreviousFrame);
function fl_ClickToGoToPreviousFrame(event:MouseEvent):void
{
prevFrame();
}
Screenshot 1:
gyazo (dot) com/e622baee46c3fabbd8c9a8d2df8396fe
Screenshot 2:
gyazo (dot) com/1b360716a5d243aa74e2be4127fe9a5b
(Sorry for typing the links like that, it won't let me add more than 1 link because I just signed up to this Forum)
For more context, I am trying to make a Skill Tree, that works similar to this one: http://www.dungeonsanddevelopers.com/#__6_Your
I will appreciate any help figuring this out!
if you have named your movieclip, skill_01, so what is the skill_01a ?
also add your event listener and its callback function in to the movieclip's parent and inside the callback function, for accessing the event owner, use
// stop(); not necessary to call stop if you have only 1 frame
with (this.diagram) {
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_02.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_03.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_04.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_01.gotoAndStop(1);
skill_02.gotoAndStop(1);
skill_03.gotoAndStop(1);
skill_04.gotoAndStop(1);
}
function fl_ClickToGoToNextFrame(event:MouseEvent):void
{
if (MovieClip(event.target.parent).currentFrame == 1)
MovieClip(event.target.parent).gotoAndStop(2);
else
MovieClip(event.target.parent).gotoAndStop(1);
}
Edit: when an event is dispatched from clicked movieclip, only the last movieclip inside hierarchy tree will be passed as event.target in your exaple, that can be skill_01 (if currentframe is 1) or skill_1a (if currentframe is 2) athwart the question body, you have not only a png image inside skill's frame 1 and 2, they are movieclip's and will be returned as event.target. but if you only put your bitmaps (locked.png & unlocked.png) instead of skill_01 and skill_01a, the previous code works fine. bitmaps does not capture event so its parent is who accepted click event and its all a little confusing, i'm not good at explain
if its hard to comprehend
we shall use some thing like it, easier but larger code
with (this.diagram) {
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s1);
skill_02.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s2);
skill_03.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s3);
skill_04.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s4);
skill_01.gotoAndStop(1);
skill_02.gotoAndStop(1);
skill_03.gotoAndStop(1);
skill_04.gotoAndStop(1);
}
function fl_ClickToGoToNextFrame_s1(event:MouseEvent):void {
toggle_buttons(skill_01);
}
function fl_ClickToGoToNextFrame_s2(event:MouseEvent):void {
toggle_buttons(skill_02);
}
function fl_ClickToGoToNextFrame_s3(event:MouseEvent):void {
toggle_buttons(skill_03);
}
function fl_ClickToGoToNextFrame_s4(event:MouseEvent):void {
toggle_buttons(skill_04);
}
function toggle_buttons(button:MovieClip):void
{
if (button.currentFrame == 1)
button.gotoAndStop(2);
else
button.gotoAndStop(1);
}
This is a function that loads a specific video into a MovieClip container at 0.0 using a video class.
public var mainVideo:SimpleVideo;
public function loadVideo(videoString:String) :void{
mainVideo = new
SimpleVideo("videos/"+videoString+".flv","",true,video_container.positionMc);
video_container.addEventListener(MouseEvent.MOUSE_DOWN,controlVideoPlayer);
addChild(mainVideo);
trace('adding new video to container');
}
I'd like to preferably check, each time a video is loaded, to see if there already is another video loaded. And if there is, then remove it, and add the new video.
I've tried using removeChild() in a variety of ways, but it doesn't seem to work correctly.
Would I use removeChild(mainVideo)? video_container.removeChild(mainVideo)?
And how would I be able to check if there was already an existing mainVideo?
Any help would be greatly appreciated!
-Update-
If I try and use removeChild(mainVideo) I get this error:
TypeError: Error #2007: Parameter child must be non-null. at
flash.display::DisplayObjectContainer/removeChild() at
Main/removeVideo() at MethodInfo-127()
If you add the video into a container movieclip rather than onto stage, and that container only has just one instance of SimpleVideo in its display list at a time, you could do:
if( myVideoContainer.numChildren > 0 )
{
myVideoContainer.removeChild( myVideoContainer.getChildAt( 0 ) );
}
A bigger issue though is how the SimpleVideo class you are using cleans itself up. Most video classes have a cleanup or kill function that removes the listeners and cleans up the netStream. I would think you'd need to call something like that as well as removing the video instance from the container.
I hope this respone may help you
--> Would I use removeChild(mainVideo) ? video_container.removeChild(mainVideo) ?
you should use removeChild function but you need to know who is the parent of mainVideo and call this function of his parent, in your case, I think, video_container is not the parent because is a variable near mainVideo and you add mainVideo to the instance of the class which has loadVideo function
sometimes you can use mainVideo.parent.removeChild(mainVideo), not the best approach but it works, it's better to know who is the parent and call function removeChild() explicitly on that "parent"
--> would I be able to check if there was already an existing mainVideo?
well, you can do a simple if(mainVideo != null) { .. } because in function loadVideo you create new instances each time, that means if you called at least one time this function then if condition should be true that means you have already an existing mainVideo
--> TypeError: Error #2007: Parameter child must be non-null.
that means your mainVideo is not instantiated, because I guess you have not called loadVideo() yet, that's why #putvande asked you to provide more code, we need to see where and when you call removeChild()
I have a movieclip with several layers and frames. Some of those contain buttons. When I click button 1 I want it to go to the next frame. When I click button 2 I want it to go to frame 23. This is the code I'm using for button 1:
this.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.nextFrame();
}
and this is the code for button 2:
this.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.gotoAndStop(23);
}
What happens now is that when I click either of the buttons, or in fact anywhere inside the movieclip (even layers I haven't applied actions to) it executes both functions at the same time so I end up going to frame 24. Can somebody provide an answer to this problem?
Obviously this in both cases refers to the same object, and not to the buttons in particular. Record the names of those buttons as you've named their instances on the timeline, say button1 and button2 and write the code employing those names.
button1.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.nextFrame();
}
button2.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.gotoAndStop(23);
}
With this, however, you will need to update the link to gesprekkencivcen in both listeners, as those buttons will have this as parent, and their target apparently is not a child of this. I have tried to plainly set a call to parent.parent.gesprekkencivcen, which might not work.
this is my first time posting a question here.
I have an array of 12 buttons on a timeline that when first visiting that part of the timeline, get a CLICK eventlistener added to them using a for loop. All of them work perfectly at that point.
When you click one it plays a frame label inside the specific movieClip and reveals a bio on the corresponding person with a close button and removes the CLICK eventlisteners for each button, again using a for loop. The close button plays a closing animation, and then the timeline goes back to the first frame (the one with the 12 buttons on it) and the CLICK eventlisteners are re-added, but now only the first 9 buttons of the array work. There are no output errors and the code to re-add the eventlisteners is exactly the same as the first time that works. I am completely at a loss and am wondering if anyone else has run into this problem.
All of my buttons are named correctly, there are absolutely no output errors (I've used the debug module) and I made sure the array with the buttons in it is outputting all 12 at the moment the close button is clicked to add the eventlisteners back.
for (var q = 0; q < ackBoDBtnArray.length; q++){
contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[q]].addEventListener(MouseEvent.CLICK, showBio);
}
private function showBio(eo:MouseEvent):void {
trace("show the bio");
bodVar = ackBoDBtnArray.getIndex(eo.target.name);
contentArea_mc.acknowledgements_mc.BoD_mc.gotoAndPlay(ackBoDPgArray[bodVar]);
contentArea_mc.acknowledgements_mc.BoD_mc.closeBio_btn.addEventListener(MouseEvent.CLICK, hideBio);
for (var r = 0; r < ackBoDBtnArray.length; r++){
contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[r]].mouseEnabled = false;
contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[r]].removeEventListener(MouseEvent.CLICK, showBio);
}
}
private function hideBio(eo:MouseEvent):void {
trace("hide it!");
contentArea_mc.acknowledgements_mc.BoD_mc.closeBio_btn.removeEventListener(MouseEvent.CLICK, hideBio);
contentArea_mc.acknowledgements_mc.BoD_mc.gotoAndPlay(ackBoDClosePgArray[bodVar]);
for (var s = 0; s < ackBoDBtnArray.length; s++){
trace(ackBoDBtnArray[s]);
contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[s]].mouseEnabled = true;
contentArea_mc.acknowledgements_mc.BoD_mc[ackBoDBtnArray[s]].addEventListener(MouseEvent.CLICK, showBio);
}
Thanks in advance for any help and insight you can provide...I have a slight feeling that its something that may be obvious to another set of eyes...haha.
Something that looks suspicious to me is that you're removing the event listener from closeBio_btn at the start of the hideBio(). Try placing that line after the for loop.
I'm having trouble justifying this hunch, but a couple of wild theories that have crossed my mind are:
This is a race condition. You're removing the listener for the handler from inside the handler itself, and since the handler was added dynamically, the runtime marks the handler for garbage collection. On the next sweep, the handler is deleted — but since gotoAndPlay probably takes a non-significant amount of time, the handler is still in the for loop adding click handlers when it gets killed, and has only added the first 9 at this point.
This is a data-corruption problem. Since the hide button and the array of 12 buttons are at least tangentially related through BoD_mc, the runtime is doing some strange optimization behind the scenes that makes accessing items through Bod_mc[with indexes] unreliable after closeBio_btn has been touched.
This is an interesting problem, I hope you find a solution :)
It seems to fix it I had to just add every btn dynamically instead of just having them on the timeline. Still strange though, thanks everyone
I am programming a little software prototype as Flash/Actionscript3 application. Currently I registered some Events on stage - but it is cumbersome since stopPropoagation() needs to be used all the time.
As Example:
I am having a element shown via mouseclick would and a event for closing the menu on stage. Without using stopPropagation, the menu opens and closes again immediately. The hide-function is registered on some objects so just checking if target= stage would not do it, unfortunately.
Are there any good solutions to get around this?
So you have a listener for a MOUSE_CLICK on the stage thats getting fired when you click on an 'element'.
Which looks something a bit like:
addEventListener(MouseEvent.CLICK, onClick)
function onClick(e:MouseEvent)
{
trace("CLICK")
}
mc.addEventListener(MouseEvent.CLICK, onMcClick)
function onMcClick(e:MouseEvent)
{
trace("mc")
e.stopPropagation();
}
If thats the case then yes, stage will always recieve this event since it has to do with how native flash events propogate and bubble. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html
Instead of listening to the stage and having to call stopPropogation you can restructure your code. You will need to remove the listener on the stage and instead add it to the actual item so:
mc2.addEventListener(MouseEvent.CLICK, onClick)
function onClick(e:MouseEvent)
{
trace("CLICK")
}
mc.addEventListener(MouseEvent.CLICK, onMcClick)
function onMcClick(e:MouseEvent)
{
trace("MC 2 CLICK")
}
Of course this might then require you to change some of your other code but since I can't see it I am not sure what that is. Just remember that events propogate and bubble. So if you had a movieclip 'c' inside a movieclip 'b' thats on stage, and both c and b have listeners for a MOUSE_CLICK then if you click on c then both b and c events will recieve this event since it bubbles up the display list. But if c was not in b but c was on the stage and b was on the stage then this would not happen since b is not on the path for the bubbling of c. Hope that helps :)
1 solution is to check stage.focus , that is if when the menu opens the focus is on it you can add a focus out event listener so when the stage is clicked and the menu loses focus it will close .