I haven't been able to replicate the setup, mainly because of hardware/software issues, plus I've been playing around with some other things. But I don't see any issues at first glance with your code.
I would suggest tracing/monitoring the stream of stuff that your Flash program has to consider during runtime. Like I mentioned, when I was working on my Catenary program I noticed that there was an ever-increasing delay in my Scratch program seeing changes in analog values from the board, so when I looked at it more closely I realized that the C program within the arduino board was swamping my Java/Processing program with analog values that it was then parsing and sending off to Scratch. To fix it, I had to change the sampling rate and come up with a way to ignore any sensor-update or broadcast that was not related to data acquisition.
On the other hand, it seems like you're pretty close. Unfortunately, this is one of those cases where using a non-open IDE (AS/Flash) is a major downside, since there are not as many hackers/coders with Flash on their machine (unlike Python, Processing, Ruby, etc.), so it's a little harder to get help with tough problems...
Last edited by chalkmarrow (2009-02-07 14:33:11)
Offline
I really don't know how to fix this. Your problem sounds similar to mine but I can't fix it so easily.
I made a test project that should have worked. In the test, once both computers were connected, the remote computer would update a single variable every 5 seconds. But it developed an increasing delay even still
I will try asking around some flash forums for help with this.
Last edited by archmage (2009-02-07 21:36:21)
Offline
I did notice that running Net_Tag2.sb and playing with the player number variable resulted in spamming the socket with sensor updates of playerNumber, because the stage script has a forever loop with no delay in it where it constantly updates those variables. I'm assuming your test programs are a lot simpler that the net_tag project, but that's the kind of thing that can bog down communication.
Last edited by chalkmarrow (2009-02-07 22:39:17)
Offline
But with my test I used another project. The only thing this project did was change a single variable every 5 seconds and even that didn't work.
Any ideas on why this simple communication isn't working?
Last edited by archmage (2009-02-07 22:46:19)
Offline
This Happens To Me In Scratch Too....Maybe You Could Convert This Into Flash:
[blocks]
<when green flag clicked>
<forever>
<set{ ???? }to( ???? )
I Don't Know.
Offline
Here is another question. I just tried sending broadcasts and about 50% of the time flash receives the message version "ScratchServer 2.0 alpha" instead of the broadcast message. Anyone have a clue on why this happens?
Offline
I am sorry Arch(Cam I call you Arch?)mage, but I can't help you. But...
I give you support on your way though by encourging you and keeping this alive.
You go Dudes!
Offline
Can anyone help make FlashScratchNetworking? Arch and Chalk need help!
Offline
Buddy_ca111 wrote:
Can anyone help make FlashScratchNetworking? Arch and Chalk need help!
Offline
archmage: i'll probably have time this weekend to get a couple computers setup to talk via scratch/flash, so i'll take another look. i'm curious: are broadcasts delayed in the same way that sensor-updates are delayed?
Last edited by chalkmarrow (2009-02-10 09:02:58)
Offline
chalkmarrow wrote:
archmage: i'll probably have time this weekend to get a couple computers setup to talk via scratch/flash, so i'll take another look. i'm curious: are broadcasts delayed in the same way that sensor-updates are delayed?
No acutally, broadcasts don't seem to have the same sort of delay, but I haven't tested it thoroughly. The main problem I am having with sending broadcasts is that about 50% of the time when I want to send a broadcast message flash receives a "ScratchServer 2.0 alpha" message.
I will test it more later and check out the results.
Offline
I've never seen the "ScratchServer 2.0 alpha" message myself, I suspect that is a question for Jens or Johnm.
Offline
so there is not wait command in actionscript arch? I'm getting flash soon so maybe I'll be able to help)
Offline
No there isn't, but there are alternatives. You should very rarely need to preform a wait anyways.
Offline
Also what about using lists? Is that possible?
Offline
Buddy_ca111 wrote:
Also what about using lists? Is that possible?
Lists are not really called lists in programming. They are called arrays.
Flash has different types of arrays including multi dimensional arrays that let you put arrays in your arrays
Actionscript can do everything scratch can and much more.
Offline
Archmage:
OK. I don't know if I've solved all the problems, but I was able to get a connection between Flash and Scratch over my network, and noticed that sensor update messages from Scratch to Flash were inconsistent. The following revised code works, however, after I changed the way readUTF() reads the input (using a while loop to empty the buffer completely each time data was received.) I was able to send 20 numbers 0.1 seconds apart from Scratch without losing any (though of course there was a little lag). This code is for one computer, but you will of course see how to modify it for two.
I'm continuing to play with it, but this will solve one of the major problems.
var dataOut:String;
//Create new socket
var s=new Socket();
//Create event listeners related to socket connection
s.connect('192.168.0.10',42001); // Scratch listens to port 42001
s.addEventListener(Event.CONNECT,socket);
s.addEventListener(Event.CLOSE,socket);
s.addEventListener(IOErrorEvent.IO_ERROR,socket);
//Create event listener for data sent from Scratch
s.addEventListener(ProgressEvent.SOCKET_DATA, dataInHandler);
// Send a string to scratch
function sendToScratch(dataOut:String) {
var sizeBytes:ByteArray = new ByteArray();
sizeBytes[0]=0;
sizeBytes[1]=0;
sizeBytes[2]=0;
sizeBytes[3] = dataOut.length;
for (var i:Number=0; i<4; i++) {
s.writeByte(sizeBytes[i]);//send each element in the array to scratch
}
s.writeMultiByte(dataOut, "us-ascii");
s.flush();
}
//Handle data sent from Scratch
function dataInHandler(event:ProgressEvent):void {
var inString:String = "";
//trace("received data:" + event);
while (s.bytesAvailable){
inString+=s.readUTF();
}
trace(inString);
}
// Handle event listeners for socket connection
function socket(Event):void {
switch (Event.type) {
case 'ioError' :
trace("can't connect");
break;
case 'connect' :
trace("connected");
sendToScratch('broadcast "HelloScratch"');
break;
case 'close' :
trace("can't connect");
break;
}
}Offline
Here's a better version that includes functions for parsing broadcasts and sensor updates:
var dataOut:String;
//Create new socket
var s=new Socket();
//Create event listeners related to socket connection
s.connect('localhost',42001); // Scratch listens to port 42001
s.addEventListener(Event.CONNECT,socket);
s.addEventListener(Event.CLOSE,socket);
s.addEventListener(IOErrorEvent.IO_ERROR,socket);
//Create event listener for data sent from Scratch
s.addEventListener(ProgressEvent.SOCKET_DATA, dataInHandler);
// Send any old string to Scratch
function sendToScratch(dataOut:String) {
var sizeBytes:ByteArray = new ByteArray();
// length <255. This section needs to be rewritten using bitwise ops.
sizeBytes[0]=0;
sizeBytes[1]=0;
sizeBytes[2]=0;
sizeBytes[3] = dataOut.length;
for (var i:Number=0; i<4; i++) {
s.writeByte(sizeBytes[i]);//send each element in the array to scratch
}
s.writeMultiByte(dataOut, "us-ascii");
s.flush();
}
// Send a broadcast message to Scratch
function sendScratchBroadcast(broadcast:String){
sendToScratch('broadcast "'+broadcast+'"');
}
// Send a sensor variable update to Scratch
function sendScratchSensorUpdate(variable:String, value:String){
sendToScratch('sensor-update '+variable+' '+value);
}
//Handle data sent from Scratch
function dataInHandler(event:ProgressEvent):void {
var inString:String = "";
while (s.bytesAvailable){
inString+=s.readUTF();
}
//trace(inString);
parseInput(inString);
}
// Handle event listeners for socket connection
function socket(Event):void {
switch (Event.type) {
case 'ioError' :
trace("can't connect");
break;
case 'connect' :
trace("connected");
sendScratchBroadcast('HelloScratch');
break;
case 'close' :
trace("can't connect");
break;
}
}
// Parse the data received from Scratch
function parseInput(input:String){
// Look for broadcast from Scratch
var pattern1:RegExp = /broadcast\s\"(\w+)\"/;
var result:Object = pattern1.exec(input);
if (result != null){
trace('Scratch broadcasted:'+result[1]);
}
// Look for sensor-update from Scratch
var pattern2:RegExp = /sensor-update\s\"Scratch-(\w+)\"\s(\w+)/;
result = pattern2.exec(input);
if (result != null){
trace('Scratch changed variable '+result[1]+' to '+result[2]);
}
}Offline
archmage wrote:
Buddy_ca111 wrote:
Also what about using lists? Is that possible?
Lists are not really called lists in programming. They are called arrays.
Flash has different types of arrays including multi dimensional arrays that let you put arrays in your arrays![]()
Actionscript can do everything scratch can and much more.
except:
[blocks]
<wait( )secs>
[/blocks]
Last edited by Buddy_ca111 (2009-02-13 01:16:41)
Offline
Buddy_ca111 wrote:
archmage wrote:
Buddy_ca111 wrote:
Also what about using lists? Is that possible?
Lists are not really called lists in programming. They are called arrays.
Flash has different types of arrays including multi dimensional arrays that let you put arrays in your arrays![]()
Actionscript can do everything scratch can and much more.except:
[blocks]
<wait( )secs>
[/blocks]
there are ways to simulate this. something looking like this:
var i = getTimer()
onEnterFrame = function () {
while(getTimr()<i+A){ //A being the amount of time you want to wait
}
trace ("you have waiter A seconds")
}
or you can use intervals. Generally i dont use neither methods though. i do the above in a IF statement
Offline
Thanks for the new script chalkmarrow
I think I am now at the point where I can make a simple game with this. But one thing that is bugging me is that the computer that is receiving input will never get a variable value that is less than 0. Any clue why this is?
Offline
So How Close Are You Done With This and SSBS? (Super Smash Brothers Scratch)
Offline
Magnie wrote:
So How Close Are You Done With This and SSBS? (Super Smash Brothers Scratch)
Not that close at all. Still fixing the character attacks. Right now I am working on the scratch networking features. I've had a lot of school work lately so that is slowing me down.
Offline
sounds great!
Offline