Code Library
From SUwiki
|
Use this page to find bits of code you can use in your own project. Just fill in your own variable and instance names. You can also add your own code snippets here, with a short explanation for how to use them.
Button Code
Jump to a different frame.
buttoninstancename.onRelease = function(){
gotoAndPlay(frame number or "frame label");
}
Another version that just moves on to the next frame.
buttoninstancename.onRelease = function(){
gotoAndPlay.nextFrame;
}
Moving a movie clip using 4 keyboard keys
Speed can be changed to make you character move more or less on each key press. instancename is the name of the instance you would like to move.
var speed = 5;
onEnterFrame=function() {
if(Key.isDown(Key.RIGHT)){
instancename._x += speed;
}
if(Key.isDown(Key.LEFT)){
instancename._x -= speed;
}
if(Key.isDown(Key.UP)){
instancename._y += speed;
}
if(Key.isDown(Key.DOWN)){
instancename._y -= speed;
}
}
Download an example of "Moving a movie clip using keyboard keys" here: Media:Movement_Example.fla
Dragging a movie clip
This is to drag a movie clip using the mouse
instancenameofmovieclip.onPress = function() {
this.startDrag();
}
To stop dragging
instancenameofmovieclip.onRelease = function() {
this.stopDrag();
}
Making a Preloader Screen
For very large games, it can be helpful to add a loading screen to increase initial play speed. Loading screens can also just be for looks.
--Daniel 15:33, 1 June 2009 (EDT)
You will need a dynamic text field on the stage and you will need to give it an instance name.
_root.onEnterFrame = function(){
percent = Math.round((this.getBytesLoaded()/this.getBytesTotal())*100);
instancenameoftextfield.text = percent + "%";
if (percent == 100){
delete _root.onEnterFrame; // stops the onEnterFrame loop
gotoAndPlay("FirstGameFrame");
}
}
Sample fla: File:Daniel Preloader.fla
Making a count-down timer
When you want something to count-down, like say a bomb or something else that would induce stress on the game player, this is the coding you'd use. Note: you will need a dynamic text box for this.
--Jbohon 12:01, 3 April 2009 (EDT)
stop();
timervariablename = 10(value you want the time to start at); instancenameoftextbox.text = timervariablename; countDown = function () { timervariablename-1; instancenameoftextbox.text = tm; if (timervariablename == 0) { clearInterval(timer); gotoAndPlay("frame label" or frame number want to go to when the timer reaches 0); } };
timer = setInterval(countDown, 1000);
Making a movieclip invisible with code
This way is one of the ways to make a movie clip invisible. Just fill in the instance names of the movie clip you want to hide
onClipEvent(load){
instancename._visible= false;
}
then if you want to make it visible later on just change it to true
instancename._visible= true;
--White452 17:17, 23 March 2009 (EDT)
Creating a "pop-up window"
This is to make a window-like box appear on the screen (for Menu buttons or the like), where button is the button to call the window, window is the movie clip that is called, and window_close is the button that calls the window away.
--Jbohon 19:56, 16 March 2009 (EDT)
button._x = xxx.xx; button._y = yyy.yy;
buttoninstancename.onRelease = function(){ window._x = x2; window._y = y2; button._x = 4000; }
window.closebuttoneinstancename.onRelease = function(){ window._x = 5000; window._y = 5000; button._x = xxx.xx; }
Changing the cursor
Sometimes you need a different cursor, like a cross-hair of a gun, for your game. Here's the coding to do that.
Note: Put the coding on the cursor movie clip
--Jbohon 09:35, 15 April 2009 (EDT)
onClipEvent (load) {
startDrag ("_root.instancenameofcursor, true);
Mouse.hide();
}
Code to toggle a boolean (true/false) variable
var tog:Boolean = false;
buttoninstancename.onRelease = function(){
if(tog)
tog = false;
else
tog = true;
