Tag Archives: as3

Bad PI Approximation

Actionscript:
  1. var inside:Number = 0
  2. var precision:Number = 1000000;
  3. for (var i:int = 0; i<precision; i++){
  4.        var xp:Number = 0.5 - Math.random();
  5.        var yp:Number = 0.5 - Math.random();
  6.        if (Math.sqrt(xp * xp + yp * yp) <0.5){
  7.                inside++;
  8.        }
  9. }
  10. trace(inside / precision * 4);
  11. // outputs : 3.143304

Someone described this bad method of a PI approximation to me the other day... figured I'd try it out... pretty funny. I always liked 22/7... but this one is definitely funnier...

Posted in Math | Also tagged , | 3 Comments

Project Time-bomb

Actionscript:
  1. var d:Date = new Date()
  2. // payment deadline
  3. var payMonth:int = 9;
  4. var payDay:int = 5;
  5. // add five days so it's not too obvious and so you can replace
  6. // the swf on their server if they do pay
  7. payDay+=5;
  8. if (d.getMonth()>= payMonth){
  9.     if (d.getDay()>= payDay){
  10.         // very nasty code to crash flash, alternately you could do anything that will break
  11.         // you app
  12.         while(1){
  13.            stage.addEventListener(Event.ENTER_FRAME, function(){ this["__"+Math.random()]=getTimer()*Math.random()});
  14.         }
  15.     }
  16. }

WARNING: This snippet and post are a JOKE. Be careful, this code could potentially cause flash a 15 second timeout in flash and will eat ram and cpu.

Have you ever had to bug a client to get paid when the site has already gone live? Sometimes the agreement you have with the client could prevent you from getting paid until 30 or even 90 days after the site is live. I generally try to avoid these types of agreements and get a chunk of 30-50% upfront, but every now and then the job is too good to pass up and I make the choice to just suck it up and wait to get paid.... anyway...

This snippet adds a time-bomb to your swf... if the client doesn't pay up... five days after the payment deadline the swf will stop working. They will be forced to contact you and you can say that they need to pay before you'll do any additional work. You can say you don't know what the problem is off the top of your head, you'll need to go in and take a look. You can say that your local version works... so maybe the online file was corrupted... but that you'll need to be paid before you can do anything else.

I'm just kidding around. Luckily, out of all the freelance jobs I've done over the years there was only one time when I didn't get paid in full...

Posted in misc | Also tagged , | 3 Comments

Is this swf in a container?

Actionscript:
  1. if(parent == stage){
  2.    // no container
  3. }else{
  4.   // has container
  5. }

Found myself writing this snippet today.... it figures out if the current swf is inside a container (Loader) or not... lots of other ways to do this... feel free to post your version in the comments...

Posted in Loader, external data, misc | Also tagged , | 5 Comments

ActionSnippet Contest

I started ActionSnippet last year in October. Excluding the last few months I posted pretty much everyday... resulting in about 350 posts including this one. If you haven't spent time looking at some of the early posts... I recommend digging around as there are lots of very compact tips and tricks... have a look (scroll down for early posts).

For at least the next two months I'm going to post at least twice a week rather than the original every day... that said, I think it would be nice to post additional ActionScript snippets submitted by readers. This has actually been something that numerous people asked me about when the site first went online. So, if you have a snippet you'd like to submit you can simply e-mail me (bendvent [] gmail) with the subject line "AS3 Snippet". You should include your full name, link to your website and any other information you would like to be published along with your snippet.... this can include anything from links to work you've previously done... to references related to the snippet your submitting. The snippet you submit should ideally be less than 30 lines of code, but can be as long as 100 lines. This snippet submission process will go on for the next month and will end November 1st. On November 8th one of the people who submitted will be chosen as the winner and can choose from either a $300 apple gift certificate or a $300 deposit to their paypal account.

When submitting, it's important to note a few things:

If you send me a snippet that already exists on the website I'll e-mail you back and let you know that this snippet is already on the site. If you send me a snippet that doesn't make any sense, has errors etc... I may not reply at all. If you send me a snippet that I would like to include on ActionSnippet, I will e-mail you back to discuss things that we will include in the post. Only snippets that end up as blog posts on this site will be considered for judging.

Since ActionSnippet is a pretty low traffic site, I'm not expecting for there to be too many submissions, but it would be cool if people who like this site attempt to help spread the word by tweeting and blogging about this contest.

If you have any questions feel free to post comments. I have a feeling that I may need to clarify a few things about the contest.

After the contest is over I may decide to run another contest or create a snippet submission page.... my decision will be related to how well this contest goes.

Currently I'm the only judge for the contest... but there will be at least one other judge announced in the next day or two.


Updates and Contest Rules

You can submit as many snippets as you want... you aren't limited to just one... and if I like more than one of your snippets you will get multiple blog posts on the site.

Posted in Uncategorized | Also tagged , , | 11 Comments