Saturday, 17 January 2015

ARRAY FUNCTION AS2 AS3




------------------------------------------------------------------------------------------------------------
MOVIE DRAG AND DROP  WITH ARRAY  FUNCTION AS2
CRATE TEXT BOX  WITH MOVIE1 MOVIE2 MOVIE3 AND CHANGE INTO BITMAP
AFTER ALL BITMAP IMAGS CHANGE INTO MOVIE CLIPS AND GIVE
INSTANCE NAME MOVIE1,MOVIE2,MOVIE3
https://tv.adobe.com/watch/actionscript-11-with-doug-winnie/use-arrays-to-access-objects-on-the-stage-episode-36
https://www.youtube.com/watch?v=zQNQcqfFJuI
https://www.youtube.com/watch?v=DpUoVSOahq0
Download SourceFile
-----------------------------------------------------------------------------------------------------------
// MAKE FOUR MOVIE SYMBOLS WITH INSTANCE NAMES MOVIE1,MOVIE2,MOVIE3,MOVIE4
myItemArray = new Array("MOVIE1", "MOVIE2", "MOVIE3", "MOVIE4");
//MAKE 9 IDENTIFIER E1, E2 AS2
var enemyArray:Array = ["E1","E2","E3","E4","E5","E6","E7","E8","E9"];
https://www.youtube.com/watch?v=mr3lhnBahQk
-----------------------------------------------------------------------------------------------------------

