Bits & Bytes

Posts Tagged ‘actionscript’

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.

Capturing and Playing Video From a Camera in Actionscript 3.0

This program gives a simple demonstration of how to display video that is captured by a camera. If you have a camera attached to your computer (most laptops do), then you can click the box above to display the video capture from your camera. Note that you will need click the “Allow” button to see the video. To stop the video, refresh the page.

The code for this program is simple and is listed below. The first three lines display the message “Click To Play.” The next line, sets the function CaptureVideo() as a callback to respond to mouse clicks.

After that, we have the CaptureVideo() function, which attempts to get access to a camera and checks whether the attempt was successful. If there is a camera, a Video object is created, attached to the camera, and added to the stage for display.

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

stage.addEventListener(MouseEvent.CLICK, CaptureVideo);

function CaptureVideo(e:Event):void {
	var qCamera:Camera = Camera.getCamera();
	if (qCamera != null) {
		var qVideo:Video = new Video(320, 240);
		qVideo.attachCamera(qCamera);
		addChild(qVideo);
	}
}

Generating Sounds in Actionscript 3.0

This program shows how to generate and play a sound. The generated sound is in the form of a sine wave, and it plays whenever the screen is clicked. Left-click the box above to hear the sound play.

The entire code to generate the sound is shown below. The first three lines just output the message “Click To Play.” The next two lines create a Sound object and an integer for the time. The two lines after that set callbacks for generating sound and responding to mouse clicks.

The rest of the program consists of the PlaySound() and Generator() functions. The PlaySound() function initializes the integer that keeps track of the sample time to 0 and then calls play() on the Sound object. The Generator() function writes 8192 samples at a time for the left and right channels; the generated sound is a sine wave that lasts half of a second, since sounds have 44100 samples per second.

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

var qSound:Sound = new Sound();
var iTime:int = 0;

qSound.addEventListener(SampleDataEvent.SAMPLE_DATA, Generator);
stage.addEventListener(MouseEvent.CLICK, PlaySound);

function PlaySound(e:Event):void {
	iTime = 0;
	qSound.play();
}

function Generator(event:SampleDataEvent):void {
	for (var i:int = 0; i < 8192; ++i) {
		// Play for half of a second
		if (iTime >= 22050) {
			return;
		}
		event.data.writeFloat(Math.sin(iTime/25));
		event.data.writeFloat(Math.cos(iTime/25));
		++iTime;
	}
}