AS3 random array generator

Posted in: Code, General

A snippet of actionscript 3 code to create random arrays.
The allowTwins boolean, when set to false, will prevent multiple occurrences of a number in your array.

internal function createRandomArray (newArray:Array, nrElements:int, scopeRandom:int, allowTwins:Boolean) {
			newArray = new Array();
			createNr ();
			function createNr () {
				while (newArray.length < nrElements) {
					var i:int = Math.floor(Math.random() * scopeRandom);
					if (!allowTwins) {
						checkNr (i);
					} else {
						newArray.push (i);
					}
				}
			}
			function checkNr (mynum:int) {
				if (newArray.indexOf(mynum) < 0) {
					newArray.push (mynum);
				} else {
					createNr ();
				}
			}
			return (newArray);
		}

Flash CS3 AS3 Audio player for playing one sound file

Posted in: Code

I created a basic audio player. It’s purpose is to play one sound file, not as music player but to play instructions for a lesson. It has the play, pause, stop and scrub functionalities.
It all works, but the scrub bar is not draggable, but clickable.

Here’s the source files
Below’s the as3 script with comments included.

//define variables
var clipSnd:Sound = new Sound (new URLRequest("ff2.mp3"));//load sound
var clipCnl:SoundChannel;//create soundchannel
var clipTmr:Timer = new Timer(1000);//create a timer with timerevent per second
var playTime:Number = 37;//the lenght of the mp3, too much fuss to calculate when using only one file
var isPlaying:Boolean = false;
var newPos:Number;
var curPos:Number = 0;
var tmrDuration:Number = 0;

//var nessecary for scrubbar in CS3
//in CS5 use the scaleX option, dunno if this is also possible in CS4
var barTotalWidth = 275;
var barStartWidth = 0;

//define functions
function playMusic(event:MouseEvent):void {
	if (!isPlaying) {
		clipCnl = clipSnd.play(curPos);
		if (clipTmr.running != true) {
			clipTmr.start();
		}
	}
	isPlaying = true;
}

function pauseMusic(event:MouseEvent):void {
	curPos = clipCnl.position;
	clipCnl.stop();
	isPlaying = false;
	clipTmr.stop();
}

function stopMusic(event:MouseEvent):void {
	resetMusic();
}

function scrubMusic(event:MouseEvent):void {
	if (isPlaying) {
		clipCnl.stop();
		tmrDuration = Math.round((event.localX * playTime) / barTotalWidth);
		newPos = tmrDuration * 1000;
		slidermc.width = event.localX;
		clipCnl = clipSnd.play(newPos);
		//trace (event.localX);
	}
}

function updateSlider(event:TimerEvent):void {
	if (tmrDuration < playTime) {
		tmrDuration++;
		slidermc.width = Math.round((tmrDuration * barTotalWidth)/playTime);
		tf.text = String(tmrDuration);
	} else {
		resetMusic();
	}
}

function resetMusic():void {
	clipTmr.stop();
	curPos = 0;
	clipCnl.stop();
	isPlaying = false;
	slidermc.width = barStartWidth;
	tmrDuration = 0;
}

//initialize player
slidermc.width = barStartWidth;
clipCnl = clipSnd.play();
clipTmr.start();
isPlaying = true;

//set eventlisteners
playbtn.addEventListener(MouseEvent.CLICK, playMusic);
pausebtn.addEventListener(MouseEvent.CLICK, pauseMusic);
stopbtn.addEventListener(MouseEvent.CLICK, stopMusic);
scrubbtn.addEventListener(MouseEvent.CLICK, scrubMusic);
clipTmr.addEventListener(TimerEvent.TIMER, updateSlider);

//script by silvith

//on the topic of the length of the sound-file, there are 4 options:
//1. you can add this information to the ID3 tag and access it as mySound.id3.TLEN
//2. you can only use mySound.length when the file is completely loaded,
//	 so you need to check mySound.bytesLoaded against mySound.bytesTotal before using mySound.length
//3. you can import the length through flashvars, i.e. myswf.mp3?sndDur=23
//4. you can do as I did and simply fill in the number of seconds here.
//	 this limits the usage of the audio player to one file.

jquery multiple choice quiz

Posted in: Code

This is the code for a basic multiple choice quiz of which the answers an be either true or false. Consequently, the text output of the feedback can only contain two strings, i.e. “Yes, this is the correct answer” and “Sorry, wrong answer!”, for every question on the same page.

This code goes in the head of the html page:

//a link to your jquery file
<script src="js_jquery.js" type="text/javascript"></script> 

//the start of the quiz script
<script type="text/javascript">

//this function turns out the result in the div named answer, below the question
function displayResult(container, correct, questionId){
       // Change the contents
	container = $(container).next()
	container.css('background-color', correct == true ? 'lightgreen' : 'pink');
	container.css('display','block');
	container.html("<b>QUESTION " + (questionId) + ":</b> " + (correct == true ? "Correct!" : "Wrong!"));
}

//this function passes the relevant div id to displayResult, so displayResult knows which answer div to show
function finalizeAnswer(bttn,c,questionId) {
    displayResult(bttn.parentNode, c, questionId);
}
</script>

And this example of two questions goes into the body of your html page:

<div id="1">
    <div id="question">
    Question 1:<br/>
    What is the square root of 16?<br/>
    <br/>
    <button onClick="finalizeAnswer(this, false, 1)">A. 7</button>
    <button onClick="finalizeAnswer(this, false, 1)">B. 5</button>
    <button onClick="finalizeAnswer(this, true, 1)">C. 4</button>
    <button onClick="finalizeAnswer(this, false, 1)">D. 1</button>
    </div>
    <div id="answer" style="display:none">Answer</div>
</div>

<div id="2">
    <div id="question">
    Question 2:<br/>
    What is the square root of 16?<br/>
    <br/>
    <button onClick="finalizeAnswer(this, false, 2)">A. 7</button>
    <button onClick="finalizeAnswer(this, false, 2)">B. 5</button>
    <button onClick="finalizeAnswer(this, true, 2)">C. 4</button>
    <button onClick="finalizeAnswer(this, false, 2)">D. 1</button>
    </div>
    <div id="answer" style="display:none">Answer</div>
</div>

You can style the questions and answers to match your layout.
I borrowed the essence of this quiz from this answer by Druttka on stackoverflow.