Hi, my name is Bryan, and I got to say that I am new to actionscript 3
Yesterday I learned a little bit more about it, Straight to the point, I am making an iOS app using Flash CS6
and everything seems right,
I have my codes correctly, and my back and next buttons stop working after I go back to the previous scene
Ok, lets explain better, my app starts with an introduction, bla bla bla, after there's a menu with options,
i hit the first "Chapter in this case" and goes to a new frame, i hit next next next etc. with no problem,
but when i am back to the "chapter" menu and hit the first chapter again the next button stop working
need some help
here are my btns codes:
import flash.events.MouseEvent;
stop();
// EventListeners
back_btn.addEventListener(MouseEvent.CLICK, backFunc);
menu_btn.addEventListener(MouseEvent.CLICK, menuFunc);
next_btn.addEventListener(MouseEvent.CLICK, nextFunc);
// Functions
function backFunc(evt:MouseEvent) :void {
prevFrame();
}
function nextFunc(e:MouseEvent){
nextFrame();
}
function menuFunc(a:MouseEvent) :void {
gotoAndStop(3);
}
I Hope i can get some help ![]()
If this code is on frame 1 and you allow your "prev" button to return back to that frame it will re-assign all the buttons again and again. You should definitely init your app on frame 1 but then disallow the app to return to frame 1. So push all your content over to frame 2+ and never return to frame 1.
Otherwise you can set a flag for yourself to know if you need to assign the button handlers. If it's the first time you run the app you can check for the existence of a variable. If it doesn't exist then set it and init your buttons.
e.g. Frame 1:
if (!applicationAlreadyStarted)
{
// add event listeners
var applicationAlreadyStarted:Boolean = true;
}
Then the next time it hits that frame the variable will exist and you won't accidently re-assign all your buttons. Add any other init code in there that should only fire off once when your app is started. It might be what's causing trouble.
In your case I think the second method I mentioned may just be easier for you so you don't need to affect the timeline. However I can't recommend that unless I know what your timeline looks like.
Before continuing can you tell me of the buttons "back_btn", "next_btn" and "menu_btn" exist across the entire timeline without any keyframes breaks between them? If so the layer would look solid, similar to this where you see different content keyframes on all 10 frames but the buttons layer has no keyframes past frame 1:
As you can see the buttons layer has one long gray keyframe. Therefore any code inserted in frame 1 will continue to exist on the buttons all the way up to frame 10.
However if you insert a keyframe on the buttons layer, flash will "re-create" those buttons. Therefore they will not have any code applied to them unless you specifically apply it yourself on that frame. An example of that looks like this:
That would require you to re-apply your mouse click listeners every single frame to assure they work.
This just a quick example not to be taken too literally but I need to know what your timeline looks like to make the best suggestion.
Thank you, I did something like that and now it is working, now i need to add the code to back btn so when it is on certain frame it send a message error saying that you can't go back, and the same with next button
so my code looks like this
function backFunc(evt:MouseEvent) :void {
var frame:Number = this.currentFrame;
if (frame != 2) {
prevFrame();
} else (frame == 2); {
stop();
}
}
but what I don't know how to do is refer to more than one frame at a time,
In the way i was trying to do is creating an var named for example DeniedFrames, so if DeniedFrames == 1 it means that in those frames you can't use the back button and it sends a message, else DeniedFrames == 0 prevFrames(); can you help me with the code, I only need to know how to refer to more than one frame for example: If (currentFrame == 2 or == 5 or == 7 or == 11) {
DeniedFrames = 1;
}
else {
DeniedFrames = 0;
}
Could you help me and thanks for taking your time to do this guys i really appreciate that ![]()
You can make it as simple as:
function backFunc(evt:MouseEvent):void
{
if (this.currentFrame > 1)
{
prevFrame();
}
}
If the frame isn't "greater than 1" then it will simply ignore the click. Use the same logic in nextFunc and see if you can write it so if this.currentFrame < 6 it will fire off nextFrame();.
Oy, forum error. I just posted a function on how to do that. I'll retype it quickly..
function backFunc(evt:MouseEvent):void
{
var invalidFrameNumbers:Array = new Array(2, 5, 7, 11);
var validPress:Boolean = true;
for (var i:int = 0; i < invalidFrameNumbers.length; i++)
{
if (invalidFrameNumbers[i] == this.currentFrame)
{
// matched an invalid frame number, set flag, break loop
validPress = false;
break;
}
}
// check if it was a valid press
if (validPress)
{
prevFrame();
}
}
There are other ways to do this like searching arrays but this illustrates some basics you definitely want to get down. Things like Arrays, Boolean flags and looping over data.
This makes an array of invalid frames and sets a flag. It loops over all the items in the array and if any of the numbers match the current frame number it sets the flag to false and breaks the loop. After the loop it checks to see if the flag states the press is valid and only then will it run prevFrame().
You'd use similar logic for the next_btn.
I did this code to the next btn but it didn't worked, can you tell me whats wrong?
function nextFunc(e:MouseEvent) :void{
var InvalidFrames:Array = new Array(5, 11);
var validPress:Boolean = true;
for (var i:int = 0; i < InvalidFrames.length; i++) {
if(InvalidFrames[i] == currentFrame) {
validPress = false;
break;
}
}
if(validPress) {
nextFrame();
}
}
Often you need to look inside code to see what's going on. That code looks fine to me. I re-wrote it but added some trace() statements. When you press your next_btn you should see the messages I'm trace()ing. See if anything unusual is happening.
function nextFunc(e:MouseEvent):void
{
var invalidFrames:Array = new Array(5, 11);
var validPress:Boolean = true;
for (var i:int = 0; i < invalidFrames.length; i++)
{
trace('Checking if invalidFrame[' + invalidFrames[i] + '] == [' + this.currentFrame + ']');
if (invalidFrames[i] == this.currentFrame)
{
trace('Invalid frame matched, press invalid');
validPress = false;
break;
}
}
if (validPress)
{
trace('No invalid frame detected, running nextFrame()');
nextFrame();
}
}
Your trace output can be found in the output panel. Say you're on frame 11, you should see something like this:
Checking if invalidFrame[5] == [11]
Checking if invalidFrame[11] == [11]
Invalid frame matched, press invalid
If you were on frame 1 then it should look like:
Checking if invalidFrame[5] == [1]
Checking if invalidFrame[11] == [1]
No invalid frame detected, running nextFrame()
Use trace() statements as much as possible along with your debug panel to track down logic issues.
The code tends to be right. Might want to run your play head across the timeline and take a look at the frame number you think is wrong and I bet you'll see it's right. For example the menu you might think is appearing on frame 5 actually appears on frame 4, or vice versa.
Glad you got it working. If you're all set please mark it answered so we can attend unanswered questions and good luck!
North America
Europe, Middle East and Africa
Asia Pacific