I’m fairly new to Flash and have stumbled through it so far with varying levels of success.
Application:
I’m trying to develop a simple drum beat maker for a Uni project. The idea is that the user will select a note and place it onto the stave (the music score lines). On pressing a play button a play line will animate along the staves and on collision with the notes play the appropriate sound.
To view the current progress go here and select Create Mode.
The major problem I am having is that the MovieClip of the playline is only detecting collision with the last note that I place for a particular container. I’m not sure if this is a display object issue or just bad coding. I’ve been tackling Flash and AS3 without much tutor tuition, so the basics and good practices aren’t clear to me.
Below is the code I’ve come up with:
Selecting and adding a note:
//Variables
var greenNote:Sprite;
var container_greenNote:MovieClip = new MovieClip();
//Listening for note selection
floortom.addEventListener(MouseEvent.CLICK, greenNoteSelected);
//Note function
function greenNoteSelected(e:MouseEvent):void{
// The GreenDrumNote is the linkage ID of the actual image for the note
var addGreenNote:GreenDrumNote = new GreenDrumNote(); //Creates child of drum note
//I've add all of the same type of notes to a single container
container_greenNote.addChild(addGreenNote);
addChild(container_greenNote);
//Used for removing containers
numberGreenNotes++ //Increase when a green note is added
//This is could be where the issue is occurring as every note has the same name.
greenNote = Sprite(addGreenNote); //Becomes the refered to sprite for all related notes
//However I have additional code checking for mouseover of the notes and it works for ALL notes placed, so Flash is recognising each note on mouseover, but not for collision between another movieclip
greenNote.name = "Floor Tom";
greenNote.startDrag(true); //Sets child as mouse cursor
Mouse.hide(); //Hides the mouse pointer
stage.addEventListener(MouseEvent.CLICK, placeGreenNote);
}
//Placing note on a single stave
function placeGreenNote(e:MouseEvent):void{
//Checks to see if note is positioned on timeline
if(stave1.hitTestObject(greenNote)){
//Places note on timeline
greenNote.x = stave1.x && stage.mouseX;
greenNote.y = stave1.y;
dropNote=new add_sound();
dropNoteChannel=dropNote.play();
stage.removeEventListener(MouseEvent.CLICK, greenNoteEdit);
stage.removeEventListener(MouseEvent.CLICK, placeGreenNote);
greenNote.stopDrag();
Mouse.show();
//Deleting the note by dragging to trash
}else if(trashcan.hitTestObject(greenNote)){
stage.removeEventListener(MouseEvent.CLICK, placeGreenNote);
container_greenNote.removeChild(MovieClip(e.target));
numberGreenNotes--
Mouse.show();
Deleted=new recycle_sound();
DeletedChannel=Deleted.play();
if (numberGreenNotes == 0){
removeEventListener(Event.ENTER_FRAME,greenNoteDetection);
}
}
greenNote.addEventListener(MouseEvent.MOUSE_DOWN, greenNoteEdit);
greenNote.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
greenNote.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
Play button code. Not so important as it just animated the movieclip and at the moment will start checking for collision
timeline_play.addEventListener(MouseEvent.CLICK, playStart);
function playStart (e:MouseEvent):void{
//The event listener which will start producing my tracing command repeatedly.
addEventListener(Event.ENTER_FRAME, greenNoteDetection);
playline.gotoAndPlay("Speed_1");
}
My attempt at the collision detection code
function greenNoteDetection(e:Event):void {
if (playline.hitTestObject(greenNote)){
trace ("Green Note collision");
} else{
trace ("Off");
}
}
Sorry if this is long winded, but it may help to visit the link and give it a play to see how it works.