This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#101 2009-03-29 12:08:13

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

Your mProjector program is useless. Flash can turn swfs to exe all by its self. Just open your flash file and create a projector (AKA exe file)

Also my fla (for flash CS3) is coming

Sorry to sound like a rep for the program, but that's not true. AS2 alone can't create standalones with transparent backgrounds, manipulate files (outside of flash.net.FileReference, which is useless), mess with the Registry, render web pages in a MovieClip, or a host of other things. I don't know why I'm defending it, other than the fact that I just think it's cool  big_smile  !


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#102 2009-03-29 12:10:31

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

To anyone that cares, here is the download link for my networking program

It needs 2 pcs running scratch to work
http://rapidshare.com/files/214987709/NetTest_CS3.fla.html

AWESOME!


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#103 2009-03-29 12:19:51

demosthenes
Retired Community Moderator
Registered: 2008-02-19
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

To anyone that cares, here is the download link for my networking program

It needs 2 pcs running scratch to work
http://rapidshare.com/files/214987709/NetTest_CS3.fla.html

Only PCs (windows) not macs?


I've taken a long hiatus, but I still visit sometimes. Give me some time to answer any messages you post on my projects!

Offline

 

#104 2009-03-29 12:21:00

demosthenes
Retired Community Moderator
Registered: 2008-02-19
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

demosthenes wrote:

archmage wrote:

To anyone that cares, here is the download link for my networking program

It needs 2 pcs running scratch to work
http://rapidshare.com/files/214987709/NetTest_CS3.fla.html

Only PCs (windows) not macs?

Is it socket to socket do you have to make a direct connection to the other computer?


I've taken a long hiatus, but I still visit sometimes. Give me some time to answer any messages you post on my projects!

Offline

 

#105 2009-03-29 12:22:01

abeair
Scratcher
Registered: 2008-07-19
Posts: 100+

Re: scratch networking commands with actionscript 2.0

demosthenes wrote:

demosthenes wrote:

archmage wrote:

To anyone that cares, here is the download link for my networking program

It needs 2 pcs running scratch to work
http://rapidshare.com/files/214987709/NetTest_CS3.fla.html

Only PCs (windows) not macs?

Is it socket to socket do you have to make a direct connection to the other computer?

Macs can too. I ran it on my MacBook.


http://dragcave.net/image/NPtA.gif
http://www.danasoft.com/sig/ObamaLovesMacs.jpg

Offline

 

#106 2009-03-29 12:24:23

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

1. It works with any system that has flash cs3 or cs4

2. It directally connects to sockets


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#107 2009-03-29 15:01:04

Goon94
Scratcher
Registered: 2009-03-28
Posts: 4

Re: scratch networking commands with actionscript 2.0

would simply using the swf version work as well?

Offline

 

#108 2009-03-29 16:07:32

abeair
Scratcher
Registered: 2008-07-19
Posts: 100+

Re: scratch networking commands with actionscript 2.0

Goon94 wrote:

would simply using the swf version work as well?

Yes. I'll give you one.

Right here: http://rapidshare.com/files/215072358/NetTest_CS3.swf.html

Last edited by abeair (2009-03-29 16:07:48)


http://dragcave.net/image/NPtA.gif
http://www.danasoft.com/sig/ObamaLovesMacs.jpg

Offline

 

#109 2009-03-29 19:20:32

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

for some reason just .swf files won't work, I think it has to do with security

Just turn the file into an exe and it will work


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#110 2009-03-29 20:47:16

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

for some reason just .swf files won't work, I think it has to do with security

Just turn the file into an exe and it will work

Archmage:

Nice job! I've been playing around a little with networking, and hope to have a general Adobe AIR application along the same lines done some time next week.

Offline

 

#111 2009-03-29 20:51:38

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

That is awesome chalkmarrow!

Too bad mine isn't working at the moment.

