Showing posts with label as2. Show all posts
Showing posts with label as2. Show all posts

Monday, 18 August 2014

How to randomly generate -1 or +1, in one line.

A hobby of mine is avoiding if statements, with mathematical formula that return the right number based on other conditions.

It is easy to generate a 0 or a 1 randomly, simply use Math.round(Math.random());

That will work in a number of languages, or something similar.

For a random -1 or +1, try this:

var randNum = -1 + (2*Math.round(Math.random));

If the random number generated by Math.random() is 0.5 or above, then the answer will be 1. If the random number generated is less than 0.5, the answer will be 1.

Enjoy!

Tuesday, 16 April 2013

How to Make Flash Respond to a Clicker and Act Like A PowerPoint Presentation

I've just had to make a short presentation, and I was going to do it in PowerPoint - certain things, like making your bullet points appear one after the other, are very quick to do in PowerPoint.

But positioning things, and making the exact transitions that you want, are very difficult. So I switched to Flash. I thought I'd publish it as an executable, so it would work on anything WIndows.

I laid out my 'slides' as frames on the root timeline, each with an animated clip that contained the transitions that would occur within a slide. I named each of these movie clips 'clp'. The first frame and the last frame appear in one go, so they didn't have a 'clp' movie clip on them.

The main timeline has to stop at the beginning, so I used stop(). And each of the 'clp's is set to play, with stop frames within it.

Making it go forward when you click on the screen:

You can make a transparent movieclip that covers the screen and give it a name, e.g 'fwd'.

On the frame (this is AS2 code):

fwd.OnRelease = function() {
_____if(_root.clp._currentframe < _root.clp._totalframes) {
___________root.clp.play();
_____}else{
___________root.nextFrame();
_____}
}

The if statement only returns true if the movieclip exists and it isn't on its last frame, so in that case it advances the animated slide, otherwise, it goes to the next slide on the root timeline.
If this was AS3, then Flash would get upset everytime the movieclip didn't exist.

I also made other clips to give a finer control of the going backwards and forwards of the presentation, using similar code.

Making it go to full screen:

On the first frame, on the root timeline, I used fscommand("fullscreen",true);

Making it respond to a clicker:

This was the cool bit. A clicker is a little hand held device, you plug the USB port into your laptop/whatever, and it has forward and back buttons that the powerpoint presentation responds to, and usually a laser pointer as well, so you can walk around the stage while you present.

The forward and back buttons are actually registering on the computer as PageUp and PageDown buttons.
To get your Flash executable to respond to these buttons:

On the first frame of the root timeline:

var keyListener:Object = new Object();
keyListener.onKeyUp = function() {
_____if(Key.getCode() == 34) {
__________fwd.onRelease();
_____}
}
Key.addListener(keyListener);

34 is the keycode for the page forward button, and the code above tells the executable to do whatever it is that the fwd button would normally do when it is clicked.

I was under the impression that when fullscreen, flash wouldn't accept keyboard events, but perhaps this doesn't apply to the page up etc type buttons.

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 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.

Monday, 3 October 2011

How to Shuffle an Array in One Line

I often have to get a randomised version of an array, and I used to do it like this:

Create an empty array for the shuffled version
(Within a for loop) {
Get a random index using Math.floor(Math.random() * array.length)
Put the value from the unordered array into the ordered array, using the random index
Remove the value from the unordered array, again using the random index.
}

But now:

(Within a for loop) {
array.push(array.splice(Math.floor(Math.random() * array.length),1));
}

One line, no need for an empty array to be created.

Thursday, 29 September 2011

No text? Are you sure?

I just spent about half an our trying to get Flash to recognize a blank text field as 0.

First I wrote a function to strip out the blanks (maybe Flash already has this, but anyway...), but when I tried an if statement to check if the empty input box.text was == to empty (''), I kept getting false.

It turned out that the html option was on, so even an empty text input box has a sort of text associated with it, the html tags.

How irritating - but now I have switched off the html option, so it is just a plain old text box, and an empty space is just empty space.

Wednesday, 22 September 2010

SetInterval

I hardly ever use this, but in all honesty, it is very useful.

The code below is what I have used for a button which starts or stops the setInterval method. This is AS2 code.

var methInterval = null;
btn.onRelease = function () {
if(methInterval == null) {
methInterval = setInterval(meth,200);
} else {
clearInterval(methInterval);
methInterval = null;
}
}

There may be a more graceful way to do this, but this worked for me. If you trace the interval after clearing it, you get numbers anyway, and I think the number goes up by one each time you set it, so why not just set it to null when you are not using it?!