Working with Methods in Stack: Next / Previous Items Buttons
Updated: Jan 14, 2008
Views: 595
Description: This tutorial will show you how to use buttons to call methods of the Stack that will the select next and previous items in Stack.
Components Used in this Tutorial: Stack AS3.0 and buttons created within of Flash.
The Completed Project:
Complete Code:
Set-Up and Add Content Using through XML
Before you begin make sure that you have successfully installed the component and have been able to load content into the Stack component using XML. Also make sure that you give your Stack an instance name of myStack.
Also make sure that you have created two buttons or dragged two instances of the Button component (Window > Components > User Interface) onto the stage. Give the button the instance names of btnNextImg, btnPrevImg, btnNextPage, and btnPrevPage.
Plan for ActionScript
We will use a little ActionScript to connect these buttons to the Stack.
As noted in our plan, we create an event listener for when our next item button is clicked. In this event listener we call a function that calls a method of our Stack which selects the next item and a property that makes sure the directions is forward.
Selecting Previous Item in the Stack with a Button
For the previous item, we proceed similar to the next item except reference our other button, call a different function, use the select previous item method, and change the animation direction of our Stack.
// Handle previous image button
function prevImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "OUT";
myStack.selectPreviousItem();
}
btnPrevImg.addEventListener(MouseEvent.CLICK, prevImgClick);
The Completed Project:
Complete Code:
import com.afcomponents.events.StackEvent;
// Handle previous image button
function prevImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "OUT";
myStack.selectPreviousItem();
}
btnPrevImg.addEventListener(MouseEvent.CLICK, prevImgClick);
// Handle next image button
function nextImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "IN";
myStack.selectNextItem();
}
btnNextImg.addEventListener(MouseEvent.CLICK, nextImgClick);
// Handle previous image button
function prevImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "OUT";
myStack.selectPreviousItem();
}
btnPrevImg.addEventListener(MouseEvent.CLICK, prevImgClick);
// Handle next image button
function nextImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "IN";
myStack.selectNextItem();
}
btnNextImg.addEventListener(MouseEvent.CLICK, nextImgClick);
Set-Up and Add Content Using through XML
Before you begin make sure that you have successfully installed the component and have been able to load content into the Stack component using XML. Also make sure that you give your Stack an instance name of myStack.
Also make sure that you have created two buttons or dragged two instances of the Button component (Window > Components > User Interface) onto the stage. Give the button the instance names of btnNextImg, btnPrevImg, btnNextPage, and btnPrevPage.
Plan for ActionScript
We will use a little ActionScript to connect these buttons to the Stack.
- First we want to add a mouse event listener for when our next image button is clicked and then in function that is called reference a method of our Stack component which selects the next item and a property that makes sure the animation direction is forward.
- Likewise, we want to add a mouse event listener for previous image button is clicked and reference a method that will select our previous item in our Stack component and a property that reverses the direction so that items come backwards.
As noted in our plan, we create an event listener for when our next item button is clicked. In this event listener we call a function that calls a method of our Stack which selects the next item and a property that makes sure the directions is forward.
import com.afcomponents.events.StackEvent;
// Handle next image button
function nextImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "IN";
myStack.selectNextItem();
}
// Handle next image button
function nextImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "IN";
myStack.selectNextItem();
}
Selecting Previous Item in the Stack with a Button
For the previous item, we proceed similar to the next item except reference our other button, call a different function, use the select previous item method, and change the animation direction of our Stack.
// Handle previous image button
function prevImgClick(event:MouseEvent):void {
myStack.stackStyle.direction = "OUT";
myStack.selectPreviousItem();
}
btnPrevImg.addEventListener(MouseEvent.CLICK, prevImgClick);
Other Tutorials
