as3 Button gets disabled, no error pop ups and no idea how it's happening and how to fix it - actionscript-3

i'm working on a AS3 ball game, currently i have 2 scenes home and baseball. home has a button which sends the player to the baseball which has a little ball catching game. the button in question is on the home page, it works fine when the FLA loads and you have to click the reb button to load the game but once the game over it sends the player back to the home and the button doesnt want to load.

You'll notice that when you return to the home scene, the output continues to trace "return". This is because you are never removing your Event.ENTER_FRAME listener for exit or resetting the Score to 0.
function exit (evt:Event) : void
{
if (Score == 2)
{
Score = 0;
trace("return");
clearInterval(interval);
removeEventListener(Event.ENTER_FRAME, exit); // <--- added line
removeEventListener(Event.ENTER_FRAME,Ballgame);
gotoAndPlay(1,"home");
}
}
This will fix your issue with the button not working in your home scene, however, you'll want to add some code to clean up the display list (as it will still show all of the baseballs that you generated on the ballgame scene).

Related

Actionscript 2.0 flash

I have written this basic button code a thousand times before on CS5. Now I bought CS6 and this simple AS2 code is not working.
On the first frame I made a movieClip symbol and added in its actions :
on(release){
gotoAndPlay(2);
}
Then I made a new layer on top, I typed :
stop();
in the frame's action.
Problem: From the second frame the animation was supposed to start, but it didn't work and stayed on the first frame even when I clicked the button several times. How to fix this issue?
I have even tried :
rooting on(release)
{ _.root.gotoAndPlay(2); }

Flash CS3 null reference to button/mc after returning to Frame

I've been researching this issue for hours and while I've found somewhat similiar situations, i have yet to find a simple fix. Basically I have a timeline where some animation plays. Eventually I get to my main game screen (frame 256) where a stop(); is called and the user then gets to click on one of 3 doors. Clicking on any door takes the user ahead a bunch of frames and they get to play a game. Once the game is done or the user clicks back, it takes the user back to the original frame (frame 256) and every single time it does this, it says my "upDoor" is a null reference and then the upDoor button(instance of upStairsDoor button) is no longer on the stage.
This seems to happen regardless of which door the user picks first. If the user picks the upDoor and plays the minigame there or picks the outsideDoor and plays that specific minigame, when the user returns to this frame (frame 256), it throws this error on the door and then of course because it throws an error, nothing else works at that point and I have to exit the game.
It's not a typo! Please don't suggest I check my instance names. As I mentioned, the door works fine the first time you get to the frame, it's just when you go back to it. I've read that it might have to do with the garbage collector but when we return to the frame, shouldn't it recreate all of the instances that I've placed to the stage? It doesn't error on ANY other button or movieClip that I've dragged to the stage, only this one particular door.
I forgot to mention that it's error'ing on a line of code that references the upDoor button. I have these lines of code here...(frame 256)
if (downDoor.enabled) {
downDoor.enabled = false;
}
if (upDoor.enabled) {
upDoor.enabled = false;
}
if (outDoor.enabled) {
outDoor.enabled = false;
}
What these do is disable the doors until the user clicks another object on the screen which then runs a function that sets all the doors to enabled. The error in question is saying I can't access a property of a null reference.
Thanks for the input guys. What ended up being the solution for me was to implement all of the movieclips and buttons programatically when the frame loads and then remove them all when I switch frames. That way, everytime the frame is reloaded from a gotoAndPlay, everything gets re-created again.

ActionScript 3: eventlisteners give 1009 error when not in frame1

