Help with simple Flash & ReacTIVision problem please! :)
Posted: 25 July 2008 10:18 AM   [ Ignore ]
New Member
Rank
Total Posts:  4
Joined  2008-07-15

Hi everyone, I hope someone can help me with this smile

I’m using Flash and reacTIVision for a project but I’m having some problems. I’ve set up a webcam under a perspex sheet that fiducials are moved around on. I’ve downloaded the TUIO Flash demo from here: http://nuigroup.com/forums/viewthread/281/ and finally got it to work.


What I’d like to do is:
* Depending on the coordinates of the fiducial (I’ll define the coordinates in the code), move to a particular frame of a simple Flash animation I’ve made.
* Depending on whether a particular fiducial is present, open a flash pop-up screen and control a video (simply play or stop a video clip).

I thought this would be a simple IF statement - e.g. if fiducial with id = 2 is at coordinate (100,200) then go to frame 20.

The problem I’m having is I can’t figure out where in the code to write the extra code to do this. I’ve pasted the code from the demo below.
I’m thinking that my code should use sid, id, x, y, a but how? Or maybe it should go in the part that says “THIS IS WHERE YOU COULD DO SOMETHING COOL”?!

I feel so dumb for not being able to figure out something so simple - any help would be appreciated soooo much!! thanks

.
.
.

/**
* flosc - Flash OpenSound Control
* Ben Chun, 2002
*
* Credits:
* design and structure based on MoockComm by Colin Moock.
* server based on CommServer by Derek Clayton.
* (see TcpServer.java and TcpClient.java for more information.)
*
* Modified by Michel Maas for attaching movieclips as objects
* Adding, moving, rotating + removing functional. Also adds
* a textfield with the fiducial number on the MC.
*
* Also added cursor (fingertip) support.
*
*/

stop ();

// *** general init
var incomingUpdated false;    
incoming "";                    

createEmptyMovieClip("Scene",1);
createEmptyMovieClip("Line",2);
var 
objectList = new Array(120);
var 
cursorList = new Array(120);

// here we create holders for the objects and cursor
for (var i=0;i<120;i++) {
    
var tuio Scene.attachMovie("object","tuio"+i,i);
    
tuio.x=0;
    
tuio.y=0;
    
tuio.active=false;
    
tuio._alpha 0;
    
    var 
tuic Scene.attachMovie("cursor","tuic"+i,120+i);
    
tuic.x=0;
    
tuic.y=0;
    
tuic.active=false;
    
tuic._alpha 0;    
    
    
objectList[i] false
    
cursorList[i] false;
}

// create the cursor
function addCursor(sidxy){

    
var tuic Scene["tuic"+sid];
    
tuic._alpha 100;
    
Line.lineStyle(1,0x000000,100);    
    
    
tuic._x=x;
    
tuic._y=y;    
    
Line.moveTo(x,y);    
}