var MovieArray = [MOVIE1,MOVIE2,MOVIE3]
for (i = 0; i < MovieArray.length; i++){
MovieArrayFunction(MovieArray[i]);
function MovieArrayFunction(MovieArray){
MovieArray.onPress = function (){
MovieArray.startDrag();
}
MovieArray.onRelease = function (){
MovieArray.stopDrag();
}}  

------------------------------------------------------------------------------------------------------------
MOVIE DRAG AND DROP  WITH ARRAY  FUNCTION AS3
CRATE TEXT BOX  WITH MOVIE1 MOVIE2 MOVIE3 AND CHANGE INTO BITMAP
AFTER ALL BITMAP IMAGS CHANGE INTO MOVIE CLIPS AND GIVE
INSTANCE NAME MOVIE1,MOVIE2,MOVIE3
https://www.youtube.com/watch?v=zQNQcqfFJuI
https://www.youtube.com/watch?v=DpUoVSOahq0
-----------------------------------------------------------------------------------------------------------

var MovieArray = [MOVIE1,MOVIE2,MOVIE3];
for (var i:int = 0; i < MovieArray.length; i++){
MovieArray[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
MovieArray[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}
function drag(event:MouseEvent):void {
event.currentTarget.startDrag();
}
function drop(event:MouseEvent):void {
event.currentTarget.stopDrag();
}
------------------------------------------------------------------------------------------------------------
TRACE SHUFFLED LETTERS ARRAY IN AS3 TEXT BOX
Download Source:
------------------------------------------------------------------------------------------------------------

var letters:Array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var shuffledLetters:Array = new Array(letters.length)
var randomPos:Number = 0;
for (var i:int = 0; i < shuffledLetters.length; i++){
randomPos = int(Math.random() * letters.length);
shuffledLetters[i] = letters[randomPos];
TraceArray.text = shuffledLetters.toString()
trace(shuffledLetters)}
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
var tempArray:Array = [1,2,3,4,5]
ShuffleArray(tempArray);
trace(tempArray);
function ShuffleArray(input:Array){
for (var i:int = input.length-1; i >=0; i--){
var randomIndex:int = Math.floor(Math.random()*(i+1));
var itemAtIndex:Object = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
}
------------------------------------------------------------------------------------------------------------------
MAKE MAIN TIME LINE 12 INSERT KEY FRAME WITH DIFFERENT IMAGES
PASTE BELOW CODE IN FIRST FRAME
------------------------------------------------------------------------------------------------------------------
var im;
var num;
var frm;
var genum=[];
var k=0;
var myarr=[0,1,2,3,4,5,6,7,8,9];
var noKey = true;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (noKey) {
if (Key.isDown(Key.RIGHT)) {
im=myarr.length;
num=random(im);
frm=myarr[num]+2;
genum.push(frm);
myarr.splice(num,1);
gotoAndStop(frm);
if(im<=1)gotoAndStop(12);
noKey = false;
}}}
keyListener.onKeyUp = function() {
if (!Key.isDown(Key.RIGHT)) {
noKey = true;
}}
Key.addListener(keyListener);
stop();
------------------------------------------------------------------------------------------------------------
SHUFFLE MOVIE ARRAY FUNCTION IN AS3
------------------------------------------------------------------------------------------------------------

var mcArray:Array = [WELCOME00, WELCOME01, WELCOME02, WELCOME03];
var init:Boolean = true;
function addAtRandomPos():void {
for (var i:uint = 0; i < mcArray.length; i++) {
if (init) {
mcArray[i].alpha = 0;
addChild(mcArray[i]);
init = false;
}
mcArray[i].x = Math.floor( Math.random()*(stage.stageWidth - mcArray[i].width) );
mcArray[i].y = Math.floor( Math.random()*(stage.stageHeight - mcArray[i].height) );
Tweener.addTween(mcArray[i], {alpha:1, time:1, onComplete:fadeOut});
}
}
function fadeOut():void {
for (var i:uint = 0; i < mcArray.length; i++) {
Tweener.addTween(mcArray[i], {alpha:0, delay:.2, time:2.8, onComplete:addAtRandomPos});
}
}
addAtRandomPos();
------------------------------------------------------------------------------------------------------------
OR USE THIS CODE SHUFFLE MOVIE ARRAY FUNCTION IN AS3
--------------------------------------------------------------------------------------------------------------

var MovieArray:Array = [MOVIE1,MOVIE2,MOVIE3];
for (var i:uint = 0; i < MovieArray.length; i++) {
MovieArray[i].x = Math.floor( Math.random()*(stage.stageWidth - MovieArray[i].width) );
MovieArray[i].y = Math.floor( Math.random()*(stage.stageHeight - MovieArray[i].height) );
}
function MovieRandomPositon():void {
MovieRandomPositon();
}
----------------------------------------------------------------------------------------------------------------
OR USE THIS CODE SHUFFLE MOVIE ARRAY FUNCTION IN AS2
----------------------------------------------------------------------------------------------------------------

// MAKE FOUR MOVIE SYMBOLS WITH INSTANCE NAMES MOVIE1,MOVIE2,MOVIE3
kCount = 46;
init();
init();
function init(){
myItemArray = new Array("MOVIE1", "MOVIE2", "MOVIE3");
var x = 0;
while (x < kCount){
myItemArray[x] = Math.floor(Math.random() * 9) + myItemArray[x];
++x;
}
myItemArray.sort();
var x = 0;
while (x < kCount){
myItemArray[x] = myItemArray[x].substr(1, myItemArray[x].length);
++x;
}
myXArray = new Array(_root.MOVIE1x, _root.MOVIE2x, _root.MOVIE3x);
myYArray = new Array(_root.MOVIE1y, _root.MOVIE2y, _root.MOVIE3y);
var t = 0;
while (t < kCount){
myElement = eval("_root." + myItemArray[t]);
myElement._x = myXArray[t];
myElement._y = myYArray[t];
myElement._visible = true;
myElement.found = false;
++t;
}
MOVIE1x = _root.MOVIE1._x;
MOVIE1y = _root.MOVIE1._y;
MOVIE2x = _root.MOVIE2._x;
MOVIE2y = _root.MOVIE2._y;
MOVIE3x = _root.MOVIE3._x;
MOVIE3y = _root.MOVIE3._y;
}

--------------------------------------------------------------------------------------------------------------------
WITHOUT INSTANCE NAME MOVIE CLIP ARRAY WITH SET CLASS NAME AS3
--------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE
---------------------------------------------------------------------------------------------------------------------

//MAKE 3 MOVIE CLIPS WITHOUT INSTANCE NAME
//GO TO SYMBOL PROPERTY PANNEL SET CLASS NAME
//CLASS NAME LIKE "Class1","Class2","Class3"
var MC1 = new Class1;
var MC2 = new Class2;
var MC3 = new Class3;
addChild(MC1);
addChild(MC2);
addChild(MC3);
var MovieArray:Array = [MC1,MC2,MC3];
MovieArray[0].x = 120;
MovieArray[0].y = 250;
MovieArray[1].x = 260;
MovieArray[1].y = 250;
MovieArray[2].x = 400;
MovieArray[2].y = 250;
------------------------------------------------------------------------------------------------------------
OR WRITE UPPER CLASS ARRAY CODE AS3 THIS WAY
--------------------------------------------------------------------------------------------------------------

//GO TO SYMBOL PROPERTY PANNEL SET CLASS NAME
//CLASS NAME LIKE "Class1","Class2","Class3"
var MC1 = new Class1;
var MC2 = new Class2;
var MC3 = new Class3;
addChild(MC1);
addChild(MC2);
addChild(MC3);
var MovieArray:Array = [MC1,MC2,MC3];
for (var i:uint = 0; i < MovieArray.length; i++) {
MovieArray[i].x = Math.floor( Math.random()*(stage.stageWidth - MovieArray[i].width) );
MovieArray[i].y = Math.floor( Math.random()*(stage.stageHeight - MovieArray[i].height) );
}

----------------------------------------------------------------------------------------------------------------
ARRAY PLUS METHOD IN AS3
-----------------------------------------------------------------------------------------------------------------

//declare the 2 variables
var A :Array = ["A"];
var B :Array = ["B"];
button1.addEventListener(MouseEvent.CLICK, calculate_fn);
function calculate_fn(e:MouseEvent):void {
boxOne.text=String("A"+"B")
}


--------------------------------------------------------------------------------------------------------
TWO ARRAY FUNCTION
1- NEED BUTTON WITH INSTANCE NAME: b_red, b_greenb_yellowb_blue
2-  NEED MOVIE CLIP SYMBOL WITH INSTANCE NAME: sqr
http://coursesweb.net/flash/actionscript-change-movieclip-color_t
--------------------------------------------------------------------------------------------------------

// an Array with the instances of the buttons
var btts:Array = [b_red, b_greenb_yellowb_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;
}

----------------------------------------------------------------------------------------------------------------

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
http://flashcollege.blogspot.co.uk/2016/03/score-count-button-array-as3.html

-------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

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