Thanks for the responses
*Note when the buttons and code are on Frame 1 it works perfectly *
All the script is in Frame 2, as are the two buttons. The only things not in frame to are the sprites I'm calling out of the library.
Ideally On frame 1 are navigation buttons - each button with a gotoAndPlay() call attached. When you click each one of these navigation buttons it takes you to a different page.
Each page has a a bunch of buttons. Each button, when clicked, plays a an audio, and adds/deletes children to the stage
My problem is any frame other than 1, (in this case 2) as soon as it lands on the frame even with appropriate buttons present it says it doesn't see them and balks at the even listeners.
I hope this makes sense.
----- Original post --------
I'm trying to understand how these things work. I have an empty frame in frame one. In that frame I have the code:
gotAndStop(2);
On frame 2 I have two buttons. I've added event listeners to them. This works fine. The problem is as soon as it hits frame two I get this error :
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at TesT_2_fla::MainTimeline/frame2()
The confusing thing is, when I have everything in Frame 1 it works like a charm.
On frame 2 the buttons are there already, if I put them in frame 1 it works, but in frame 2 no dice.
Can anyone explain what is happening and how I might be able to remedy this?
Not sure why you are writing the script in Frame 1. If nothing were in Frame 1, the user would automatically go to Frame 2, if you placed a stop(); on Frame 2. If you want to stop(); on Frame 1, you could add a button gotoAndStop(2) with an event listener. When placing script on a frame that may be also referenced in a later frame, use a separate script layer and extend that layer from the (key)frame on which the script first appears to the (key)frame on which it is also referenced. HTH
Ya know - after much searching I found a solution it does seem awfully cumberome. I added the following code (which I copied from someone elses post) Just in case any other noobs have a similiar problem
enter code here
//listen for the Flash player's ENTER_FRAME event...
this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false);
//and call this checker function continually until all of the buttons are accounted for
function onEnterFrame(e:Event):void
{
if(and_btn != null && big_btn != null )
{
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
//clean up the enter frame listener so that this function no longer gets called
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);
}
}
It seems the code was running so fat the buttons didn't have a chance to populate the stage

My flash menu going crazy. AS3

I have a menu with 4 button, for switch to other content page, and all of it had this code:
function chuyenhome(e:MouseEvent):void{
homect.addEventListener(Event.ENTER_FRAME, nexttohome);
btn_home.gotoAndStop(15);
if (btn_about.currentFrame == 15){
btn_about.gotoAndPlay(16);
}
if (btn_menu.currentFrame == 15){
btn_menu.gotoAndPlay(16);
}
if (btn_contact.currentFrame == 15){
btn_contact.gotoAndPlay(16);
}
homect.gotoAndPlay(31);
}
function nexttohome(e:Event):void{
if (homect.currentFrame>=homect.totalFrames){
MovieClip(root).gotoAndStop(4);
}
}
This is menu HOME. 3 other menu ABOUT, BLOG, CONTACT button is using the same code, but renamed function. It work fine if I click only 2 menu button. But when I try to click the third menu, it's going crazy. For example, when I click at HOME and ABOUT, it switch to HOME page and ABOUT page with no problem, no matter if I click it for thousand times. It work fine, but when I try to click at CONTACT, then... it show up HOME page, I re-click CONTACT, it show up ABOUT... and so on. It's crazy. Sorry about my English. I'm using Flash CS4 and AS3. Thanks!
You have to not add multiple event listeners to one MC. See, if all the other functions are this one copypasted with altered frames, then each of them adds an enter frame listener. These are cumulative, thus calling homect.addEventListener(Event.ENTER_FRAME, nexttohome); twice will result in two calls to nexttohome per frame. Probably you can avoid this listener altogether by putting code to the timeline of homect movie clip on the last frame, to call stop(); MovieClip(root).gotoAndStop(4);

Touch movement will not animate

Ok, so I'm in the process of developing a game in Adobe Flash (air) it will be a mobile game.I asked this question a while ago, but animation doesn't work so know I'm back.Anyway, the problem I was having was that there was no continuous movement when I held down a button, so I had to constantly keep pressing the button to move.the person told me to use ticks, so it's how I built the movement engine, all around ticks.When I go to add my animation, it goes once and just stops (the animation) here is my code:
myButton.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,tick); //while the mouse is down, run the tick function once every frame as per the project frame, this is where I add the players animation.
}
function mouseUp(e:Event):void {
removeEventListener(Event.ENTER_FRAME,tick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
function tick(e:Event):void {
//do your movement
}
What I'm saying is, is there anyway to get around the problem? How can I animate with the tick!
Like others, I find the code perfectly fine. Since, I don't have access to entire code and timeline, I have the below guess about the problem:
Since, you want to start animation only on mouse down, then you must be having a stop() action in the first frame of your animation. (Please correct me if this isn't the case). The last frame of your animation must be having gotoAndPlay(x) action again for looping.
Based on above assumption, when your animation loops, it again reaches a frame which has stop() action already defined.