Thursday, April 14, 2011

Randomize Splashing Effect

After put in flash effect, next step is to randomize the splashing effect around motion detection. I've found some code online for the randomize array thing in AS3. I decided to make a tutorial first before import into main splashing effect flash code. Here is the code for randomize array I've found online:


//Create an empty Array with the name movieArray
var movieArray:Array = new Array();

//Put the MovieClips into the Array
//The size of this Array is now 3
movieArray = ["Tree", "Sun", "FootBall"];

// Declare variable to use later
var myMovieClip:MovieClip;
if ((movieArray[Math.floor(Math.random() * 3)]) == "Tree") {
// Create a new MovieClip
myMovieClip = new Tree();
output_txt.text = "The Tree MovieClip was added!";
} else if ((movieArray[Math.floor(Math.random() * 3)]) == "Sun") {
// Create a new MovieClip
myMovieClip = new Sun();
output_txt.text = "The Sun MovieClip was added!";
} else if ((movieArray[Math.floor(Math.random() * 3)]) == "FootBall") {
// Create a new MovieClip
myMovieClip = new FootBall();
output_txt.text = "The FootBall MovieClip was added!";
} else { // In case the random number is 1
myMovieClip = new FootBall();
output_txt.text = "The FootBall MovieClip was added!";
}

// Add the new MovieClip to the empty MovieClip
// so that we can see it.
emptyMC_mc.addChild(myMovieClip);

// Set the location if required
//emptyMC_mc.x = 250;
//emptyMC_mc.y = 210;


And below is the tutorial I've done after modify the code:


import flash.ui.Keyboard;
import flash.events.KeyboardEvent;

//Create an empty Array with the name movieArray
var Splash:Array = new Array();

//Put the MovieClips into the Array
//The size of this Array is now 3
Splash=["sp1","sp2","sp3"];

// Declare variable to use later
var myMovieClip:MovieClip;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(event : KeyboardEvent):void {
if (event.keyCode==Keyboard.SPACE) {
if ((Splash[Math.floor(Math.random() * 3)]) == "sp1") {
// Create a new MovieClip
myMovieClip = new sp1();
} else if ((Splash[Math.floor(Math.random() * 3)]) == "sp2") {
// Create a new MovieClip
myMovieClip = new sp2();
} else if ((Splash[Math.floor(Math.random() * 3)]) == "sp3") {
// Create a new MovieClip
myMovieClip = new sp3();
} else {// In case the random number is 1
myMovieClip = new sp1();
}
// Add the new MovieClip to the empty MovieClip
// so that we can see it.
emptyMC_mc.addChild(myMovieClip);
emptyMC_mc.x = Math.round(Math.random() * stage.width);
emptyMC_mc.y = Math.round(Math.random() * stage.height);
}
}


I've success to make the splash come out but it call out all 3 movie clip and the only random is the sequence in calling different movie clip. After I try to put into the main code it shows a lot of error. I've tried few days in correcting the code but it still show no improvement. I decided to consult lecturer after that. Below is the output for tutorial.



Reference:
1. http://www.republicofcode.com/tutorials/flash/as3arrays/
2. http://www.kirupa.com/forum/showthread.php?t=308712
3. http://www.flashessential.com/archives/60
4. http://www.flashwonderland.com/load-library-movieclip/load-mc-3.html

No comments:

Post a Comment