Friday, 20 April 2012

How to print jobs in as2

In AS2, the PrintJob class allows you to pick parts of the screen, or particular movieclips, or frames of a movie clip to send to the printer.

You can make changes to the appearance of a movie clip, send it to the printer, and then change it back.

var pj:PrintJob = new PrintJob();
pj.start(); //now you can find out the orientation of the page setup, or the width of the printable area etc.
pj.addPage(_root);
pj.send();


The addPage function has four parameters:

The first lets you say which movie clip you want to print.
The second lets you pick a particular area of the movie clip.
The third lets you render bitmap pictures more accurately.
The fourth lets you pick a frame of the movie clip to print, so you could print frame 2 of the root timeline while still on frame 1.

For more info, see http://docs.brajeshwar.com/as2/PrintJob.html

Thursday, 12 April 2012

How to alter text files on your hard drive with as3

This uses the FileReference class, and shows how to access a text file, and save a text file.
To access the text file:

var fr:FileReference = new FileReference(); //if this is created within a function, it won't work
//because it really only exists within that function

function lFile(evnt:MouseEvent):void {//this function has to be initiated by the user

fr.addEventListener(Event.SELECT, seld); //the method that will be called when the user selects a file.
fr.browse(); //this opens up the dialog box


}


function seld(evnt:Event):void {


fr.addEventListener(Event.COMPLETE, compd);
fr.load(); //makes the file available to the swf

}


function compd(evnt:Event):void {


outpt.text = String(fr.data);


}


The uploading of a file has to be initiated by the user, e.g. by clicking a button.



To save some text to a file:

var fr2:FileReference = new FileReference();


function sFile(evnt:MouseEvent):void {//again, this has to be called by the user

fr2.save(outpt.text, 'textfile.txt');//second parameter is optional

}

One odd thing I noticed with this, by specifying what I wanted the text file to be called, if I accepted that name in the saveas dialog box, then it was fine, even though the '.txt' was not shown in the dialog box. If, however I changed the name to something else, then I had to add the '.txt' myself, or it wouldn't know what kind of file it was supposed to be. (So if I go to Windows Explorer, it doesn't have the right icon, and when I try to open it from there, I get asked what program should be used.)

How to get the REAL date into Flash

Flash has a Date object, that can give you the date and time, but it gives you the date and time that is on the users computer.

e.g.
var dt:Date = new Date();
trace(dt);

This will trace out whatever time the user's computer is set to, whether it is 5mins slow, or 10 years fast.

If this date/time is being used for some kind of authentication i.e. to see if the deadline is past, then you should load the time from the server.

var ld:LoadVars = new LoadVars();
ld.onLoad = function(succ:Boolean) {
        dt_txt.text = this.servertime;
}
ld.sendAndLoad('servertime.php',ld,'POST');

On the servertime.php file:


$today = date('YmdHis');
echo "&servertime=$today";
?>

(Sometimes I have issues with the data being echoed out, try removing the ampersand from the front, or not.)

The reason why I use the date('YmdHis') is because it gives you a number that is perfect for sorting, and a recognised datetime stamp for mysql, if you ever need it.

4.30pm (and 22 seconds), 5/11/2012   =    20121105163022

If you need to sort the dates, you can do a numeric sort. If you want to check if something happened before Jun 2012, you look to see if the number is smaller than 20120600000000, and so on.

Tuesday, 10 April 2012

How to make a div scrollable

<style type="text/css">
div.scroller
{
       overflow: scroll;
       width:300px;
       height:500px;
}
</style>
...
<div id="scroller"></div>

The scroller property is controlled via css. You won't see the scroll bars unless their is a width and height to your div.

How to use javascript to change an element's attributes

document.getElementById('id').setAttribute("style","position:absolute;left:10px");


This method is useful if you have a lot of properties to add, but it can overwrite the other style attributes that you may have already set.


An alternative method is:

document.getElementById('id').style.width = "500px";

Sunday, 8 April 2012

Javascript: Rewrite element content

How to change the content of an element...?

In the head tag:

<script type="text/javascript">
function giveWidth(e) {
 document.getElementById('wid').innerHTML = window.innerWidth;
}
</script>

In the body tag:

<p>Window width <a href="" onclick="giveWidth(event);return false">is</a> <span id="wid">500</span> pixels.<p>

The return false makes sure that the browser doesn't try to open a new window when you click on the link.