// draws the movieclip on the scene
// In here it gets rotated and moved.
function updateObject(sididxya{
    
//incoming += "set "+sid+" "+id+" "+x+" "+y+" "+a+"\n";
    
var tuio Scene["tuio"+sid];
    
tuio._x=x;
    
tuio._y=y;
    
tuio.num.text id;
    
tuio._rotation 100;
    
tuio._alpha 100;
}

// draws the cursor (fingers) on the scene.
// same as the object, moves as well.
function updateCursor(sidxya){
    
//incoming += "set "+sid+" "+id+" "+x+" "+y+" "+a+"\n";
    
var tuic Scene["tuic"+sid];
    if (
tuic._x 0){
        tuic
._x=x;
        
tuic._y=y;
        
Line.lineTo(x,y);
    
else {
        addCursor
(sidxy);
    
}
}

// these two functions remove the movieclips
function removeObject(id{
    
//incoming += "remove "+i+"\n";
    
var tuio Scene["tuio"+id];
    
tuio.unloadMovie();
}

function removeCursor(id{
    
//incoming += "remove "+i+"\n";
    
var tuic Scene["tuic"+id];
    
tuic.unloadMovie();
    
Line.clear();
}

// Not used by this method, left in here for you, if you ever need it
//function draw(tuio) {
//    tuio.clear();
//    tuio.beginFill("0x000000",100);
//    
//    x1 = tuio.x-15;
//    x2 = tuio.x+35;
//    y1 =  tuio.y-15;
//    y2 =  tuio.y+35;
//    tuio.moveTo(x1,y1);
//    tuio.lineTo(x2,y1);
//    tuio.lineTo(x2,y2);
//    tuio.lineTo(x1,y2);
//    tuio.endFill();
//}
    
// attach the scroll manager movie
// (see its child movie's enterframe event for scroll code)
attachMovie("processScroll""processScroll"0);

// turn off ugly yellow highlight on buttons
_focusrect false;


// *** create a new socket and attempts to connect to the server
function connect () {
    mySocket 
= new XMLSocket();
    
mySocket.onConnect handleConnect;
    
mySocket.onClose handleClose;
    
mySocket.onXML handleIncoming;

    if (!
mySocket.connect(IPaddressport)) gotoAndStop("connectionFailed");
}


// *** disconnect from the server
function disconnect () {
    
for (var i=0;i<120;i++) {
        
//var tuio = Scene["tuio"+i];
        
var tuio "tuio"+id
        tuio
.removeMovieClip();
    
}
    
    mySocket
.close();
    
mySocket.connected false;
    
incoming "";
    
gotoAndStop(2);
}


// *** event handler for incoming XML-encoded OSC packets
function handleIncoming (xmlIn{
    
    
// USEFUL DEBUG - display the raw xml data in the output window
    // incoming += xmlIn.toString() +"\n";

    // parse out the packet information
    
xmlIn.firstChild;
    if (
!= null && e.nodeName == "OSCPACKET"{
        packet 
= new OSCPacket(e.attributes.addresse.attributes.port,
                                
e.attributes.timexmlIn);
        
displayPacketHeaders(packet);
        
parseMessages(xmlIn);
    
}

    
// tell the text field manager it's time to scroll
    
incomingUpdated true;
    
lastScrollPos incoming.scroll;
}


// *** event handler to respond to successful connection attempt
function handleConnect (succeeded{
    
if(succeeded{
        incoming 
+= "Connected to " IPaddress " on port " port "\n";
        
mySocket.connected true;
    
else {
        mySocket
.connected false;
    
}
    gotoAndPlay
("connecting");
}

function handleClose () {
    incoming 
+= ("The server at " IPaddress " has terminated the connection.\n");
    
incomingUpdated true;
    
mySocket.connected false;
    
numClients 0;
}

function OSCPacket(addressporttimexmlData{
    this
.address address;
    
this.port port;
    
this.time time;
    
this.xmlData xmlData;
}


function displayPacketHeaders(packet{
    
/*incoming += "** OSC Packet from " + packet.address +
                ", port " + packet.port +
                " for time " + packet.time + "\n";
                */
}


// *** parse the messages from some XML-encoded OSC packet
//
//     THIS IS WHERE YOU COULD DO SOMETHING COOL
//     (probably based on the value of the arguments)

function parseMessages(node{
    
//trace(node);
    
if (node.nodeName == "MESSAGE"{
        
if (node.attributes.NAME=="/tuio/2Dobj"{
            
// from here on it parses the object (fiducial markers) data
            
var child node.firstChild;
            var 
cmd child.attributes.VALUE;
            if (
cmd=="set"{
                child 
child.nextSibling;
                var 
sID child.attributes.VALUE;
                
child child.nextSibling;
                var 
id child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE 640;
                
child child.nextSibling;
                var 
child.attributes.VALUE 480;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
                var 
speed Math.sqrt(X*X+Y*Y);
                
// sID, ID, xPos, yPos, angle, mSpeed, rSpeed, mAccel, rAccel
                
updateObject(sID,id,int(x),int(y),a);
                
// this is to check if the marker is still there
            
else if (cmd=="alive"{
                
                
var newList = new Array(120);
                for (var 
i=0;i<120;i++) { newList[i] false}

                child
=child.nextSibling;
                while (
child != null{
                    
var id child.attributes.VALUE;
                    
newList[id]=true;
                    
child=child.nextSibling;
                
}
                
                
for (var i=0;i<120;i++) 
                    
if ((objectList[i]==true) && (newList[i]==false)) {
                        removeObject
(i);
                    
}
                }
                
                objectList 
newList;
                
            
}
        } 
else if (node.attributes.NAME=="/tuio/2Dcur"{
            
// from here on it parses the cursor data
            
var child node.firstChild;
            var 
cmd child.attributes.VALUE;
            if (
cmd=="set"{    
                child 
child.nextSibling;
                var 
sID child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE 640;
                
child child.nextSibling;
                var 
child.attributes.VALUE 480;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;
                
child child.nextSibling;
                var 
child.attributes.VALUE;

Profile
 
 
Posted: 25 July 2008 10:18 AM   [ Ignore ]   [ # 1 ]
New Member
Rank
Total Posts:  4
Joined  2008-07-15

Rest of the code:

updateCursor(sID,int(x),int(y),m);
                
// checking if it is still there, otherwise remove it
            
else if (cmd=="alive"{
                
                
var newCursorList = new Array(120);
                for (var 
i=0;i<120;i++) { newCursorList[i] false}

                child
=child.nextSibling;
                while (
child != null{
                    
var id child.attributes.VALUE;
                    
newCursorList[id]=true;
                    
child=child.nextSibling;
                
}
                
                
for (var i=0;i<120;i++) 
                    
if ((cursorList[i]==true) && (newCursorList[i]==false)) {
                        removeCursor
(i);
                    
}
                }
                
                cursorList 
newCursorList;
                
            
}
        }
    }
    
else // look recursively for a message node
        
for (var child node.firstChildchild != nullchild=child.nextSibling{
            parseMessages
(child);
        
}
    }
}



// *** build and send XML-encoded OSC
//
//        THIS IS ANOTHER PLACE TO DO SOMETHING COOL

function sendOSC(nameargdestAddrdestPort{
    xmlOut 
= new XML();

    
osc xmlOut.createElement("OSCPACKET");
    
osc.attributes.TIME 0;
    
osc.attributes.PORT destPort;
    
osc.attributes.ADDRESS destAddr;

    
message xmlOut.createElement("MESSAGE");
    
message.attributes.NAME name;

    
argument xmlOut.createElement("ARGUMENT");
    
// NOTE : the server expects all strings to be encoded
    // with the escape function.
    
argument.attributes.VALUE escape(arg);
    
argument.attributes.TYPE "s";
    
    
// NOTE : to send more than one argument, just create
    // more elements and appendChild them to the message.
    // the same goes for multiple messages in a packet.
    
message.appendChild(argument);
    
osc.appendChild(message);
    
xmlOut.appendChild(osc);

    if (
mySocket && mySocket.connected{
        mySocket
.send(xmlOut);
        
incoming += "Sent XML-encoded OSC destined for "
                    
destAddr
                    
", port "
                    
destPort
                    
"\n";
    
}

    incomingUpdated 
true;
}

Profile