Groups | Blog | Home
all groups > flash actionscript > april 2004 >

flash actionscript : Same name but different imput parameter functions?


MrVisualize
4/18/2004 8:29:59 PM
One of the things I find most useful while programming in other languages is
to be able to change the the input parameters of functions without changing the
functions name. This is to be able to make a function polymorphic.


for instance

function makeSpaceship(){
speed = 100;
ammo = 500;
}

and then

function makeSpaceship(speed:Number, ammo:Number){
this.speed = speed;
this.ammo = ammo;
}

this way one can for instance set default values when no parameters are input,
one can put in different types of parameters and make the function translate it
into its internal logic etc.

Is there a way to make Action Script 2.0 do this?
mandingo
4/18/2004 10:55:54 PM
Then why not put the logic straight into the function?? If the parameters
exist, it uses them otherwise it uses your defaults.

function makeSpaceship(speed:Number, ammo:Number){
defaultSpeed = 100;
defaultAmmo = 500;
if(!speed){
this.speed = defaultSpeed ;
}else{
this.speed = speed;
}
if(!ammo){
this.ammo = defaultAmmo;
}else{
this.ammo = ammo;
}
}


Hope that helps
cheers
MrVisualize
4/19/2004 2:19:23 PM
Thanx! :)

I'll try that. But obviously from your code, its not as clean and pretty....
needing an if-sentence... and if youd like to have more than just to ways of
putting parameters you might end up with long messy and nested if-else stuff.

It'll do the job but it will wont' be pretty.


AddThis Social Bookmark Button