Copy link to clipboard
Copied
Hi,
I am very new to actionscript programming. I am stuck at finding out how the sequencing of buttons is done?
For Example, i have got 8 buttons on the page which are to be selected in a pre defined order. Upon each selection and if its in the correct sequence, it takes the page to the required scene. Otherwise it should show an error and redirect to the main page.
could someone please help me with this?
The first thing you should do is get rid of using scenes. They are only a problem waiting to happen. Either use frames of just the one timeline or use movieclips and control which is visible. You will find it alot easier to manage of you take the latter approach.
The next thing you need to do is get that function out of the loop you have it in. You only need to assign multiple listeners... the function is coded generically and only needs to be defined once.
Another thing to avoid doing is usin
...Copy link to clipboard
Copied
You need to think thru a logic scheme wherein you keep track of the button usage order and compare it to a predefined order based on keeping a count of where you are in that order.
One approach would be to store the instance references of the buttons in an array in the order they need to be for a correct sequence . Then have a counter that starts at 0 and gets incremented each time a button gets used.
When you click a button you check to see if the button that was clicked (the event's currentTarget property will tell you that) matches the button in the array using the counter index...
if(event.currentTarget == orderedButtonsArray[counter]){
//increment the counter
// if the end of the array is reached, you sequence was followed correctly
} else {
// show an error
}
Copy link to clipboard
Copied
Hi Ned,
Thank you for the reply.
But i am fairly new to all this. I would really appreciate if could elaborate this with an example? It will help me to understand more clearly.
Copy link to clipboard
Copied
I just gave you an example. What part of it do you not understand?
Copy link to clipboard
Copied
If you want a more detailed example, go to the following link:
http://forums.adobe.com/thread/1161732?tstart=30
It looks to be asking for exactly the same thing you are, so it might be you in that posting, or it might be a classmate, or it might just be a strange coincidence.
Copy link to clipboard
Copied
Thanks for that link. By the way, it was not my post.
What i am actually doing is:
I have got 8 buttons whose instance name i have given as btn1, btn2, btn3, btn4....btn8
supposingly the correct sequence is btn3, btn4, btn2, btn6, btn7, btn1, btn5, btn8.
If the user clicks on btn3 first, then the it should move to a different scene. Then on that scene there is a button for coming back to the home page where all these buttons are listed. Like this if the user clicks on the correct button in the given sequence the procedure will be completed.
But, in case the user in between clicks on some other button then it should move to an error scene saying "wrong option selected, please try again" and then there is a button for coming to the home page again. In this way he can try again and again till he gets the right order.
Could you please help in this?
Copy link to clipboard
Copied
As far as I can tell, you have all the information you need to be able to do this now, including a detailed coding sample. To ask for more help at this stage is basically asking someone to do the work for you, and I am not one of the folks here that does that. There are a few others that will if you want to wait and see if they come around.
If not, then start in on trying to solve this with what you have been given. If you need help along the way then present your problem along with the code that is not doing what you want.
Copy link to clipboard
Copied
I have already started to work on it based on the approach you gave. I am facing some issues. Will try to work on these again.
Thanks.
Copy link to clipboard
Copied
Hi Ned,
I need a help!
I have tried with the coding. I created three scenes:
1st scene: 5 buttons are placed;btn1,btn2,btn3,btn4,btn5 and the order is same as the numbering of buttons
2nd scene: correct page
3rd scene: error page
Now when i click on btn1, it takes me to the correct page. And, if i click on any other button nothing happens.
Below is the code:
var myArray:Array = [btn1,btn2,btn3,btn4,btn5];
var counter:int = 0;
for(var i:int=1;i<=5;i++)
{
this["btn"+i].addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);
function fl_ClickToGoToScene(event:MouseEvent):void
{
if(myArray[counter] == event.currentTarget)
{
counter = counter+1;
MovieClip(this.root).gotoAndPlay(1, "Scene 3");
//increment the counter
//go to the correct scene
}
else if(myArray[counter] != event.currentTarget)
{
MovieClip(this.root).gotoAndPlay(1, "Scene 4");
// show an error
}
}
}
Copy link to clipboard
Copied
The first thing you should do is get rid of using scenes. They are only a problem waiting to happen. Either use frames of just the one timeline or use movieclips and control which is visible. You will find it alot easier to manage of you take the latter approach.
The next thing you need to do is get that function out of the loop you have it in. You only need to assign multiple listeners... the function is coded generically and only needs to be defined once.
Another thing to avoid doing is using unnecessary root references. If the code is in the main timeline, there is no need to target it using root.
For your conditional there is no need for the != test, just the else portion is needed. The first if is enough to determine whether it is true or false... if it ain't true it has to be false....
if(test==true){
} else {
}
Copy link to clipboard
Copied
Hey thanks for your help. I was making some minor mistake in defining instances, I got it now. Though i am still using scenes. I will learn the method that you just mentioned.
By the way, if you could give me some suggestion regarding another query that I have?
When I start my application, it displays a page with a start button.
Upon clicking it, eight buttons are displayed. For my application, I want that everytime I run it, the buttons should randomize that is they should appear at different positions on the page.
Could you please help?
Copy link to clipboard
Copied
Randomizing positions depends on whether you mean using the same positions and assigning them to different buttons each time or just planting them naywhere whether they bump into each other or not.
Copy link to clipboard
Copied
Yes the positions remain the same, its just the buttons which are to move to different positions each time.
How could it be done?
Copy link to clipboard
Copied
You could collect the position information into an array in your loop code and then shuffle the values in that array to randomize them. THen loop thru the buttons array and relocate the buttons using the shuffled position data.
Copy link to clipboard
Copied
Ok, I will try to work on this logic.
Copy link to clipboard
Copied
Hey I am really not getting this logic..I mean i did put the position information into an array.
For X pos i had two values and for Y pos 4 values.
I shuffled these two arrays as well and returned the shuffled value in the same array name.
Now i am not understanding as to how I can loop these through the buttons array to relocate the buttons.
Can you please help?
Copy link to clipboard
Copied
You already have the loop for assigning the event listner to each of your buttons. Use that same loop to assign the position. You are probably better off having just one array with x/y pairs in it rather than 2 arrays. You can store them as objects holding an xValue and a yValue. positionArray.push({xValue: ###, yValue:###})
where the ###'s are the actual position values
Essentially, you need as many elements in the array as you have buttons so that in the loop you are simply assigning...
this["btn"+i].x = positionArray.xValue;
this["btn"+i].x = positionArray.yValue;
Copy link to clipboard
Copied
Hi Ned, I wrote this code and it works absolutely fine but for only one scene
var arr:Array = new Array();
for (var i:int=1; i<=8; i++) {
var btn:Sprite = this["btn"+i] as Sprite
arr.push({x:btn.x, y:btn.y})
}
function arrayShuffle(array_arr:Array):Array{
for(i= 0; i < array_arr.length; i++){
var randomNum_num = Math.floor(Math.random() * array_arr.length)
var arrayIndex = array_arr;
array_arr = array_arr[randomNum_num];
array_arr[randomNum_num] = arrayIndex;
}
return array_arr;
}
trace(arrayShuffle(arr));
for (i=1; i<=8; i++)
{
btn = this["btn"+i] as Sprite
btn.x = arr[i-1].x;
btn.y = arr[i-1].y;
}
As I mentioned there are 8 scenes and I want to put this code in all those scenes. Now, when I do that I get a lot of errors:
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property emphasized through a reference with static type flash.display:Sprite. |
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property enabled through a reference with static type flash.display:Sprite. |
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property label through a reference with static type flash.display:Sprite. |
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property labelPlacement through a reference with static type flash.display:Sprite. |
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property selected through a reference with static type flash.display:Sprite. |
home, Layer 'Layer 1', Frame 2 | 1119: Access of possibly undefined property toggle through a reference with static type flash.display:Sprite. |
Can you please help?
Copy link to clipboard
Copied
I already gave you the best advice back in response 9, first sentence.
Copy link to clipboard
Copied
Yes, I understand that but since I had already started with the scenes and due to less time available with me I went ahead with this. Now, If I start working on just one timeline it may take a long time for me to learn that. And, I will have to redo the entire work.
So, If you could guide me with what is the reason of these errors and how I could eliminate it...it would be really helpful!!
Copy link to clipboard
Copied
Yes, I understand that but since I already told you to not use scenes and you continued with them I would have to take more time and I am simply not here to guide you thru completing your assignment.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now