A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: sending&recieving :: great code.. need help!

  1. #1
    Senior Member
    Join Date
    Dec 2002
    Posts
    156

    sending&recieving :: great code.. need help!

    I have this neat code to send and recieve parameters through 2 movies:

    // Sender Movie:
    _root.onEnterFrame = function () {
    mySender = new LocalConnection();
    mySender.send("incomingMessage", "onRecieve", myInputText.text)
    mySender.close();
    };

    // Reciever Movie:
    myReciever = new LocalConnection();
    myReciever.onRecieve = function(myParameter) {
    myDynamicText.text = myParameter;
    };
    myReciever.connect("incomingMessage");

    In this case it sends text from an input box to a text box in other movie.

    The question i'd like to ask is, how do i adapt this code to send a variable such as: light = on / off. in my movie1 clicking a button will change for example my light on or off, on load of movie2 i want the light to be on or off accordingly. the variable thats sent is then put into an if statement in movie2 to say if its on then load whatever symbol etc. i just need to get the state across! any ideas much appreciated...

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    try an onEnterFrame event to capture the variable
    code:

    myReciever = new LocalConnection();
    myReciever.onRecieve = function(myParameter) {
    myDynamicText.text = myParameter;
    };
    myReciever.connect("incomingMessage");

    this.onEnterFrame = function(){
    myDynamicText.text == "off" ? light._visible = 0 : light._visible = 1;
    };


  3. #3
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    hi, thanks for replying.
    sorry im a dumbass i should have shown the code for the lights as im not sure that will work out...

    lights = "off" //set lights to off
    _root.car_mc.lights_btn.onPress = function () {

    switch (lights) {
    case "off":
    _root.car_mc.lights_mc.attachMovie("onLight", "newLights", 1)
    lights = "on"
    //(above) when the lights are "off", switch to the onLight symbol & set them to "on"
    break;
    case "on":
    _root.car_mc.lights_mc.attachMovie("noLight", "newLights", 1)
    lights = "off"
    break;
    //(above) when the lights are "on", switch to the noLight symbol & set them to "off"
    }
    }

    this is in the sender movie, as u can see it attaches a symbol. its also in the reciever movie. the reciever needs to take its initial lights = " " from the passing variable.

    Here... are the FLA's so you can see
    Attached Files Attached Files

  4. #4
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    just incase i made it confusing, the text and textboxes are nothing to do with it, theyre just an example to show that communication is working. the code needs adapting to work for the light symbols.
    cheers,
    Adam

  5. #5
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    does this file do what you wanted ?

  6. #6
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    hi, yeah it does.
    the thing is i really cant have the text boxes. i see it relies on the contents of the box being on/off? is there any way to remove the use of the text boxes in both movies?

    later i shall be adding about 5 more variables such as sound=on/off so i really need it to be passed via the variable state of IF on/off rather than if the textbox.text is.. " ",

  7. #7
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    hide the textfields well off the stage area

    the localConnection relies on them -
    code:

    mySender.send("incomingMessage", "methodToExecute", myInputText.text);

    myReciever.methodToExecute = function(myParameter) {
    myDynamicText.text = myParameter;
    };
    myReciever.connect("incomingMessage");


  8. #8
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    ok thankyou!
    i was hoping i could lose the text box method because its for an assignment and i am marked heavily on code and methods, it would be better to pass the value accross but this works so thanks.

    i almost managed it by changing the textbox reference to the name "state" to try and assign it to that. and it worked up until i removed the use of the on/off label in textbox in Sender ::


    SENDER::

    _root.onEnterFrame = function () {
    mySender = new LocalConnection();
    mySender.send("incomingMessage", "methodToExecute", state);
    mySender.close();
    };

    //lights code **********************************
    lights = "off" //set lights to off onLoad
    _root.car_mc.lights_btn.onPress = function () {

    switch (lights) { //radio case statement
    case "off":
    _root.car_mc.lights_mc.attachMovie("onLight", "newLights", 1)
    lights = "on";
    break;
    case "on":
    _root.car_mc.lights_mc.attachMovie("noLight", "newLights", 1)
    lights = "off"
    break;
    }
    }

    RECIEVER::
    myReciever = new LocalConnection();
    myReciever.methodToExecute = function(myParameter) {
    state = myParameter;
    };
    myReciever.connect("incomingMessage");

    //lights code **********************************
    this.onEnterFrame = function(){
    if(state == "on"){
    _root.car_mc.lights_mc.attachMovie("onLight", "newLights", 1);
    lights = "on";
    } else{
    _root.car_mc.lights_mc.attachMovie("noLight", "newLights", 1);
    lights = "off";
    }
    }

    I think only the sender requires the use of the textbox, the reciever works fine without it if you rename it to something like "state"
    Just one question, how would it be possible for me to add further code into the connection section to allow another change "radio = on/off", then i have a case statement in each movie for car = red/blue/green/yellow :S do i need to produce a newLocalconnection code + a new text box and variable name for each?

  9. #9
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    or using lights as the variable -
    code:

    /*in sender*/
    mySender = new LocalConnection();
    mySender.send("incomingMessage", "methodToExecute", lights);

    /*in receiver*/
    myReciever.methodToExecute = function(myParameter) {
    theVar = myParameter;
    };

    this.onEnterFrame = function(){
    if(theVar == "on"){...}


  10. #10
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    this works well in seperate movies.
    i have just moved the code into my actual movies. The thing is the Reciever movie is a .swf loaded into an empty movie clip symbol within the so called sender movie.

    When played in that state it doesnt work, when played seperately it does. what possible reason could this be? i dont understand

  11. #11
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    hmm.. not sure what you are trying to do, as localConnection is a method of sharing variables between separate movies in a browser window ( or separate browsers, for example, a movie running in Netscape can send a localConnection to a movie running in Internet Explorer.)

    if you are loading one movie into another, you only need the correct path between the two movies to pass variables -
    in loaded movie -- lights = _root.lights;
    from loading movie -- _root.loaderMC.lights = "on";

    btw, you can pass multiple params in a localConnection -

  12. #12
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    ohhh i see. god id be f*$!úd without this help!
    i see how the multiple parameters work.

    are you saying i shouldnt use this localConnection method??

    ..see i have my main movie with the car. on a click this symbol travels to a certain point and loads a smaller scene (external.swf) into an empty_mc. this swf also contains an identical car symbol. due to the interactivity of the lights, radio etc in main movie.. i need the lights to be on (at load) in the second movie if they were so clicked in the 1st before loading of the 2nd. to make the transition truely seamless. i hope this is clear. i would post it but its huge.

  13. #13
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    as i said previously, localConnection is used to communicate between two movies in the same HTML page, or two movies in different pages.

    i assume you have car_main.swf
    it has a movieclip, instance name - empty_mc
    you wish to load car_sub.swf to empty_mc -

    empty_mc.loadMovie("car_sub.swf");

    in car_main.swf, you have variables -
    lights = "on";
    radio = "on";

    in car_sub.swf, watch for these variables with -
    code:

    this.onEnterFrame = function(){

    if(_level0.lights == "on"){
    // do something
    } else {
    // do something else
    }

    if(_level0.radio == "on"){
    // do something
    } else {
    // do something else
    }

    };



    hth

  14. #14
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    Originally posted by a_modified_dog

    i assume you have car_main.swf
    it has a movieclip, instance name - empty_mc
    you wish to load car_sub.swf to empty_mc -

    empty_mc.loadMovie("car_sub.swf");

    in car_main.swf, you have variables -
    lights = "on";
    radio = "on";

    [/as]

    hth
    This is true yes. I have added the code but still no luck. i think its because of using the code level0 as you described,


    if i strip my movie of sounds etc do you have an email that will hold just over 2mb?

  15. #15
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    flash is so stupid how come when you remove all your sounds and massive images and re-save it the FLA is the same size!

  16. #16
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    zip the 2mb and post to some webspace,
    send a link to the zip and i'll download
    save new file with "Save as"

  17. #17
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    i cant get it uploaded, i have 1 with a limit of 1mb, and another where it wont accept anything but html and downloads as html 4kb :S

    any ideas?

  18. #18
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    ok think i got it sorted..

    go here

    hope this works, i tried downloading it and it decided it was only a few kb :S but its up there and its 2mb in zip.

    if no luck then follow link at this web page

    get back to me cheers
    Adam

  19. #19
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    file save as gives 4kb of nothing here
    the full link -
    http://www.adamgraham.catalogg.com/t.../flash/car.zip
    reports -
    The file you are searching for is not located on our servers

    can you not make two new fla,
    copy the problem layer and actionscript from car_main to newfla1
    copy the problem layer and actionscript from car_sub to newfla2

    zip and post these new fla here

  20. #20
    Senior Member
    Join Date
    Dec 2002
    Posts
    156
    voila!

    bloody flash! how come when i copy every single frame, layer and action to a new movie, it goes from 2.5mb to 100kb! ok so i used to have some sound in it, which made it rise to 2.5 but why when you remove that does the size stay the same. irritates me.


    anyway here it is.. thanks! any time you have to look at it much appreciated. all im really looking for is the lights, body colour (note these are symbols changed when the brush is dragged onto the car), and the radio to keep state in the new movie. then a second movie garage.swf which i will simply copy code to.

    as you'll notice the sub swf is called wash and is located on the right, load it via the button. code is all on layer.

    i'll be out for a while but will check back later, thanks again
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center