Hungry? Join our RSS Feed

Send to a friend

Featured Videos

Some amazing footage of how Photoshop can be used to great effect..

Even with its limited tools and palette, someone has managed to d..

Learn how to create 3d spinning animated text with thie Swift 3D ..

Sorenson Squeeze is a great piece fo software that has saved my a..

This speed painting video is a great way to see how you would go ..

Popular Tutorials

Futuristic city, 3D techniques in Flash

See the tricks behing the advanced Flash animation that was used to bring this futuristic ..

Futuristic Texture Pack

A collection of futurstic textures used in the Future city project...

Gradient Mapping

How to transform a standard photograph for use with a specific colour scheme or brand imag..

Creating a 3D Tunnel Effect in Flash

Learn how to create this very cool 3D tunnel effect in Flash using some simple 2D animatio..

Flash Actionscript Goodies

Heres a few helpful bits or flash actionscript code to help you on your way with developing dynamic flash content.

 

Random Number

There are a few ways to generate a random number in flash it all depends on what you need and how your going to do things, heres some examples:

Generate a random number between 0 and 10


variable = Math.random()*10;

Result: variable = 7.967452

 

Rounding a Random Number


variable = Math.floor(Math.random()*10);

Result: variable = 7

 

Random Number (Between specific numbers 10-20)


variable = Math.floor(Math.random()*(20-10+1))+10;

Result: variable = 15

 

For/While Loops

Loops are useful because they allow you to cycle through variables without having to write mountains of code, heres an example of a simple loop for creating many different numbered variables each with a random number.


for (i=0;i<11;i++) {
var tempvar = "variable"+i;
_level0[tempvar] = Math.floor(Math.random()*30);
}

Remember once you define the letter (i) in the loop, it will display the value it has been given, in this example (i++) means that it will add 1 to (i) each time, (<11) means this loop will stop after (i) hits 10. The Output for the above is as follows:

variable1 = 23
variable2 = 17
variable3 = 3
variable4 = 9
variable5 = 22
variable6 = 12
variable7 = 6
variable8 = 9
variable9 = 10
variable10 = 30

 

Key Listeners

Key Listeners allow you to record the input of keys from the keyboard in flash and allow you to peform actions with keys

First you need to setup your key listener as a function as follows:


KeyListener = new Object();
KeyListener.onKeyDown = function() {
if (Key.getAscii() == 13) { // Code for ENTER key
gotoAndPlay(25);
} else if (Key.getAscii() == 8) { // Code for BACKSPACE key
gotoAndPlay(35);
}
}

 

Then either after the function or on another frame where you want the key listender to start simply add this code:


Key.addListener(KeyListener);

 

Useful Key ASCII/ALT Codes

ENTER

13

BACKSPACE

8

0

48

1

49

2

50

3

51

4

52

5

53

6

54

7

55

8

56

9

57

Comments (6)

Thank You .. Realy helpful tips .. Please can you tell me how to make SUDOKU random ... i know it is large detailes ... thank you again

Posted by: PacimaN on 13/08/2008

Thanks for long winded response, but the aim of this post was to create a bookmark with code people could copy paste into flash as and when they need it, thus saving brina power for creativeness ;)

Posted by: Ben on 24/07/2008

@ UnknownFuty: Anybody who's had basic maths classes and had probability as a unit will know that the rand int functions returns a number between 0 and 1. This is then multiplied by a number to obtain a certain range of numbers. In this case in the loop it's been multiplied by 30 so doing the maths the maximum obtainable result is 30 passing the parameters perfectly. That being completely besides the point, this is a great turorial and I got it bookmarked , thx!

Posted by: Mathew on 24/07/2008

Thx for the tip

Posted by: ranjithkes on 22/07/2008

Thx some good tips i have saved!

Posted by: Freedom47 on 19/07/2008

Since when has 23 been between 0 and 10? Fail.

Posted by: UnknownFury on 19/07/2008

Comments:

Author:  

What colour is the sky?

More Content

Building your website with includes

Building your website with includes

Using server side includes to design and code your web site allows you to keep your develo..

Getting more Google Search Traffic

Getting more Google Search Traffic

A look at the things I've done over the past few months that have dramatically increased m..

Tips and tricks for designing logos

Tips and tricks for designing logos

How to design a logo covering some good font techniques, how to get inspiration, playing w..

Graphics Design Guide

Graphics Design Guide

The second in a series of help and reference guides, this one covers graphics design and h..

Adsense Warfare, Using graphics design to increase earnings

Adsense Warfare, Using graphics design to increase earnings

Graphics techniques that can double your click-thru rate with Google Adsense..

Flash EXE Presentation for an Auto-Run CD

Flash EXE Presentation for an Auto-Run CD

This article will teach you how to create an Auto run CD for a fullscreen Flash presentati..

 Subscribe to our feed

Send to a friend