------------------------------------------------------------------------------------------------------------------------
COPY AND PASTE CLASS FILE CODE: MainTimeline.as
------------------------------------------------------------------------------------------------------------------------
package{
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.display.*;
import flash.events.*;
dynamic public class MainTimeline extends MovieClip{
public var square1:r;
public var square2:r;
public var square3:r;
public var circle1:c;
public var square4:r;
public var circle2:c;
public var circle3:c;
public var circle4:c;
public var current:int;
public var fadeIn:Tween;
public var fadeOut:Tween;
public var thisCircle:MovieClip;
public var thisSquare:MovieClip;
public var circles:Array;
public var squares:Array;
public var i:Number;
public function MainTimeline(){
addFrameScript(0, this.frame1);
return;
}
public function doFadeIn(event:MouseEvent) : void{
if (this.current == event.currentTarget.id){
return;
}
this.fadeOut = new Tween(this.squares[this.current], "alpha", None.easeNone, 1, 0, 2.5, true);
this.fadeIn = new Tween(this.squares[event.currentTarget.id], "alpha", None.easeNone, 0, 1, 2.5, true);
this.current = event.currentTarget.id;
return;
}
function frame1(){
this.current = 0;
this.circles = new Array(this.circle1, this.circle2, this.circle3, this.circle4);
this.squares = new Array(this.square1, this.square2, this.square3, this.square4);
this.i = 0;
while (this.i < this.circles.length){
this.thisCircle = this.circles[this.i];
this.thisCircle.buttonMode = true;
this.thisCircle.id = this.i;
this.thisCircle.addEventListener(MouseEvent.CLICK, this.doFadeIn);
if (this.i != this.current){
this.thisSquare = this.squares[this.i];
this.thisSquare.alpha = 0;
}
var ve1:* = this;
var ve2:* = this.i + 1;
ve1.i = ve2;
}
return
}}}
-------------------------------------------------------------------------------------------------------------------
http://stackoverflow.com/questions/9665364/as3-array-of-movieclip-buttons-with-fluid-animation-and-at-state
--------------------------------------------------------------------------------------------------------------------
import flash.events.MouseEvent;
import flash.events.Event;
var clipArray:Array = [btn_1,btn_2]; // Movieclip's called btn_1 etc...
var destArray:Array = ["page_1","page_2"]; Labels on timeline...
for (var i:int = 0; i < clipArray.length; i++) {
clipArray[i].buttonMode = true; // Define Button from Movie Clip
clipArray[i].useHandCursor = true; // Enable HandCursor over clip
clipArray[i].mouseChildren = false; // Define clip as single denomination
clipArray[i].addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
clipArray[i].addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
clipArray[i].addEventListener(Event.ENTER_FRAME, frameHandler);
clipArray[i].addEventListener(MouseEvent.CLICK,clickHandler, false, 0, true);
}
function clickHandler(event:MouseEvent):void {
for (var i:int = 0; i < clipArray.length; i++) {
if (event.currentTarget == clipArray[i]) {
this.gotoAndStop(destArray[i]);
clipArray[i].mouseEnabled = false;
clipArray[i].useHandCursor = false;
clipArray[i].alpha = 0.5;
} else {
clipArray[i].mouseEnabled = true;
clipArray[i].useHandCursor = true;
clipArray[i].alpha = 1;
}
}
}
function mouseOverHandler(e:MouseEvent){
e.target.onOff = true;
}
function mouseOutHandler(e:MouseEvent){
e.target.onOff = false;
}
function frameHandler(e:Event){
if(e.target.onOff){
e.target.nextFrame();
} else {
e.target.prevFrame();
}
}
-----------------------------------------------------------------------------------------------------------------------
http://coursesweb.net/flash/actionscript-change-movieclip-color_t
-----------------------------------------------------------------------------------------------------------------------
// an Array with the instances of the buttons
var btts:Array = [b_red, b_green, b_yellow, b_blue];
// sets an object with colors for each button
var set_colors:Object = {'b_red':0xff0000, 'b_green':0x00ff00, 'b_yellow':0xffff00, 'b_blue':0x0000ff};
// sets a ColorTransform object
var obj_color:ColorTransform = new ColorTransform();
// traverse the "btts" array with button instances
for(var i:int=0; i<btts.length; i++) {
// set the color to each button
obj_color.color = set_colors[btts[i].name];
btts[i].transform.colorTransform = obj_color;
// register CLICK event for each button
btts[i].addEventListener(MouseEvent.CLICK, changeColor);
}
// function called by CLICK events
function changeColor(evt:Event):void
{
// get the instance name of the clicked button
var b_name = evt.target.name;
// set and change the square (sqr) color
obj_color.color = set_colors[b_name];
sqr.transform.colorTransform = obj_color;
}
---------------------------------------------------------------------------------------------------------------
http://demetri-media.com/FlashTalker/ButtonNavigation.html
---------------------------------------------------------------------------------------------------------------------
var hpage:pagehome = new pagehome();
var p1:page1 = new page1();
var p2:page2 = new page2();
var p3:page3 = new page3();
red.addChild(hpage);
var btnArray:Array = [btn1,btn2,btn3];
for (var i:uint=0; i<btnArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.ROLL_OVER, bRollover);
btnArray[i].addEventListener(MouseEvent.ROLL_OUT, bRollout);
btnArray[i].addEventListener(MouseEvent.CLICK, bCLICK);
btnArray[i].buttonMode=true;
}
var myColor:ColorTransform = new ColorTransform();
myColor.redMultiplier = .5;
var myColor2:ColorTransform = new ColorTransform();
myColor2.redMultiplier = 1;
function bRollover(event:MouseEvent):void {
event.target.transform.colorTransform=myColor;
event.target.scaleY=1.5;
}
function bRollout(event:MouseEvent):void {
event.target.transform.colorTransform=myColor2;
event.target.scaleY=1;
}
function bCLICK(event:MouseEvent):void {
red.removeChildAt(1);
switch (event.target) {
case btn1 :
red.addChild(p1);
break;
case btn2 :
red.addChild(p2);
break;
case btn3 :
red.addChild(p3);
break;
}
for (var i:uint=0; i<btnArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.ROLL_OUT, bRollout);
btnArray[i].transform.colorTransform=myColor2;
btnArray[i].scaleY=1;
}
event.target.removeEventListener(MouseEvent.ROLL_OUT, bRollout);
event.target.transform.colorTransform=myColor;
event.target.scaleY=1.5;
}
----------------------------------------------------------------------------------------------------------------
http://board.flashkit.com/board/showthread.php?824580-simple-show-hide-movieclips
----------------------------------------------------------------------------------------------------------------
var mcArray:Array = new Array(mc1, mc2, mc3);
var btnArray:Array = new Array(btn1, btn2, btn3);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo);
}
function expandInfo(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
for(var i:uint =0; i < mcArray.length; i++){
mcArray[i].visible = false;
}
mcArray[index].visible = true;
mcArray[index].gotoAndPlay(2);
}
----------------------------------------------------------------------------------------------------
OR UPPER CODE WRITE IN THIS WAY
----------------------------------------------------------------------------------------------------
var mcArray:Array = new Array(mc1, mc2);
var btnArray:Array = new Array(btn1, btn2);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo);
}
function expandInfo(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
for(var i:uint =0; i < mcArray.length; i++){
mcArray[index].visible = false;
}
}
-----------------------------------------------------------------------------------------------------
OR WRITE THIS WAY
-----------------------------------------------------------------------------------------------------
var mcArray:Array = new Array(mc1, mc2);
var btnArray:Array = new Array(btn1, btn2);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo);
}
function expandInfo(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
mcArray[index].visible = false;
}
------------------------------------------------------------------------------------------------------------------
OR WRITE ABOVE CODE IN JAVA SCRIPT
------------------------------------------------------------------------------------------------------------------
<button id="mc1">MovieClip1</button>
<button id="mc2">MovieClip2</button>
<button id="btn1">BUTTON1</button>
<button id="btn2">BUTTON2</button>
<script type='text/javascript'>
var mcArray =new Array(mc1,mc2);
var btnArray = new Array(btn1,btn2);
for (var i = 0; i < mcArray.length; i++){
btnArray[i].addEventListener("click",expandInfo,true);
function expandInfo(e){
var index = btnArray.indexOf(e.target);
mcArray[index].style.visibility = "hidden";
}
}
</script>
---------------------------------------------------------------------------------------------------------------------
OR WRITE THIS WAY FOR DRAG
---------------------------------------------------------------------------------------------------------------------
var mcArray:Array = new Array(mc1, mc2);
var btnArray:Array = new Array(btn1, btn2);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, DragMe);
}
function DragMe(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
mcArray[index].startDrag();
}
-----------------------------------------------------------------------------------------------------------------
OR USE THIS METHOD
http://www.ilike2flash.com/2011/04/drag-and-drop-in-as3-part-3.html
-----------------------------------------------------------------------------------------------------------------
//Array to hold the target instances, the drop instances,
//and the start positions of the drop instances.
var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3);
var dropArray:Array = new Array(drop1,drop2,drop3);
var positionsArray:Array = new Array();
//This adds the mouse down and up listener to the drop instances
//and add the starting x and y positions of the drop instances
//into the array.
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}
//This drags the object that has been selected and moves it
//to the top of the display list. This means you can't drag
//this object underneath anything.
function mdown(e:MouseEvent):void {
e.currentTarget.startDrag();
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
//This stops the dragging of the selected object when the mouse is
//released. If the object is dropped on the corresponding target
//then it get set to the x and y position of the target. Otherwise
//it returns to the original position.
function mUp(e:MouseEvent):void {
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
target.stopDrag();
if (target.hitTestObject(hitArray[dropIndex])) {
target.x = hitArray[dropIndex].x;
target.y = hitArray[dropIndex].y;
}else{
target.x = positionsArray[dropIndex].xPos;
target.y = positionsArray[dropIndex].yPos;
}
}
Here are some extras to spice up this game. I have created a button to reset the drop objects when they are all on the respective targets.
reset.addEventListener(MouseEvent.CLICK, backObjects);
function backObjects(e:MouseEvent):void{
for(var i:int = 0; i < dropArray.length; i++){
if(dropArray[i].x == hitArray[i].x && dropArray[i].y == hitArray[i].y){
dropArray[i].x = positionsArray[i].xPos;
dropArray[i].y = positionsArray[i].yPos;
}
}
}
I have also added a scream sound effect when the drag object is on the correct target. You will need to import a sound effect into the library then right click on the audio file and select properties, check the export to Actionscript button and give it a class name. For more information on sounds in Actionscript 3, checkout some of my audio tutorials.
function playSound(SoundName:Class):void{
var sound = new SoundName();
var channel:SoundChannel = sound.play();
}
To play the sound effect, add the following line of code inside the If statement of the mUp function.
playSound(TheNameOfTheClass);
-------------------------------------------------------------------------------------------------------------
http://www.flashhilfe.de/forum/actionscript-3-0/movieclip-initialisieren-fuer-fadeuebergang-283954-283954.html
-----------------------------------------------------------------------------------------------------------------
var btnArray:Array = [this.btn_1,this.btn_2,this.btn_3];
var mcArray:Array =[this.mc_1,this.mc_2,this.mc_3];
var mem_mc:MovieClip = null;
for (var i:int=0; i<btnArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, funky(i));
}
function funky(ix:int):Function {
return function(e:MouseEvent):void {
if(mem_mc != null){
mem_mc.alpha = 1;
}
mcArray[ix].alpha = 0.5;
mem_mc = mcArray[ix];
}
}
-----------------------------------------------------------------------------------------------------------
http://www.actionscript.org/forums/archive/index.php3/t-194517.html
------------------------------------------------------------------------------------------------------------
This is actually a very good question. I have wondered about it myself. There's no natural association between a button and a MovieClip, so you have to make one somehow. One solution I've used is parallel arrays. If there are five buttons and five clips, here's how that might go:
var buttonArray:Array = [btn1, btn2, btn3, btn4, btn5];
var clipArray:Array = [mc1, mc2, mc3, mc4, mc5];
for(var i:int = 0; i < buttonArray.length; i++) {
buttonArray[i].addEventListener(MouseEvent.CLICK, clickHandler);
}
function clickHandler(event:MouseEvent):void {
for(var i:int = 0; i < buttonArray.length; i++) {
if(event.currentTarget == buttonArray[i]) {
clipArray[i].gotoAndPlay(2);
break;
}
}
}
------------------------------------------------------------------------------------------------------------------------
http://www.actionscript.org/forums/archive/index.php3/t-156401.html
--------------------------------------------------------------------------------------------------------------------------
Finally, in the clickHandler function, you can tell which button was clicked on with event.target. So each button can control the corresponding mc using event.target.theIndex.
var btns:Array = new Array(b1, b2, b3);
var mcs:Array = new Array(m1, m2, m3);
for(var i:int = 0; i < btns.length; i++) {
btns[i].addEventListener(MouseEvent.CLICK, clickHandler);
btns[i].theIndex = i;
}
function clickHandler(event:MouseEvent) {
mcs[event.target.theIndex].alpha = .5;
}
---------------------------------------------------------------------------------------------------------------------
OR USE WITH ENTER FRAME FUNCTION
http://gotoanswer.stanford.edu/?q=addEventListener+%26+Array+of+Buttons
-------------------------------------------------------------------------------------------------------------------
import flash.events.Event;
import flash.events.MouseEvent;
// put buttons in array
var buttons:Array = [btn1,btn2,btn3,btn4,btn5,btn6];
// put corresponding movie clips in another array
var movieclips:Array = [mc1,mc2,mc3,mc4,mc5,mc6];
// begin listening each of frames
addEventListener(Event.ENTER_FRAME, oef);
function oef(event:Event):void
{
// loop through buttons
for each(var btn:Button in buttons)
{
// test if button exist and if it doesn't have mouse listener
// for the click event yet
if(btn != null && !btn.hasEventListener(MouseEvent.CLICK))
{
// add click mouse event listener for the button
btn.addEventListener(MouseEvent.CLICK, omc);
}
}
}
function omc(event:MouseEvent):void
{
// take corresponding movieclip out of the movies array
// related the index number of the button in buttons array
var mc:MovieClip = movieclips[buttons.indexOf((event.target as Button))];
// start playing corresponding movieclip from frame 1;
mc.gotoAndPlay(1);
}
---------------------------------------------------------------------------------------------------------------
BUTTON MOVIE CLIP ARRAY IN AS3
https://www.youtube.com/watch?v=pqNQLKZa5Jc
--------------------------------------------------------------------------------------------------------------
var score:Number = 0;
var mcArray:Array = new Array(mc1, mc2);
var btnArray:Array = new Array(btn1, btn2);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo);
}
function expandInfo(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
mcArray[index].visible = false;
score += 1;
scorecounter.text = " SCORE:"+score.toString()
if(score==2){
this.nextFrame();
}
}
----------------------------------------------------------------------------------------------------------------
BUTTON AND MOVIE CLIP ARRAY AS3
-------------------------------------------------------------------------------------------------------------------
TWO ARRAY FUNCTION AS3 http://flashcollege.blogspot.co.uk/2015/02/two-array-function-as3.html ================================================================= var mcArray:Array = new Array(mc1, mc2); var btnArray:Array = new Array(btn1, btn2); for(var i:uint = 0; i < mcArray.length; i++) { btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo); } function expandInfo(e:MouseEvent):void { var index:uint = btnArray.indexOf(e.target); mcArray[index].visible = false; }
----------------------------------------------------------------------------------------------------------------
BUTTON AND MOVIE CLIP ARRAY AS3
https://www.youtube.com/watch?v=pqNQLKZa5Jc
-------------------------------------------------------------------------------------------------------------------
TWO ARRAY FUNCTION AS3 http://flashcollege.blogspot.co.uk/2015/02/two-array-function-as3.html ================================================================= var mcArray:Array = new Array(mc1, mc2); var btnArray:Array = new Array(btn1, btn2); for(var i:uint = 0; i < mcArray.length; i++) { btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo); } function expandInfo(e:MouseEvent):void { var index:uint = btnArray.indexOf(e.target); mcArray[index].visible = false; }
BUTTON AND MOVIE CLIP ARRAY AS3 WITH SCORE AND NEXT FRAME
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
var score:Number = 0;
var mcArray:Array = new Array(mc1, mc2);
var btnArray:Array = new Array(btn1, btn2);
for(var i:uint = 0; i < mcArray.length; i++) {
btnArray[i].addEventListener(MouseEvent.CLICK, expandInfo);
}
function expandInfo(e:MouseEvent):void {
var index:uint = btnArray.indexOf(e.target);
mcArray[index].visible = false;
score += 1;
scorecounter.text = " SCORE:"+score.toString()
if(score==2){
this.nextFrame();
}
}
EmoticonEmoticon