Archive for the ‘Action script’ Category

Flash for math

Saturday, July 11th, 2009

On Flash and Math one finds tutorials for creating math applets with Actionscript 3. There are also excellent live examples like the Derivative Plotter.

On Amazon.com there is an ActionScript community.

Flash scrollbar with action script 3

Friday, July 10th, 2009

In the first frame of the page that uses a scrollbar write in the action layer:

stop;
setupScrollbar(oslotext);
/* where oslotext is the name of the movie clip to be scrolled and scrollbar is the name of the scrollbar

/* This code is placed in frame 1 on the action layer . When the user clicks somewhere this code runs. */

stop();
import fl.controls.UIScrollBar;
import fl.events.ScrollEvent;

var scrollingMC:MovieClip;
var diffY:Number = 0;

function setupScrollbar(mc:MovieClip=null):void {

 removeFocusRects();
 var sb:UIScrollBar = getChildByName("scrollbar") as UIScrollBar;
 if(scrollingMC == mc) {
 return;
 }
 scrollingMC = mc;
 if(mc != null) {
 sb.visible = true;
 if(scrollingMC.name == "oslotext") {
 sb.x       = 763;
 sb.y       = 37;
 sb.height  = 479;
 }
 sb.enabled = true;
 diffY = sb.y - scrollingMC.y;
 sb.setScrollProperties(scrollingMC.height, 0, (scrollingMC.height-sb.height));
 sb.addEventListener(ScrollEvent.SCROLL, scrollMC);
 sb.scrollPosition = 0;

 } else {
 sb.visible = false;
 }
}
function scrollMC(event:ScrollEvent):void {
 scrollingMC.y = -event.position + event.target.y - diffY;
}

function removeFocusRects():void {
 for(var i:int = 0; i < numChildren; i++) {
 var o:DisplayObject = getChildAt(i);
 if(o.name.indexOf("btn") != -1) {
 o["focusRect"] = false;
 }
 }
}

The code was extracted from code written by my son Paulo.

Code for various actions when clicking a button

Saturday, July 4th, 2009

Here is the code for moving to a different frame when clicking a button, open a pdf file when clicking a button, open a url when a button on an image is clicked, and play the flash again when an image is clicked.

/* This code is placed in frame 1 on the action layer .
When the user clicks somewhere this code runs. */

stop();

import flash.events.MouseEvent;

/* Listen for mouse clicks. */
addEventListener(MouseEvent.CLICK, clickHandler);

/* When the user clicks somewhere with the mouse, this function is running. */
function clickHandler(event:MouseEvent):void
 {

 /* if what was clicked was not a button then do nothing */
 if(!(event.target is SimpleButton))
 {
  return;
  }

/*
These are the possible button names:
btnframe(label of frame) -
example: btnframepage43 -
action: goto frame 43
btnpdf(file name of pdf without the .pdf extension) -
example: btnpdfmanual -
action: open the file manual.pdf, the files is in the folder pdf/

btnurl(name of web site) -
example: btnurlcnn -
action: open the website cnn, the url is given in the code below

btnplayagain -
example: btnplayagain -
action: play the flash file again
 */

  /* remove the btn part of the button name*/
 var name:String = event.target.name.toLowerCase().replace("btn", "");

 if (name.indexOf("frame") != -1)
   {
   name = name.replace("frame", "")
   trace("Going to frame " + name);
   gotoAndStop(name);
       return;
   }

 if(name.indexOf("pdf") != -1)
  {
  var pdfName:String = "pdf/" + name.replace("pdf", "") + ".pdf";
  trace("Open PDF file " + pdfName);
  navigateToURL(new URLRequest(pdfName));
  return;
  }

 /* if an image is clicked and a url should open */
 if (name.indexOf("url") != -1)
   {
   name = name.replace("url", "");
   var urlName:String = "";
   switch(name)
     {
      case "cnn":
         urlName = "http://www.cnn.com/";
         break;
     case "andres":
          urlName = "http://www.andresnordgreen.com/";
          break;
      }
      trace("Open URL: " + urlName);
      navigateToURL(new URLRequest(urlName));
      return;
  }

if (name.indexOf("playagain") != -1)
 {
 trace("Play again");
 gotoAndPlay(1);
 return;
 }

}

How to make a button that jumps to another frame (internal web page)

Thursday, June 25th, 2009

I found the code here and video tutorials here and here for how to do it with Action Script 3.

Update June 28:

The code was something like this. If someone clicks the button My_btn the function CursorClick will run. CursorClick moves to frame 2.

stop();
import flash.events.MouseEvent;
My_btn.addEventListener(MouseEvent.CLICK, CursorClick);
function CursorClick(event:MouseEvent):void
{
  gotoAndStop("2");
}

Thanks to some code from my son Paulo, here is better code. The listening for mouse click is set at frame 1 in the action layer and any click is listened for. If a button is clicked one moves to the frame given by the name of the button (the ‘btn’ part is removed). The frame one jumps to needs to have a layer with label identical to the name of the button. This is a far better solution as only one piece of code is needed and not one piece of code for each button.

stop();
import flash.events.MouseEvent;
addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(event:MouseEvent):void  {  if(!(event.target is SimpleButton))    {     return;     }   var name:String = event.target.name.toLowerCase().replace("btn", "");
gotoAndStop(name);   }

The next step is to add code for what should happen if a button that opens a pdf file or opens a window with an external url is clicked.

Action script

Wednesday, June 24th, 2009

Again, last time was in 2007, I have decided to learn a little bit of Action Script that is used in Flash.

Here are a few posts from the blog I made (it has now been deleted):

Learning by stealing

April 19, 2007 by jannordgreenA compiled flash file has extension swf. When you see a nice flash you want to save you can not just right-click it and save it as you can do with images.

Here are a few ways to copy a swf file to your hard disk.

  1. In Firefox, File – Save Page As – Web Page, complete. This gives you a folder you can open. It contains all the swf files on the page you saved.
  2. In Firefox, use the tool Web Developer. Information – Web Page Information – Media. Scroll to a swf file and click Save As.
  3. Download Sothink SWF Catcher for Firefox. Click Alt+C and all the swf files on the page are listed on the left.

A flash source file has extension fla. How do you decompile a swf file to a fla file so you can have a look at to see how it was made?

Sothink SWF Decompiler is the only tool I have found so far. The free download does not include any action script in the swf file, and it lasts only 30 days. The full version comes with a full price at $79.99. (I don’t know about you, but I intend to buy a chocolate with the one cent it is off $80.)

Now I have found a few more! Here is a list.

DeFlash costs only $12, but has dubious information about its availability:

Resources for beginners

May 2, 2007 by jannordgreenI have received some resource tips:

Thanks Paulo Fierro for the tips!