Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Picking up sound fragment

New Here ,
May 16, 2013 May 16, 2013

In this applincation I am creating a music game where the player has to control the character by keyboard, When the player lands on the dedicated sqaure a clip of a song will play, The player has to press space to collect the soundfile and check if it plays in the correct order.

The problem is, I am not too sure on how to create the coding to collect the song fragment, Change the sqaure back to normal and have the sound clip to the side of the grid so that the player can hear it and put it in the right order. The code that I highlight in red is what I am trying to do.

Code:

"import flash.events.MouseEvent;
import flash.events.KeyboardEvent;

// ========= GLOBAL VARIABLES ===================================
// NEED A GRID TO DISPLAY THINGS...
const COLUMNS = 11; // odd numbers so there IS a centre cell
const ROWS = 9;

var displayMap:Array = new Array(ROWS);  // WHAT WE SEE
var musicMap: Array = new Array(ROWS);   // where we put stuff...
var robot_mc:Robot;
var headphone_mc:Headphone;
//var collectedSounds: Array = [0,1,2,3,4,5,6,7,8,9];

var songs:Array = [["picnic_cut",5],["arthur_cut",4],["failyodd_cut",4],["spiderman_cut",4], ["spongebob_cut",9]]; //part-names of song files
var sounds:Array;  // used for the song fragments
var songTitle;  // so we know what the song is that's playing
var segments;   // how many segments will it have?

// first time load... initialise these arrays==================================================
sounds = new Array();
var randSong = randInt(0,songs.length-1); //choose a random song. Pointless here cos there's only one song, but…
songTitle = songs[randSong][0]; //songs is an array of pairs… first elt is filename
segments = songs[randSong][1]; // second elt is number of segments
for (var k = 1; k <= segments; k++) {
var snd:Sound = new Sound(new URLRequest(songTitle+k+".mp3")); // loads each song fragment
sounds.push(snd); // saves it on the sounds array...NB: sounds[0] = songTitle1.mp3, sounds[1] = songTitle2.mp3
}

// now for the display==============
for (var row = 0; row < ROWS; row++) {
displayMap[row] = new Array();
musicMap[row] = new Array();
for (k = 0; k < COLUMNS; k++) {
musicMap[row] = 0; // put 0's in all of the music map cells... empty for now
displayMap[row] = new Cell();
addChild(displayMap[row]); // will stop on Frame1 by default...
with (displayMap[row]){
scaleX= scaleY = 0.75 // Cell is a bit big from an earlier design decision. Make it smaller...
x = k * 30 + 230;
y = row * 30 + 30;
}
// so each Cell knows its position in the grid
displayMap[row].row =row 
displayMap[row].col=k
// test purposes, can click on a cell to play sound behind...
displayMap[row].addEventListener(MouseEvent.CLICK, playSound);
}
}
musicMap[0][0] = -2;// for the robot (can't put a bit of the song in top-left)

//=== now add the robot ====
robot_mc = new Robot();
addChild(robot_mc);
with(robot_mc){
scaleX= scaleY = 0.75;
x = displayMap[0][0].x;
y = displayMap[0][0].y;
}
robot_mc.row = 0; // robot knows where it is too...
robot_mc.col = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdHandler);

//headphone_mc = new Headphone();
//addChild(headphone_mc);
//with(headphone_mc){
// scaleX = scaleY = 0.70;
// x = displayMap [0] [4].x;
// y = displayMap [4] [0].y
//}


// place song fragments==========================
// familiar randomisation techniques:
for ( k = 1; k <= segments; k++) {
var randRow;
var randCol;
do {
randRow = randInt(0,ROWS-1);
randCol = randInt(0,COLUMNS-1);
} while ( ! spaceFree(randRow, randCol));
// after the while loop, randRow and randCol will give coordinates of an empty space in the array
musicMap[randRow][randCol] = k;  // update music map with the number of the song fragment
displayMap[randRow][randCol].gotoAndStop(2);

// shows that there is a song fragment there.
}


// ================= Function Definitions ============================================================

// playing sounds==================================
// currentTarget is a cell in the grid. It KNOWS which row and col its in because we have set those up as properties of the cell when we set up the displayMap
function playSound(mEvt) {
with (mEvt.currentTarget) {
playSegment(row, col);  // row & col of currentTarget
}
}// end function playSound __________________________________________

function playSegment(row, col) {
SoundMixer.stopAll();
var temp = musicMap[row][col];
if (temp > 0) {  // temp is the number of the songfragment, or 0 (empty) or -1 (original robot position)
sounds[temp - 1].play(); // REMEMBER: sounds[0] = songTitle1.mp3, sounds[1] = songTitle2.mp3
}
} // end function playSegment _______________________________________

// distributing song ================================================
// === RANDOM NUMBER GENERATION ============================
// this version of randInt allows zero values...
// returns random int in the range min <= x <= max
function randInt(min:int, max:int):int {
return Math.round(Math.random() * (max - min)) + min;
}

