Random Stuff :: shuffling array in actionscript 3

Hello everyone.

i am writing this post because i found it very useful and necessary must have function/method for every ActionScript developer, just want to show you how to shuffle an sorted array…

Shuffling an Array in ActionScript3

// Lets make a Array first and fill with some Integers
var myArray:Array = new Array();
for (var i:int = 0; i <10; i++) {
	myArray[i] = i;
}
//Tracing output
trace("My sorted Array: " + myArray);
 
 
var s:Array = shuffle(myArray);
trace("Unsorted array: " + s);
 
// Logics for Shuffle
function shuffle(arr:Array):Array {
	var sArray:Array = new Array();
	var j:int = 0;
	while (myArray.length > 0) {
		var r:int = Math.floor(Math.random()*myArray.length);
		sArray[j] = myArray[r];
		j+=1;
		myArray.splice(r,1);
	}
	//finally return shuffle array.
	return sArray;
}

that’s all folks.

No Comments

Google Weather API and Flash – Part 2

This is multipart tutorial, and this is second part of this tutorial. part 1 of this tutorial is here.

Lets move ahead and try to call google weather API. for this lets first make result component(Text Area) to read-only, because we just wanted to display XML result from google weather API in result Field. here is the code.

package {
	//Because Label are Components we must call him.
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import flash.display.MovieClip;
	public class mainClass extends MovieClip
	{
		private var labelComponent:Label;
		private var cityInputComponent:TextInput;
		private var goButton:Button;
		private var result:TextArea;
		public function mainClass() {
			//calling componentSetup() function in Constructor Function.
			componentSetup();
		}
		private function componentSetup():void {
			//placing components in Stage remember to import fl.controls.Label for Label Control
			// First of all lets place Label and move it x=20 and y=20 px in stage
			labelComponent = new Label();
			labelComponent.text = "City Name";
			labelComponent.move(20, 20);
			addChild(labelComponent);
 
			//Now add TextInput component Remember to Import fl.controls.TextInput
			cityInputComponent = new TextInput();
			cityInputComponent.move(100, 20);
			cityInputComponent.width = 200;
			addChild(cityInputComponent);
 
			//Adding Button Control. Remember to import fl.controls.Button;
			goButton = new Button();
			goButton.move(320, 20);
			goButton.label = "Show Weather";
			addChild(goButton);
			// Now time to add TextArea Component..Remember to import fl.controls.TextArea for TextArea Component
			result = new TextArea();
			result.width = 400;
			result.height = 200;
			result.move(20, 50);
			// lets make result field READ-ONLY
			result.editable = false;
			addChild(result);
		}
	}
}

line 43 -  makes result field read only.

Read the rest of this entry »

, , , ,

1 Comment

Twitter Weekly Updates for 2009-06-28

No Comments

Google Weather api and Flash

Today we will learn how to use google weather api in flash ActionScript3.actually today is very funny day for me because i was stumbling google and Yahoo! for weather api because i wanted to create my own weather gadget in flash for my website. i got very interesting ideas about weather api, and definitely we will learn game development with google api in future.

In this tutorial we will learn how to works with ActionScript3 Components,XML and weather api etc. now the question is what is the relation between Weather API and Game development. it is not easy to answer this question but i will explore the opportunity to relate weather and game in some way soon.

What do we learn in this tutorial?

we will learn how to use components, XML and Google weather api. I am not going to teach you uses of XML in Flash because there are tons of material who covers very well this topic. you can found some good website to learn XML and Flash like kirupa, flash-creation and actionscript.org.

Read the rest of this entry »

, , , ,

3 Comments

Flash game tutorial- Bubble shooter- part 1

In this tutorial we will learn how to create a balloon shooter like game, so i just changed name to Bubble Shooter. this is very basic tutorial particularly helpful if you are moving from ActionScript2 to ActionScript3.

During this tutorial we learn how to organize classes in ActionScript3, how to overcome problem step-by-step and how ActionScript3 makes Flash Game development very easy. Also we will cover the best practices like-

  • Just one frame in source .fla file
  • No ActionScript in Source and/or MovieClips.
  • Organizing multiple .as files
  • Organizing multiple Classes.

Actually this is quite long tutorial so i decided to break it into multiple part. this is the first part of Bubble Shooter Game.

I am using Adobe Flash to create graphics and FlashDevelop to edit ActionScript Because I find FlashDevelop more convenient then any other ActionScript editing software. if you don’t know  more about FlashDevelop than read this article to know more about FlashDevelop. however if you do not prefer FlashDevelop fell free to use any actionscript editing tool.

Lets start tutorial by setting up games.

Read the rest of this entry »

, ,

3 Comments