Contact ZenDuo


Archive for: ‘ActionScript & Flash’

Actionscript 2 key listener

Tuesday, February 23rd, 2010

A simple and effective way of attaching a listener to a flash document. Great for using keyboard presses to perform actions on a website. The example below can be used to call a function when the user hits the enter key on their keyboard.

keyListener = new Object();
keyListener.onKeyDown = function() {
if (Key.getCode() == Key.ENTER){
trace('you just hit the enter button')
}
};
Key.addListener(keyListener);

Tags: ,
Posted in ActionScript & Flash | 1 Comment »

Find and Replace AS2

Friday, February 19th, 2010

A nice simple function to find and replace characters in a string using actionscript 2. This is perfect if you want to replace words or even remove words/characters from a string.

function stringReplace(myString:String, findThis:String, replaceThis:String):String{
return myString.split(findThis).join(replaceThis);
}

myString = "example-of-some-words-with-dashes"
/////Strip out all of the dashes by replacing -'s with nothing
myString = stringReplace(linkname, "-", "");
trace(myString)

Tags: , ,
Posted in ActionScript & Flash | No Comments »

Spacer: Perfect layout spacing

Friday, February 12th, 2010

Get Adobe Flash player

When creating any website design layout, its crucial to pay attention to detail. A common layout formation is the three column. This often requires you to calculate the width of each box taking into account any margins you wish to apply to these boxes. This handy little tool does all of the calculations for you. Simply enter all of the information in the boxes above to retrieve the exact width to set each box to.

Tags: , , , ,
Posted in ActionScript & Flash, Website Design Tools | No Comments »

Vertical / Horizontal Scrolling in Flash

Monday, September 28th, 2009

The following examples below show how to make a movie clip in flash dragable either horizontally or vertically with set positions stopping the movie clip being dragged to far.
In these examples we will use 50 as the lowest position the object can be moved and 200 as the highest value that the object can be moved.

Verticle Scrolling (up and down):

on (press) {
startDrag(this, 0, this._x, 50, this._x, 200);
}

The opposite effect can be applied when you switch the variables and number around to this._y

Horizontal Scrolling (left and right):

on (press) {
startDrag(this, 0, this._x, 50, this._x, 200);
}

Posted in ActionScript & Flash | No Comments »

Changing the right click menu in flash

Monday, September 28th, 2009

flash-right-clickChanging the right click menu in flash files is relitivley simple. Using the code below and placing it on the root of your document you can add a new right click menu to your published files. This disables the existing zoom in, zoom out ect and enables you to attach functions to the buttons, whether it be launching a new web page or changing the colour of the movie clip document.

So heres what to do:

Paste the following code onto the root of your flash document, directly onto a blank frame, publish your movie, Job done!

And heres the code:

//define any functions to included on a menu item
function launchsite1(){
getURL("http://www.zenduo.co.uk", _blank);
}

function launchsite2(){
getURL("http://www.zenduo.co.uk/contact.php", _blank);
}

////Declare a new menu item
newmenu = new ContextMenu();//Hide the built in flash menu
newmenu.hideBuiltInItems();//define your new menu items
item1 = new ContextMenuItem("My Home Page", launchsite1);
item2 = new ContextMenuItem("Another Web Page", launchsite2);
newmenu.customItems.push(item1);
newmenu.customItems.push(item2);//Build New Menu
_root.menu = newmenu;

You can change add or remove any of the items above to suit your requirements.

Posted in ActionScript & Flash | No Comments »

For loops in Flash – Setting global variables

Monday, September 28th, 2009

A common ‘mistake’ when working with any programming language is over use of code, or rather not being efficient with code. The most common mistake, and most easily rectified is repetition of similar or exact snippet of code, over and over and over and over and over and over again…. just like the example actionscript code shown below.

Example of Inefficient use of code

_global.variable1 = 0;
_global.variable2 = 0;
_global.variable3 = 0;
_global.variable4 = 0;
_global.variable5 = 0;

In the above example we are simply setting a whole load of global variables in flash. You can imagine that if the number of variables increased, say for example 40 variables, you would need 40 lines of code to achieve the desired result.

To simplify this process i have created a simple snippet of code could be contained within a for loop, creating and setting all of these variables in only 3 lines of code. By using a for loop we can add or decrease the amount of variables increased by changing just one variable.

Efficient use of code

/// set how many variables should be created
number_of_times = 5;
/// loop 5 times creating 5 variables with the value of 0
for (var i = 1; i<(number_of_times+1); i++) {
_global["slide"+i] = 0;
}

The Benefits

This makes it much easier to increase or decrease the amount of variables created, simply change “number_of_times = 13″ to a greater number.
If for example we wanted to rename the varaibale from “variable” to “myVariable” we would ammend one line of code… not 5 as shown in the initial example.

This method can be applied in a variet of different ways helping you become more productive and efficent with your code.

I hope this simple little explanation helps someone out.

Posted in ActionScript & Flash | No Comments »

Actionscript 2 Shuffle an Array

Monday, September 28th, 2009

Using arrays in flash is always a handy and efficient way to work with variables etc. But some times its handy to be able to shuffle the results in the array, for example when creating a quiz to allow a random set of questions.

Using the code below (actionscript 2.0 but should still work in 3.0) we create an array and trace the results.

The Code

Array.prototype.shuffle=function(){
   for(i=0;i<this.length;i++){
      var tempvar=this[i];
      var ranVar=random(this.length);
      this[i]=this[ranVar];
      this[ranVar]=tempvar;
   }
}

myArray=["flash","dreamweaver","illustrator","in-design","photoshop"];
myArray.shuffle();
trace(myArray);

This would return a trace with answers such as:
‘dreamweaver, flash, in-design, photoshop, illustrator’

Posted in ActionScript & Flash | No Comments »

Actionscript 2 – Flip Movieclip

Monday, September 28th, 2009

Simple short and sweet. If you have ever needed to flip a movieclip so its infact facing the other way use the following:

mymovieclipname._xscale=-100;

Posted in ActionScript & Flash | No Comments »

Pausing a Flash movie with actionscript 2

Wednesday, February 25th, 2009

Quite often you don’t want to drag your time line out say 60 seconds just to add a delay to your animation or event.

There are many different ways to delay you flash movie from playing but the one that i have included below is one of the most simplest. Using the timer below you can set your time line to pause/stop (or delay) for a set period of time.

function freezeTime(howLong:Number);void{
stop();
var t:Timer = new Timer(howLong, 1);
t.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent);void{
play();
}
}
t.start('4000');
};

Tags: ,
Posted in ActionScript & Flash | No Comments »