I was wondering about OSC could you tell me more about it.
Asked
Active
Viewed 710 times
1 Answers
1
I've used TUIO-AS3's UDPConnector for some minimal OSC interaction with as3 for a few projects and didn't have any problems. Here a minimal snippet to illustrate what I've done:
package
{
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
import org.tuio.connectors.UDPConnector;
import org.tuio.osc.*;
public class BasicOSC extends Sprite implements IOSCConnectorListener
{
private var oscSocket:UDPConnector;
private const OSCSERVER:String = "127.0.0.1";
private const PORT:int = 8082;
public function BasicOSC()
{
try{
oscSocket = new UDPConnector(OSCSERVER,PORT);
oscSocket.addListener(this);
trace(this,"OSC ready");
}catch(e:Error){ trace(e.getStackTrace()); }
}
public function acceptOSCPacket(oscPacket:OSCPacket):void{
//handle OSC here
var message:OSCMessage = oscPacket as OSCMessage;
trace("message from :",message.address,"at",new Date());
for(var i:int = 0; i < message.arguments.length; i++)
trace("\targs["+i+"]",message.arguments[i]);
}
}
}
Update: Notice that I'm casting the OSCPacket as an OSCMessage which behind the scenes deals with the parsing and easily makes the address and arguments available, which is what you're after.
For reference here's the minimal Processing sketch I've used to simulate your setup:
import oscP5.*;
import netP5.*;
OscP5 osc;
NetAddress where;
void setup() {
frameRate(25);text("click to send\nOSC",5,50);
osc = new OscP5(this,12000);
where = new NetAddress("127.0.0.1",8082);
}
void draw() {}
void mousePressed() {
OscMessage what = new OscMessage("/straps");
what.add(193.4509887695313);
osc.send(what, where);
}
HTH

George Profenza
- 50,687
- 19
- 144
- 218
-
Ok thanks, i will give this a go, i will let you know how it goes – Feb 19 '13 at 17:21
-
Ok basically i have entered your code into my AS3 script file and i get the error "1017: The definition of base class DatagramSocket was not found". Am i doing something wrong? – Feb 19 '13 at 19:52
-
The [DatagramSocket](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/DatagramSocket.html) class is an AIR class. Try to add `import flash.net.DatagramSocket;` in the class that throws the error. – George Profenza Feb 19 '13 at 20:08
-
It still throws the same error after that import, i tried making it by creating a new "Air 2.0" application rather than ActionScript 3.0 and it works... but i really need it to work in ActionScript 3.0 – Feb 19 '13 at 20:13
-
For some reason when i try to import the datagram socket library in as3 it doesnt automatically pick it up in the dropdown menu, so I am guessing it can't find a library ? – Feb 19 '13 at 20:19
-
You are still using Actionscript 3.0 in AIR. The datagram socket library is only avaible in AIR, not in the Flash Player. You can try a [UDP to TCP bridge](http://www.memo.tv/udp-tcp-bridge/) since you can't do UDP in Flash Player(but you can in AIR). Personally I think using AIR makes it simpler. Any particular reason why you can't use it ? Btw, I've uploaded a minimal Flash Builder project [here](http://lifesine.eu/so/BasicOSC.zip). HTH – George Profenza Feb 20 '13 at 14:58
-
So when using Air I can build a project as I would normally do in flash with no differences, such as the as3 code for saving to text files, loading , Tweens etc? And if it doesn't save as a flash file what file will it be? Sorry I just have experience in as3 making a flash swf project that's why I'm asking – Feb 20 '13 at 17:55
-
Correct! With AIR you have two main options: compile html/js/css for the runtime or compile as3 for the runtime. Use FlashBuilder to export an AIR application and install it. You will notice in the install directory there is also a swf. Basically the runtime can 'play' that swf but on top of doing what Flash Player does anyway it allows for some pretty neat operating systems interactions(file access/udp/native extensions/[etc](http://www.adobe.com/uk/products/air/features.html).) Basically treat an as3 AIR project as the same way you would a typical as3 one, but with extra features :) clear ? – George Profenza Feb 20 '13 at 22:41
-
Quick question. My number is coming up as the oscPacket... how can i convert this to a float so i can use it in my project, because when i run the actionscript file alone it comes up with the figures when it traces it, but when i include the actionscript file into my project and for example if i attempt to change the height of a square in accordance to the figures, for example : `include "OSCTest.as";` `square.height = oscPACKET;` It does not do anything and it doesnt trace anymore either. Is there something i'm doing wrong? – Feb 25 '13 at 14:12
-
@HK11 The object return is of type [OSCPacket](https://code.google.com/p/tuio-as3/source/browse/trunk/org/tuio/osc/OSCPacket.as?r=204). I see there's no readFloat() method by default and I'm not sure how your particular packet is formatted.You can try to use [ByteArray's readFloat()](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html#readFloat()) method to extract float value. `oscPacket.getBytes().readFloat()`.Hopefully that will work, otherwise, you need to parse the bytearray:move through it until you find the location you want to read from HTH – George Profenza Feb 25 '13 at 15:41
-
OK thanks alot for your help! I keep coming up with the Package name error as i need to import the package and class into the code on the timeline. the name of the fla is "Project.fla",the actionscript file is called "OSCTest.as" and are within a folder called "Reciever Project" on my desktop. I decided to name the package of the "OSCTest.as" to `package RecieverProject` and tried to import the package and class onto the timeline using this code: `import RecieverProject.OSCTest;` – Feb 25 '13 at 19:42
-
but it keeps coming up with the error of `5001: The name of package 'RecieverProject' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. C:\Users\ec09191\Desktop\RecieverProject\OSCTest.as` Could you help me out please? – Feb 25 '13 at 19:42
-
Unfortunately not atm, but should be able towards the end of the day. Depends how you've got your .fla's classpath setup. changing the `package{` to `package RecieverProject{` sounds about right on a 1st look. – George Profenza Feb 26 '13 at 16:54
-
Yeah that's how mine is but I'm not sure how to setup the class path properly for it to work, whenever you have a chance please! Thanks – Feb 26 '13 at 23:14
-
So your .fla file is straight on the desktop, right ? If so, can you not leave the default/empty package as it was before and simply add the ReceiverProject to the classpath of your document ? There are plenty [articles/tutorials](http://mmedia.glos.ac.uk/mu210/T39/T39_3.htm) out there. It's a matter of accessing the .fla document's Actionscript 3.0 Settings and adding ./ReceiverProject to the classpath. This will roughly 'tell' the as3 compiler to look into that folder for code – George Profenza Feb 27 '13 at 01:43
-
Couldn't answer anytime sooner. Check out the updated answer: simply cast the `OSCPacket` to `OSCMessage` and access the `address` and `arguments` properties. HTH,George – George Profenza Mar 06 '13 at 16:10
-
Thank you so much! it works now!! you have saved my life lol! one question though, right now i have one frame with code in it that gets the argument and changes a textfield on the stage in accordance to whatever the argument is... that works when i text in flash, however when i publish it the .swf file does anything? is there any different ways i should be publishing when using AIR? – Mar 07 '13 at 21:15
-
when i publish it is it packaging all the external .as files and libraries too? – Mar 07 '13 at 21:22
-
Welcome to stackoverflow ! Feel free to vote and mark/tick this answer as you see fit ;) I'm not sure I understand your question regarding flash/air. Basically compiling the AIR project means generating a swf behind the scenes and `wrapping` it for the runtime rather than flash player – George Profenza Mar 08 '13 at 15:10
-
oh and yes: when you publish, the .as files and libraries you use get compiled into a .swf file and that .swf is packaged as an AIR application so your the external .as files and libraries are there, but compiled of course – George Profenza Mar 08 '13 at 17:52
-
Ok thanks very much, do you have any experience in using AS3 to send a variable into PHP to then be put into a sql database, because I have completed my code and I can't seem to understand why it is not working. Isit okay if i show you that please? – Mar 26 '13 at 01:41
-
Basic rule of thumb: if it sounds like a common problem, there's probably a stackoverflow [answer](http://stackoverflow.com/questions/6876588/sending-and-receiving-data-from-flash-as3-to-php) for that. Also if you use an URLLoader to POST data, make sure you [add data](http://stackoverflow.com/questions/10262149/issue-with-making-a-post-call-to-a-rest-service-from-as3). HTH – George Profenza Mar 26 '13 at 11:00
-
ive tried having a looking on Stackoverflow and cant find an exact solution... could you have a look at my question i posted and see if you understand it please on http://stackoverflow.com/questions/15731491/data-not-being-sent-to-php-from-as3 – Mar 31 '13 at 16:14
-
ive posted the problem on that page with the error, any help will be appreciated thank you – Apr 02 '13 at 16:42
-
Thank you for the help on the other page mate you have been really helpful, system staff are looking into whats wrong with the server, hopefully all turns out well. By the way one question i already asked before and its about saving to SWF... when i test out saving it. the swf doesnt run the way it runs when i test the movie within flash? is there a specific process i need to follow for it to complete the application as it is in the timeline? – Apr 06 '13 at 01:48
-
all the SWF does is loads up a white screen with black dots in the middle of the screen as if it is loading something? – Apr 09 '13 at 19:07
-
@HK11 This is getting lengthy and off topic. Not sure if this particular post is the best place. A chat room could work suited or better yet you can find my email address somewhere [here](https://code.google.com/p/lifesine/) – George Profenza Apr 09 '13 at 22:16