function spaceFree(row, col):Boolean{
var isFree:Boolean = (musicMap[row][col] == 0);
if (isFree){
for (var ro = row-1; ro <= row+1; ro++){
if(ro >=0 && ro <ROWS){
for (var co = col-1; co <= col+1; co++){  
isFree = isFree && (musicMap[ro][co] == 0|| musicMap[ro][co] == -1)  // -1 is the robot...
}
}
}
}
return isFree
} // end function spaceFree _______________________________________


// KEYBOARD HANDLING & MOVING ROBOT ====================================================

var arrowKeys:Array = [Keyboard.UP,Keyboard.DOWN,Keyboard.LEFT,Keyboard.RIGHT];
function kdHandler(kEvt:KeyboardEvent) {
var temp = kEvt.keyCode;
if (arrowKeys.indexOf(temp) >= 0) {
mvRobot(temp);
}
else if (temp == Keyboard.SPACE) {
pickUpFragment(temp);
}
}  //end function kdHandler _______________________________________


stage.addEventListener(KeyboardEvent.KEY_DOWN, pickUpFragment);
function pickUpFragment(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){


}
}

function mvRobot(mvDirection) {
var tempDir = 0;
switch (mvDirection) {
case Keyboard.LEFT :  //37
case Keyboard.RIGHT :  //39
tempDir = mvDirection - 38;
if (musicMap[robot_mc.row][robot_mc.col + tempDir ] >= -1) {
robot_mc.col += tempDir ;
robot_mc.x += tempDir * 30;
}
break;
case Keyboard.UP :  //38
case Keyboard.DOWN : //40
tempDir = mvDirection - 39;
if (robot_mc.row + tempDir >= 0 && robot_mc.row + tempDir < ROWS) {
if (musicMap[robot_mc.row + tempDir][robot_mc.col ] >= -2) {
robot_mc.row += tempDir ;
robot_mc.y += tempDir * 30;
}
}
break;
}
// robot has moved... is it on a music fragment? if so, play a sound, else stop all sounds...
if(musicMap[robot_mc.row][robot_mc.col ] > 0){
playSegment(robot_mc.row, robot_mc.col )
}
else{
SoundMixer.stopAll();
}
}

// to collect the sound fragments


// Buttons to play a full preview of the song
//================================================================
var myArthur: arthur = new arthur();

play1.addEventListener(MouseEvent.CLICK, playArthur);

function playArthur(ev:MouseEvent):void{
myArthur.play();
}

//====================================================================
var myFailyodd: failyodd = new failyodd();

play2.addEventListener(MouseEvent.CLICK, playFailyodd);

function playFailyodd(ev:MouseEvent):void{
myFailyodd.play();
}

//==============================================================
var myPicnic: picnic = new picnic();

play3.addEventListener(MouseEvent.CLICK, playPicnic);

function playPicnic(ev:MouseEvent):void{
myPicnic.play();
}


//====================================================
var mySpiderman: spiderman = new spiderman();

play4.addEventListener(MouseEvent.CLICK, playSpiderman);

function playSpiderman(ev:MouseEvent):void{
mySpiderman.play();
}

//=================================================================
var mySpongebob: spongebob = new spongebob();

play5.addEventListener(MouseEvent.CLICK, playSpongebob);

function playSpongebob(ev:MouseEvent):void{
mySpongebob.play();
}
//end of preview song button


// STOP SONG BUTTON
stopBtn.addEventListener(MouseEvent.CLICK, stopPlaying);
function stopPlaying(evt) {
SoundMixer.stopAll();
}


//start of newSong_btn

newSong_btn.addEventListener(MouseEvent.CLICK, newSong);


function newSong(ev:MouseEvent):void  {
SoundMixer.stopAll();

//CHOOSE NEW SONG
sounds = new Array();
var randSong = randInt(0,songs.length-1);
songTitle = songs[randSong][0];
segments = songs[randSong][1];
for (var k = 1; k <= segments; k++) {
var snd:Sound = new Sound(new URLRequest(songTitle+k+".mp3"));
sounds.push(snd);
}


for (var row = 0; row < ROWS; row++) {

for (k = 0; k < COLUMNS; k++) {
musicMap[row] = 0;
displayMap[row].gotoAndStop(1);
}
}
musicMap[0][0] = -1;


with(robot_mc){

x = displayMap[0][0].x;
y = displayMap[0][0].y;
}
robot_mc.row = 0;
robot_mc.col = 0;

for ( k = 1; k <= segments; k++) {
var randRow;
var randCol;
do {
randRow = randInt(0,ROWS-1);
randCol = randInt(0,COLUMNS-1);
} while ( ! spaceFree(randRow, randCol));

musicMap[randRow][randCol] = k; 
displayMap[randRow][randCol].gotoAndStop(2);
}
}"

TOPICS
ActionScript
589
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013

what do you mean by, "..collect the soundfile"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 16, 2013 May 16, 2013

As in collect the coloured sqaure that contains a sound, When collected it will have part of the song that the user has to listen to and try place the fragments in the correct order.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013
LATEST

that's not clear to me but, i assume you mean a colored square will be placed in some location on-stage and/or added to another movieclip.

if so, use addChild if the square needs to be reparented and assign its x,y if it needs to be (re)positioned.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines