Bits & Bytes

Posts Tagged ‘file’

How to Stop Losing Saved Flash Games

If you play Flash games with any frequency, you have probably noticed that, after a while, your saved games will disappear. Then you have to start the game from scratch and play through the entire game from the beginning again. Here, we show you how to fix this problem.

Expanding your browser’s cache does not prevent games from being erased. In fact, saved Flash games are stored in a location that is used across browsers. So, you can play the same game and continue your saved progress in any browser. However, you need to follow the steps below to prevent your saved games from being prematurely erased.

First, navigate to a page with a game on it, like this one.

Next, right-click inside the game window to bring up the context menu shown below and then left-click “Settings…” in the context menu.

This opens the Adobe Flash Player Settings dialog shown below. Left-click the “Local Storage” tab so that your settings dialog looks like the one below. Notice that the dialog asks how much storage xoax.net can use. This means that the storage is based on the website that you playing on, but not the game. By default, the value will be something small, like 10 kilobytes. With the default setting, the storage will run out fairly quickly, and you will lose all of your saved games, unless you increase the storage maximum.

To increase the storage, left-click and drag the slider to the right to increase the storage limit. I would recommend setting it to “Unlimited” so that you don’t have to worry about losing your progress. Note that you can see how much space is currently being used too. When you are done, left-click the close button and play some games!

Beware of clearing your browser’s cache. This may cause the Flash games to lose their storage, indirectly.

Signal Processing Sound Data in Actionscript 3.0

This program demonstrates how to perform a signal processing operation on sound data and play it back. If you left-click the box above, you can hear the clip saying “XoaX.net” that I have been using in the other examples played at double speed. Note that if you want to hear the clip played a second time, you need to refresh the page.

The code is shown below. The first three lines output the “Click To Play” message. The next two lines create two Sound objects. The two lines after those set an event listener that is called when the file loads and then call load() to load the mp3 file.

The remaining code consists of three functions. The first function, FinishedLoading(), is called when the file is loaded since it was set as the callback. The FinishedLoading() function sets PlaySound() as a callback function for mouse clicks. Doing this prevents the PlaySound() from being called before the sound file is loaded.

The second function, PlaySound(), sets the DoubleSpeed() function as a callback when data is requested for the destination sound. Then it calls play(), which triggers a series of data requests as the sound plays.

The third function, DoubleSpeed(), calls the extract() function to retrieve data into the source byte array. Inside the while loop, each pair of source samples is averaged and then written into the destination array. Finally, the destination byte array is written back into the SampleDataEvent object.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";
addChild(qInstructions);

var qSrc:Sound		= new Sound();
var qDest:Sound		= new Sound();

qSrc.addEventListener(Event.COMPLETE, FinishedLoading);
qSrc.load(new URLRequest("http://www.xoax.net/public/XoaXDotNet.mp3"));

function FinishedLoading(e:Event):void {
	stage.addEventListener(MouseEvent.CLICK, PlaySound);
}

function PlaySound(e:Event):void {
	qDest.addEventListener(SampleDataEvent.SAMPLE_DATA, DoubleSpeed);
	qDest.play();
}

function DoubleSpeed(e:SampleDataEvent) {
	var qSrcBytes:ByteArray = new ByteArray();
	qSrc.extract(qSrcBytes, 8192);
	var qDestBytes:ByteArray = new ByteArray();
	var dLeft:Number = 0;
	var dRight:Number = 0;
	qSrcBytes.position = 0;
	// Read and average two samples
	while (qSrcBytes.bytesAvailable > 0) {
		dLeft = qSrcBytes.readFloat();
		dRight = qSrcBytes.readFloat();
		if (qSrcBytes.bytesAvailable > 0) {
			dLeft = (dLeft + qSrcBytes.readFloat())/2.0;
			dRight = (dRight + qSrcBytes.readFloat())/2.0;
			qDestBytes.writeFloat(dLeft);
			qDestBytes.writeFloat(dRight);
		}
	}
	e.data.writeBytes(qDestBytes);
}

Loading and Playing a Sound File in Actionscript 3.0

This program loads an mp3 file and plays it. The entire program is given below and the compiled SWF is shown above. To hear the file play, left-click the window above.

The code begins with three lines that tell the user to click the screen. Then we create a String for the URL, which could also be a local file. Next, we set our function as the callback for mouse clicks. Inside the function OnClick(), we create a Sound object, load the sound file, and then play it.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";
addChild(qInstructions);

const ksXoaXDotNet:String = "http://www.xoax.net/public/XoaXDotNet.mp3";

// Play the mp3 when the screen is clicked
stage.addEventListener(MouseEvent.CLICK, OnClick);

function OnClick(e:MouseEvent): void {
	var qSound:Sound = new Sound();
	qSound.load(new URLRequest(ksXoaXDotNet));
	qSound.play();
}