Skip to main content
Inspiring
February 6, 2012
Question

touch event singleton

  • February 6, 2012
  • 1 reply
  • 1162 views

I'm trying to create some kind of soundboard... every touch_begin would suppose to start some sound, modulate it on touch_move, and stop it on touch_end... well so far, i haven't include sound properties because I can't get ridd of all touches i begin...

Doe's anybody have any advice?

import flash.display.MovieClip;

import flash.events.TouchEvent;

import flash.ui.Multitouch;

import flash.ui.MultitouchInputMode;

import flash.media.Sound;

var i:int = -1;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

var touchMoveID:int = 0;

var touchPoints : Object = {};

board.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);

function onTouchBegin(event:TouchEvent) {

    touchMoveID = event.touchPointID;

    var fingerPrint:tapC = new tapC();

    fingerPrint.x = event.stageX-stage.width/2;

    fingerPrint.y = event.stageY-stage.height/2;

    var tLevel=touchMoveID-event.touchPointID+1;

    this.board.addChildAt(fingerPrint,tLevel);

    myTextField.text = "touch begin " + event.touchPointID+" "+tLevel;

    this.board.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);

    this.board.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);

}

function onTouchMove(event:TouchEvent) {

    var tLevel=touchMoveID-event.touchPointID+1;

    var fingerPrint=this.board.getChildAt(tLevel);

    fingerPrint.x = event.stageX-stage.width/2;

    fingerPrint.y = event.stageY-stage.height/2;

    myTextField.text = "touch move" + event.touchPointID+" "+tLevel;

}

function onTouchEnd(event:TouchEvent) {

    var tLevel=touchMoveID-event.touchPointID+1;

    myTextField.text = "touch end" + event.touchPointID+" "+tLevel;

    var fingerPrint=this.board.getChildAt(tLevel);

    fingerPrint.play();

    delete touchPoints[ event.touchPointID ];

    this.board.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);

    this.board.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);

   

}

board is an "instrument" symbol on stage and

tapC is a moviClip that removes itself

This topic has been closed for replies.

1 reply

February 6, 2012

Try removing your events from stage instead of from this.board. so..

stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);

stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);

I also would suggest tracing your events so you know if they are firing or not, maybe with some visual element like a color change or a text box since you cant test touch events directly in flash.

Another edit, I noticed you never remove your touch_begin events, of course they stay there forever, remove them on touch_end.

igregurecAuthor
Inspiring
February 6, 2012

in that case i'll have to add them to the stage first but I don't want the whole stage to be touchable, at least not producing sound.

February 6, 2012

I could be wrong but in my experience adding an event to an object and then removing it from stage does exactly what you would want it to do, seems like stage supercedes everything, since your object is a child of the stage removing it from stage also removes it from your object even if your touch end event fires somewhere off the object. I too am working on a sound board and this solves the issue of touching a button then sliding your finger off the screen and releasing outside if the remove event is tied to the initial touch object it never fires since the touch_end event fired off the object. See my other edits in my first reply.