Here are some miscellaneous functions for AS3, I made them myself, list :
- rangedIntRandom
//returns a random integer from rangeLow up to (and including) rangeHigh
- rangedFloatRandom
//returns a random float from rangeLow up to (and including) rangeHigh
- myAddEventSimple
//adds an event to an object, similar to myAddEvent in my another post, but this is the essential version
- movieClipAsButton
//treat a movie clip as a button.. moves to frame 2 when mouse over, to frame 3 when mouse pressed
- trim
//removes leading and trailing space or enter/return characters from a String
- randomOrderInArray
//random the elements in an array.. for example (0,1,3,4) becomes (1,4,0,3)
- myGetBounds
//return a detail coordinate of an object : xMin, yMin, xMax, yMax, xMid, yMid
- formatDecimal
//cuts the number count after comma in a float, as you like, you can set how many number after comma
- myRemoveMovieClip
//removes a movie clip which is attached using script,similar to my another post in this blog
- myAttachMovie
//attach a movie clip from the library, similar to my another post in this blog
Download the FLA file here : (I added the usage example in the file also)
http://www.mediafire.com/?85zxf7kbdawaxfl
Information about my Flash Game Projects (and Some Flash tutorials) and my Church Music composition project..
Friday, January 14, 2011
Saturday, January 8, 2011
AddEvent in AS3
In AS3, you can add various events to MovieClip(MC) or Button.. Some often-used events are :
function myAddEvent(targetMov, type:String, func:Function){
targetMov.addEventListener(type, func);
function targetRemoved(event){
//if a button or MC removed from stage, the event will be removed also
if(targetMov is SimpleButton){
//remove event for button sound here, if you wish
}
targetMov.removeEventListener(type, func);
targetMov.removeEventListener(Event.REMOVED_FROM_STAGE, targetRemoved);
targetMov = null;
}
if(targetMov is SimpleButton){
//add event for button sound here, if you wish
}
targetMov.addEventListener(Event.REMOVED_FROM_STAGE,targetRemoved);
}
I suggest you to use addEvent wrapped in a function, like above code..
Because, by default, an event of movieClip/button is NOT removed when the movieClip/button removed from stage.. This makes a garbage in the memory, and sometimes causing a NULL error..
- MouseEvent.MOUSE_UP (when you release your mouse after clicking MC/button)
- MouseEvent.MOUSE_DOWN (when you click your mouse on a MC/button)
- MouseEvent.MOUSE_OVER (when you hover your mouse on a MC/button)
- Event.ENTER_FRAME
- Control the movement by manipulating movieClip's properties : x, y, width, height, scaleX, scaleY, rotation, alpha, etc.
- Control AI in flash games. You can add various conditions, for example, you want the enemy to move toward the player.
function myAddEvent(targetMov, type:String, func:Function){
targetMov.addEventListener(type, func);
function targetRemoved(event){
//if a button or MC removed from stage, the event will be removed also
if(targetMov is SimpleButton){
//remove event for button sound here, if you wish
}
targetMov.removeEventListener(type, func);
targetMov.removeEventListener(Event.REMOVED_FROM_STAGE, targetRemoved);
targetMov = null;
}
if(targetMov is SimpleButton){
//add event for button sound here, if you wish
}
targetMov.addEventListener(Event.REMOVED_FROM_STAGE,targetRemoved);
}
Example how to use that :
First, make a button or movie clip, then give the instance name "instanceOfTheButton"..
Add these lines of code.. (don't forget to add above function also)
First, make a button or movie clip, then give the instance name "instanceOfTheButton"..
Add these lines of code.. (don't forget to add above function also)
function buttonClicked(event){
trace("I'm clicked");
}
myAddEvent(instanceOfTheButton, MouseEvent.MOUSE_DOWN, buttonClicked);
function buttonEnterFrame(event){
trace("It's happening every frame rate of the movie");
}
myAddEvent(instanceOfTheButton, Event.ENTER_FRAME, buttonEnterFrame);
I suggest you to use addEvent wrapped in a function, like above code..
Because, by default, an event of movieClip/button is NOT removed when the movieClip/button removed from stage.. This makes a garbage in the memory, and sometimes causing a NULL error..
Simple Way to AttachMovie for ActionScript 3.0 (AS3)
If you want to attach movie clip from library to stage in AS3, you should use addChild function. But I prefer the AS2 way to do that, which is called attachMovie.
And also, to remove a movie clip, you can use my AS2-like function to do that.
Here is a function to attach movie and remove movie in AS3 :
function myRemoveMovieClip(target){
if(target != null){
var parent1 = target.parent;
var instance = target.name;
if(parent1.getChildByName(instance) != null){
parent1.removeChild(parent1.getChildByName(instance));
target = null;
}
}
}
function myAttachMovie(tempMov:MovieClip,instance:String,parent1:MovieClip,pos:Object){
if(parent1.getChildByName(instance) != null){
myRemoveMovieClip(parent1.getChildByName(instance));
}
tempMov.name = instance;
parent1.addChild(tempMov);
for(var i in pos){
tempMov[i] = pos[i];
}
return tempMov;
}
Here is an example to use those functions :
myAttachMovie(new linkageNameInLibrary(), "instanceNameYouWant", MovieClip(root), {x:250, y:90});
myRemoveMovieClip(MovieClip(root).getChildByName("instanceNameYouWant"));
Note :
In AS3, to access an instance of movieClip/button in stage, which you attached using code, you must use getChildByName like above example.
If not attached using code, I mean if you place a movieClip/button directly and give it an instance name, you must access it like this : MovieClip(root).instanceNameYouWant
And also, to remove a movie clip, you can use my AS2-like function to do that.
Here is a function to attach movie and remove movie in AS3 :
function myRemoveMovieClip(target){
if(target != null){
var parent1 = target.parent;
var instance = target.name;
if(parent1.getChildByName(instance) != null){
parent1.removeChild(parent1.getChildByName(instance));
target = null;
}
}
}
function myAttachMovie(tempMov:MovieClip,instance:String,parent1:MovieClip,pos:Object){
if(parent1.getChildByName(instance) != null){
myRemoveMovieClip(parent1.getChildByName(instance));
}
tempMov.name = instance;
parent1.addChild(tempMov);
for(var i in pos){
tempMov[i] = pos[i];
}
return tempMov;
}
Here is an example to use those functions :
myAttachMovie(new linkageNameInLibrary(), "instanceNameYouWant", MovieClip(root), {x:250, y:90});
myRemoveMovieClip(MovieClip(root).getChildByName("instanceNameYouWant"));
Note :
In AS3, to access an instance of movieClip/button in stage, which you attached using code, you must use getChildByName like above example.
If not attached using code, I mean if you place a movieClip/button directly and give it an instance name, you must access it like this : MovieClip(root).instanceNameYouWant
Monday, January 3, 2011
My Flash Projects
Hello, I'm a Adobe Flash programmer, ActionScript 2 or 3..
Sorry I'm not very good in Artistic Designs, I'm a programmer ^^
These are two games I develop myself (Design+Program) :
- Quest for the Invisible (RPG-Action game)
It's my first game. I've studied the basic of Flash while making this game (v 1.0), until I get used to the code and developed it to the final version.
- Song of Ruination (RPG-Action Game, with Genetic Algorithm implementation on Boss)
Features of this game : 7 stage (dungeon), random enemy encounters, some skills (attack and support), items, character leveling, and many more.
Another game with copyright of Vaisaga-Project :
- Legend Wars - Guardians (under development)
- Myth Defense And Conquer (under development)
Defend your tower, attack and conquer enemy tower.. After conquering a town, you can choose to battle in another town.. The game's program is already finished by me, the design is still in progress..
There are more than 10 other Flash games which is used for final projects (skripsi), I programmed them myself (but my friends made the design), but I cannot tell u the list of the games in this blog. Email or PM me for further info.
This is my book, I wrote it myself, published by Andi Offset - Indonesia :
- Ultimate Game Design - Building RPG Games using Adobe Flash Action Script
for information, see http://m.bukukita.com/book.php?bookId=69775
I can develop Flash games for :
- Websites or offline games, as a single-player game. For now, I can develop offline single-player games only, but I will study how to make multi-player online games.. :)
Thanks :) Never hesitate to contact me..
Contact or PM me at : duke_dreadmoore@yahoo.com
Sorry I'm not very good in Artistic Designs, I'm a programmer ^^
These are two games I develop myself (Design+Program) :
- Quest for the Invisible (RPG-Action game)
It's my first game. I've studied the basic of Flash while making this game (v 1.0), until I get used to the code and developed it to the final version.
- Song of Ruination (RPG-Action Game, with Genetic Algorithm implementation on Boss)
Features of this game : 7 stage (dungeon), random enemy encounters, some skills (attack and support), items, character leveling, and many more.
Another game with copyright of Vaisaga-Project :
- Legend Wars - Guardians (under development)
- Myth Defense And Conquer (under development)
Defend your tower, attack and conquer enemy tower.. After conquering a town, you can choose to battle in another town.. The game's program is already finished by me, the design is still in progress..
There are more than 10 other Flash games which is used for final projects (skripsi), I programmed them myself (but my friends made the design), but I cannot tell u the list of the games in this blog. Email or PM me for further info.
This is my book, I wrote it myself, published by Andi Offset - Indonesia :
- Ultimate Game Design - Building RPG Games using Adobe Flash Action Script
for information, see http://m.bukukita.com/book.php?bookId=69775
I can develop Flash games for :
- Websites or offline games, as a single-player game. For now, I can develop offline single-player games only, but I will study how to make multi-player online games.. :)
Thanks :) Never hesitate to contact me..
Contact or PM me at : duke_dreadmoore@yahoo.com
Subscribe to:
Posts (Atom)