When you upload it I think you should also write instructions on how it works so that everyone can use it easily.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#112 2009-03-29 22:03:18

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

That is awesome chalkmarrow!

Too bad mine isn't working at the moment.

When you upload it I think you should also write instructions on how it works so that everyone can use it easily.

No problem. It's really easy to use. (A 9-year old was able to make a two-person Doodle program using it, which is a good test).

Though I don't have a lot of examples yet, I'll probably upload it early so you and others can come up with some good ideas for how to use it.

Offline

 

#113 2009-03-29 22:54:59

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: scratch networking commands with actionscript 2.0

archmage wrote:

That is awesome chalkmarrow!

Too bad mine isn't working at the moment.

When you upload it I think you should also write instructions on how it works so that everyone can use it easily.

Here you go:

http://scratchconnections.wik.is/User:Chalkmarrow/Snyff

Maybe you can come up with something cool to do with it  smile

Offline

 

#114 2009-04-06 13:10:00

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: scratch networking commands with actionscript 2.0

I've put Chalkmarrow's original socket code into an AS3 class called ScratchSocket, which in addition to broadcasting and updating sensors, can interpret commands from Scratch. Right now the only two are 'getUrl' and 'nospace', and you can use them by:

1. Opening Flash CS3+ and loading the class description below into a blank .as file
2. Save the .as as ScratchSocket.as then make a new .fla in the same directory.
3. Place the following code into the .fla:

Code:

var socket:ScratchSocket=new ScratchSocket();

4. Save the .fla in the same directory as ScratchSocket.as
5. Open Scratch and enable sensor connections.
6. Test the .fla (ctrl+enter). It should trace ("connection successful")
7. Create a new global variable in Scratch called "sc.io.command"
8. Create this script in Scratch:

Code:

when flag clicked{
  set sc.io.command to 0
  wait 0.05 seconds
  set sc.io.command to "getURL :: http://scratch.mit.edu :: sc.io.returnVoid
}

Press the green flag. With any luck, your browser will open to the Scratch website!

Class code:

Code:

