A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Auto-Refresh.....

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5

    Auto-Refresh.....

    Hi all, further from my last post I now need a section of code to auto-refresh every 30 seconds or so. So far my code takes hex codes out of tweets containing a certain hashtag, and draws a square of that hex code colour. If someone were to tweet a new set of hexcodes while you were looking at the site, you would have to manually refresh the page to see the new squares. Here is my code so far, I'm hoping this is simple to do! I'll be so grateful for any help.

    Actionscript Code:
    import flash.net.URLLoader;


    var myXMLLoader:URLLoader = new URLLoader();
    var myXML:XML;
    myXMLLoader.load(new URLRequest("http://search.twitter.com/search.rss?q=%23twitmosaic"));
    myXMLLoader.addEventListener(Event.COMPLETE, processXML);


    function processXML(e:Event):void{
        myXML = new XML(e.target.data);
        trace('FROM FUNCTION:'+ myXML.channel.item[0].title);
        trace (myXML.channel.item.length());
        //tweetsArray[0] = myXML.channel.item[0].title;
        for (var l:int = 0; l < myXML.channel.item.length();l++)
        {
            trace(l);
            splitStrings(l);
           
        }
        chooseColour();
        }

    var hexes:Array = [];
    var squareX:int = 275;
    var squareY:int = 200;
    var countX:int = 0;


    function splitStrings(currentTweet: int):void{
        hexes.clear;
        var bigString:String = myXML.channel.item[currentTweet].title;
        var tags:Array = bigString.split(" ");
       
        for (var i:int =0; i < tags.length; i++){
          var tag:String = tags[i].substring(1); //remove first char, which is "#"
          trace('TAG '+tag);
          if (tag == 'twitmosaic'){
            trace('twitmosaic ignored');
          }
          else if(hexes.length < 100){
            hexes.push(tag);
            trace('added to array ' +hexes);
          }
          else{
              trace('array full');
          }
        }
    }



    //finishedHex = 0x += 6 numbers variable

    function checkPosition():void{
        if (countX == 10){
            countX = 0;
            squareX = 275;
            squareY += 25;
        }
    }

    function chooseColour():void{
        for(var i=0;i<hexes.length;i++){
            var aHexVariable = hexes[i];
            trace(aHexVariable);
            var finishedHex = '0x'+aHexVariable;
            checkPosition();
            drawSquare(finishedHex);
        }
    }

    function drawSquare(currentColour: int):void{
        var square:Sprite = new Sprite();
        addChild(square);
        square.graphics.beginFill(currentColour);
        square.graphics.drawRect(0,0,25,25);
        square.graphics.endFill();
        squareX += 25;
        square.x = squareX;
        square.y = squareY;
        countX ++;
    }

  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    5
    I also need to add a line of code to say if 'tag' is anything other than 6 characters in length, ignore it. I've tried a few different ways of doing this, but being an actionscript novice, can't figure out how to do it without it pulling up an error. Hoping this is simple too!

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To make the request repeatedly, you need to use a Timer.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by 5TonsOfFlax View Post
    To make the request repeatedly, you need to use a Timer.
    OK thanks..... I've just had a quick google and I'm not quite sure how to use a Timer, sorry for being such a novice! Maybe I should have put this in that category..... Could you explain a little more please?

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    A Timer is just an object that dispatches events after a delay. You can configure the delay and how many times it should repeat.

    Let's say you wanted to reload your xml every 30 seconds:
    Code:
    var twitterTimer:Timer = new Timer(30000);
    twitterTimer.addEventListener(TimerEvent.TIMER, loadXML);
    twitterTimer.start();
    
    function loadXML(e:Event):void{
      myXMLLoader.load(new URLRequest("http://search.twitter.com/search.rss?q=%23twitmosaic"));
    }

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    5
    OK thanks that's great and it works! But what if I only want it to add to the array if there are new tweets? Because with that Timer at the moment, every time it refreshes it adds all the tags again....

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not really familiar with twitter, but I opened up that page you're requesting and it looks like they provide a twitter:refresh_url which includes a timestamp. Just save that on each load, and load that next time.

Tags for this Thread

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