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);
		}

Help, mijn toetsenbord geeft verkeerde tekens weer

Posted in: General, PC help

Je wilt een ? typen maar je ziet een = verschijnen. Of je wil iets (tussen haakjes) zetten, en plotseling staat het )zo.

Wees niet bang. Je toetsenbord is niet kapot. Wat je gedaan hebt is per ongeluk de taalinstellingen van je computer veranderen.
De snelste manier om de originele instellingen terug te zetten, is door de toetsen CTRL en SHIFT tegelijk in te drukken.
Je vindt deze toetsen vlak boven elkaar aan de linkerkant van je toetsenbord. Soms is de SHIFT alleen een pijl naarboven.

Probleem opgelost!

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.

Things

Posted in: General

It’s been a long time, as these things go. I don’t even like blogging :)
I’m on facebook now: www.facebook.com/silvith

It’s slightly regularly updated ^^

Who?

Posted in: General

There is the rumour Johnny Depp will play the Doctor in a 2012 movie, written by Russel Davies.

Aside from the fact that this is unlikely, I dread the thought that Johnny Depp will be Dr. Who. He’s a fine actor but this increases the chance for Dr. Who daddy issues with 200%.

Also a quote from Davies is “and the long-time fans will not be disappointed because yes, the Daleks make an appearance”. Yes, because that was the only reason old fans watched the show. What the hell? The “new Daleks” look like senseo machines and they are dragged into stories even when they don’t have much to do there. The whole Dalek thing was the only bad thing about the last episode of the latest season.

NO MORE DALEKS! For heaven’s sake, try to invent an original enemy race if you must have one.

Who?

Posted in: General

I apologize for the title of this post.

However, after the finale of this season of Dr. Who I can only say that Matt Smith does an awesome job. I like him just as much as David Tennant, and I hadn’t thought that possible ^_^

As for storylines, I liked this season best of all I have seen so far. I hope next year will be just as great.

And bow ties are cool (when you’re the Doctor)

No Tomorrow

Posted in: General

The title of my websites so far has consistently been No Tomorrow. From version 1.0, its layout a table, its background lime green and its text in comic sans ms, red and bold, to version 8.0, built with php and css. No Tomorrow.

It’s not that I didn’t believe there would be a tomorrow. The title was derived from a line in a song by The Offspring:

Our generation sees the world
Not the same as before
We might as well just throw it all
And live like there‘s no tomorrow
There‘s no tomorrow
We are the ones
Who are living under the gun every day
You might be gone before you know
So live like there‘s no tomorrow
Ain‘t gonna waste this life
There‘s no tomorrow – you ain‘t gonna live it for me
Believe it
The official view of the world has changed
In a whole new way
Live fast cause if you don‘t take it
You‘ll never make it
So if you understand me
And if you feel the same
Then you will know what nitro means
You‘ll live like there‘s no tomorrow – ain‘t gonna waste this life
There‘s no tomorrow – you ain‘t gonna live it for me
There‘s no tomorrow

You can listen to it here , it’s called Nitro [Youth Energy]

I think the title also came from a song I used to listen to all the time, Ancient Dreams by Candlemass …there’s no tomorrow, just sadness and sorrow, hold on to the ancient dreams…
Both completely different songs – one pointing to the now and one trying to escape from it.

Somewhere along the line No Tomorrow became the name of a student clan I wrote a story about and it got its place in that story, so I stopped using it. But I might adopt it again.

Talk about a post that has no relevancy, no meaning:s This is what’s wrong with the internet.

World of Warcraft

Posted in: General

World of Warcraft
- or the day I dropped off the planet again

It’s a boy!

Posted in: General

I’ve been busy the past months. With the greatest creative achievement of my life so far. May 15th it went live ^____^

He’s so sweet, my lovely baby boy. And such a miracle! He has ten toes, ten fingers and the most detailed little ears. I’d never imagined how a baby is only a future human being, how blank and abstract they are when they’re born. I’d never imagined how easy it is to love them, instantly and completely.

But I’ll stop here, before I dissolve in motherly pride. He’s the greatest and I hope I can give him all he needs to become a happy asset to humankind. Or possibly a Time Lord, that would be awesome too ^^