/*

ScratchSocket

@ This class sends and receives data from Scratch. Data received is sent to sc.Parse to
@ figure out what the heck Scratch wants, whereas other classes use this one to update
@ Scratch sensor variables.

@ Big thanks to Chalkmarrow for explaining how to communicate with Scratch with AS3

@ fullmoon, 2009

*/
package {
    //Import everything we need to work with flash.net.Socket and handle its events
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    import flash.utils.ByteArray;
    import flash.net.Socket;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;

    public dynamic class ScratchSocket {;
    //Reserve space for socket, but don't declare it
    private var socket:Socket;
    public function ScratchSocket() {
        //Declare socket
        socket=new Socket  ;
        connect();

    }
    private function connect():void {
        //Connect with Scratch on localhost, which responds to port 42001
        socket.connect("localhost",42001);
        socket.addEventListener(Event.CONNECT,socketConnect);
        socket.addEventListener(Event.CLOSE,socketConnect);
        socket.addEventListener(IOErrorEvent.IO_ERROR,socketConnect);
        socket.addEventListener(ProgressEvent.SOCKET_DATA,dataIn);
    }
    public function update(sensor:String,value:String):void {
        //Function to send a sensor update to Scratch
        sendScratchMessage("sensor-update " + sensor + " " + value);
    }
    //Function to send a broadcast to Scratch
    public function broadcast(broadcast:String):void {
        trace("ScratchSocket.broadcast");
        sendScratchMessage('broadcast "' + broadcast + '"');
    }
    private function sendScratchMessage(sendData:String):void {
        //The Scratch Connections protocol requires every message sent to Scratch to
        //be preceded by four bytes indicating the message length
        var bytes:ByteArray=new ByteArray;
        //Here are three '0' bytes
        bytes[0]=0;
        bytes[1]=0;
        bytes[2]=0;
        //Here's the actual message length
        bytes[3]=sendData.length;
        //Now we write the preceder plus the actual message to the socket
        for (var i:Number=0; i < 4; i++) {
            socket.writeByte(bytes[i]);
        }
        //Here comes the airplane!
        socket.writeMultiByte(sendData,"us-ascii");
        socket.flush();
    }
    private function socketConnect(Event):void {
        //Handle connection events linked in constructor
        switch (Event.type) {
            case 'connect' :
                trace("connection successful");
                broadcast("jump");
                break;
            case 'close' :
                trace("connection failed");
                connect();

                break;
            case 'ioError' :
                trace("connection failed through IOError");
                break;
        }
    }
    private function dataIn(event:ProgressEvent):void {
        trace("ScratchSocket.dataIn");
        //Create an empty string
        var inString:String = "";
        while (socket.bytesAvailable) {
            //Read the next part of the socket data into inString
            inString+=socket.readUTF();
        }
        //Send inString off to class SCparse for evaluation
        evaluate(inString);
    }
    // Parse the data received from Scratch
    private function evaluate(input:String) {
        trace("ScratchSocket.evaluate("+input+")");
        // Look for broadcast from Scratch
        // Matches any string starting with 'broadcast ' followed by a string of non-return characters in "quotes"
        var pattern1:RegExp = /broadcast\s\"(.*)"/;
        var result:Object = pattern1.exec(input);
        if (result != null) {
            trace('Scratch broadcasted:'+result[1]);
        }
        // Look for sensor-update from Scratch
        // Matches any sensor-update string Scratch sends
        var pattern2:RegExp = /sensor-update\s\"Scratch-(.*)\"\s\"(.*)\"/;
        result = pattern2.exec(input);
        if (result != null) {
            trace('Scratch changed variable '+result[1]+' to '+result[2]);
            //If the variable changed is 'sc.io.command' then interpret it.
            if (result[1]=="sc.io.command") {
                interpret(result[2]);
            }
        }
    }
    private function interpret(input:String):void {
        trace("ScatchSocket.interpret("+input+")");
        var command:Array=input.split(" :: ");
        trace(command);
        var returnVal="";
        //If the command returns to an invalid sensor then return to the default sensor, 'sc.io.defaultReturn'.
        if (command[2].slice(0,6)!="sc.io.") {
            command[2]="sc.io.defaultReturn";
        }
        //Find out what the command wants
        switch (command[0]) {
            case "getURL" :
                trace("sc.getURL");
                navigateToURL(new URLRequest(command[1]));
                returnVal="success";
                update(command[2],returnVal);
                break;
            case "nospace" :
                trace("sc.concat");
                var a:Array=command[1].split(" ");
                for (var i=0; i<a.length; i++) {
                    if (a[i]=="") {
                        a[i]=" ";
                    }
                }


                var s:String=a.join("");
                returnVal=s;
                update(command[2],returnVal);

        }


    }

}
}

This is just a test, and eventually I plan to incorporate getter/setter functions for Flash persistent Shared Objects and advanced keyboard/mouse input, etc into an AIR application like Snyff. But for now, I'm having trouble getting Scratch to receive a message with spaces in it. When I try the command:

Code:

set sc.io.command to "nospace :: t h i s  i s  c o o l :: sc.io.sayThis"
say(sensor value(sc.io.sayThis))

which is supposed to take a concatenated list from Scratch and turn it into an actual word, it just returns "this", which leads me to my question: Can Scratch receive messages with spaces in them?

Last edited by fullmoon (2009-04-06 13:14:53)


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#115 2009-04-07 13:12:24

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: scratch networking commands with actionscript 2.0

That's a great idea, converting it to an AS3 class. Looks really cool. Regarding your question, I don't believe Scratch has any problems receiving spaces in messages, since I do that with Snyff all the time. I haven't looked at your program closely, so I'm not sure yet what the issue is. Good luck with this, though.

Offline

 

Board footer