#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    43
    Rep Power
    14

    Is this Possible? (Flash/PHP/MySQL )


    I would like to know if Flash is the way to go with this possible project. I having a programming background, but don't know Flash yet, but will give it a go if this is possible (and the best language to use for this).

    I would like to have a number of objects (10-15) that are controlled by a set of coordinates. The coordinates would be read from a Database (MySQL) or from a text file. I assume I could have a Flash Application read this file every so many seconds (ie. 45 secs) and update the display accordingly.

    Is my above assumption correct?

    To populate the database (or create the text files) I would like to have a similar display with the ability to drag these objects (10-15) around and submit the coordinates of their positions. This would be an admin function display.

    Is that possible?

    Thanks for any help or guidance,
    Andrew
  2. #2
  3. No Profile Picture
    flash junkie
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    CO, USA
    Posts
    172
    Rep Power
    12

    loading txt data into flash


    it sure is possible... because it's a simple amount of data, i would probably leave out MySQL and just work with text files and PHP. Basically, you need to format your text file to look like this:

    &variable=value&variable2=value2& ...

    you could also do:

    &variable=value&
    &variable2=value2&

    just for readability's sake... So use PHP to fopen your text file, and fwrite or fput the new variables in (i'll explain how to get them from flash to php in a minute, but first, how to get them from php/txt to flash)

    If you are using Flash MX, you'll need to create a new LoadVars Object, and the process the variables from there:
    PHP Code:
    //actionscript
        
    myLoad = new LoadVars();
        
    myLoad.onLoad = function(done){
            if (
    done){
                
    trace ("success loading the file");
            } else {
                
    trace("something went wrong");
            }
        }
        
    myLoad.load("myText.txt"); 
    what this will do is let you track the loading of the text file. it's creating the LoadVars object, setting the callback function to an annymous function that accepts any value and the loads the actual .txt file. In this case, i'm just using trace to tell us if the file loaded successfully (the only way this will fail is if the file doesn't exists). we'll change the response when we know it's working right.

    So, as a mock text file, lets put some content in it (myText.txt):

    &variable1=nuttin1&
    &variable2=nuttin2&
    &variable3=nuttin3&
    &variable4=nuttin4&

    change your function in flash so that if(done) also has the following line:

    trace(this.variable1);

    it should trace "nuttin1" in the output window. notice that i had to put "this." before the variable name.. that is because when the variables are loaded in, they are loaded to the myLoad object, so we need to reference the `this' to point Flash in the right direction.

    Armed with this knowedlge, we can now use our function to load in variables and then process them when we get them. If we are dealing with coordinates, it will be easiest to use an array. Flash has an explode() method of it's own, so we'll just reformat the text file:

    &xcoords=12|15|17|19|21|24&
    &ycoords=50|27|34|85|48|22&

    and also our flash function. i'll write another function to show that our variables are now available out side of our loading function:
    PHP Code:
    //actionscript
    myLoad = new LoadVars ();
    myLoad.onLoad = function (done)
    {
        if (
    done) {
            
    this.xcoords.split("|");
            
    this.ycoords.split("|");
            
    trace ("success loading the file");
            
    processCoords();
        } else {
            
    trace ("something went wrong");
        }
    };
    myLoad.load ("myText.txt");
    processCoords = function(){
        
    trace(x);
        
    trace(y);

    Now that we have those variables in an array we can set any property (_x or _y included) of any object by indexing those arrays.

    mysquare._x = x[0];
    mysquare._y = y[0];
    //sets the coordinates to (12,50)

    now, writing the coordinates is a little more tricky, but it emcompasses the same methods...

    coming next post

    bret
  4. #3
  5. No Profile Picture
    flash junkie
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    CO, USA
    Posts
    172
    Rep Power
    12
    for this part, i just made a little .fla to demonstrate saving coordinates. open up the attached and follow along...(i've also included all of my source)

    each square has a start and stopdrag command on it, so you can move them. i'm using "duplicateMovieClip" to make copies of the original, and then setting the coordinates to a random value within the stage limits. here's the .fla script:

    PHP Code:
    //actionscript
    for(i=1;i<15;i++){
        
    square0_mc.duplicateMovieClip("square"+i+"_mc",i);
        
    this["square"+i+"_mc"]._x Math.round(Math.random()*550);
        
    this["square"+i+"_mc"]._y Math.round(Math.random()*350);
        
    this["square"+i+"_mc"].number_txt.text i;
    }
    submit_mc.onRelease = function(){
        
    mySend = new LoadVars();
        
    mySend.xsend = [];
        
    mySend.ysend = [];
        for(
    i=0;i<15;i++){
            
    mySend.xsend[i] = _root["square"+i+"_mc"]._x;
            
    mySend.ysend[i] = _root["square"+i+"_mc"]._y;
        }
        
    mySend.onLoad = function(success){
            if(
    success){
                
    trace("sent variables okay");
            
            } else {
                
    trace("something went wrong");
                
    getURL("javascript:alert('something went wrong');");
            }
        }
        
    mySend.send("writeTXT.php",mySend,POST);

    so, like i said, i just create duplicate the square and place it randomly... put those positions into an array, assign the array as part of my mySend object and send the variables off to PHP

    here's the writeTXT.php file:
    PHP Code:
    <?
        $x 
    $_POST[xsend];
        
    $y $_POST[ysend];
        
    $xcoords explode(",",$x);
        
    $ycoords explode(",",$y);
        
    $xputs "&xcoords=";
        
    $yputs "&ycoords=";
        for(
    $ii=0;$ii<count($xcoords);$ii++){
            if(
    $ii == count($xcoords)-1){
                
    $xputs .= $xcoords[$ii] . "&\n";
                
    $yputs .= $ycoords[$ii] . '&';
            } else {
                
    $xputs .= $xcoords[$ii] . '|';
                
    $yputs .= $ycoords[$ii] . '|';
            }
        }
        
    $puts $xputs .  $yputs;
        
    $text fopen("myText.txt","w");
        if(
    $text){
            
    fwrite($text$puts);
            
    fclose($text);
            print 
    '<script language="javascript">window.location = "/myText.txt";</script>';
        }
        exit;

    ?>
    if you've done PHP before, this should all be pretty straight forward. i'm a flash developer so there's probably better ways to do the PHP than the way i did it, but it works write back if you have questions regarding this script.

    here's a working example:

    http://dev.noskillx.com/sendCoords.html

    move the boxes around wherever you want and then send them. it will load the PHP script which will write to the text file and then open the text file to prove it worked

    http://dev.noskillx.com/loadText.html

    that will alert the new coordinates. Try changing the boxes around, every time you reload the loadText.html file, it will reflect your changes that you saved to the text file. That should be everything.

    cheers,

    bret
    Attached Files
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    43
    Rep Power
    14
    Thanks Bret. I give it a go.

    I'll probably be back for some assistance.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    12
    Rep Power
    0

    Thumbs up


    How good is that?

    He's done pretty much the whole thing for you, complete with step by step instructions almost!

    I've never seen anything like that before. A pat on the back for Bret for restoring my faith in humanity!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    43
    Rep Power
    14
    Hello,

    I am attempting to recreate the above scenario, but with 2 objects. I have created 2 symbols and used the "duplicateMovieClip" but only one of the two objects duplicates. It is whichever one is listed second (to duplicate). Why is this happening?

    I'm sure it is simple, but I am fairly new to Flash.

    Thanks,
    Andrew
  12. #7
  13. No Profile Picture
    flash junkie
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    CO, USA
    Posts
    172
    Rep Power
    12
    hey,

    when you use duplicateMovieClip, you need to be sure to specify a unique depth value, otherwise, only the last clip that you duplicated will show up (since only ony object can exist on a level at a time).

    levels are the way that flash organises it's stacking order, so you'll just need to specify a new one:

    square_mc.duplicateMovieClip("newsqaure1",1);
    square_mc.duplicateMovieClip("newsquare2",2);

    cheers,

    bret
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    43
    Rep Power
    14
    That did it!

    Thanks Bret.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    43
    Rep Power
    14
    I'm back with another question (for above scenario)...

    Is there an easy way to make it so my objects wont overlap?

    My thought is that once the object is "released" it can be run through the other object's coordinates to check for an overlap... and moved accordingly. Does this sound okay or is there something better?

    Note: My objects are circles.

    - Andrew
  18. #10
  19. No Profile Picture
    flash junkie
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    CO, USA
    Posts
    172
    Rep Power
    12
    hum... i think an easy way to check this would be to just do a hitTest with every movieclip when you let the object go and then snap back to it's original position if there is a hit somewhere. You could put all of the objects into an array and just loop through, using whichever one you grabbed as the hitTest source.

    write back if you need code?

    cheers,

    bret

IMN logo majestic logo threadwatch logo seochat tools logo