consolidating functions

  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Professor,

I need a lesson in how to consolidate code, and pass arguments to functions. The following is my code for two separate instances of the same volume knob movieClip.

The knobs work just as I intended, but I would guess it could be just as easily done without separate functions for each. http://cluttered.flvservers.com/knobTest.html


Without writing a custom class, how do I rewrite the following using only one turn() function, and one convertValue() function?

I understand that this is very basic; your patience is much appreciated.

this.knob_mc.nobBase.gain_mc.onPress=function(){
turning1=true;
mouseStart=_root._ymouse;
knobStart=this._rotation;
}
this.knob_mc.nobBase.gain_mc.onRelease=this.knob_mc.nobBase.gain_mc.onReleaseOutside=OFF

this.knob2_mc.nobBase.gain_mc.onPress=function(){
turning2=true;
mouseStart=_root._ymouse;
knobStart=this._rotation;
}
this.knob2_mc.nobBase.gain_mc.onRelease=this.knob2_mc.nobBase.gain_mc.onReleaseOutside=OFF


function turn1():Void{
myMouse=_root._ymouse;
var knobRotation:Number=(myMouse-mouseStart)*9;
this.knob_mc.nobBase.gain_mc._rotation=knobStart+knobRotation;

_root.yoyo_txt.text=knob_mc.nobBase.gain_mc._rotation;
_root.convt_txt.text=convertValue();
}
function turn2():Void{
myMouse=_root._ymouse;
var knobRotation:Number=(myMouse-mouseStart)*9;
this.knob2_mc.nobBase.gain_mc._rotation=knobStart+knobRotation;

_root.yoyo2_txt.text=knob2_mc.nobBase.gain_mc._rotation;
_root.convt2_txt.text=convertValue2();
}


function convertValue(){
Q=knob_mc.nobBase.gain_mc._rotation;

if(Q>0){
n=(Q+180)-180;
}else if(Q<0){
n=(Q+360);
}
F=Math.floor(n*.3703)-3
if(F>=100){
F=100;
}
return(F);
}

function convertValue2(){
Q=knob2_mc.nobBase.gain_mc._rotation;

if(Q>0){
n=(Q+180)-180;
}else if(Q<0){
n=(Q+360);
}
F=Math.floor(n*.3703)-3
if(F>=100){
F=100;
}
return(F);
}

this.onMouseMove=function(){
if(turning1){
turn1();
updateAfterEvent();
}
if(turning2){
turn2();
updateAfterEvent();
}
}


function OFF(){
turning1=false;
turning2=false;
delete this.onMouseMove;
}
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

don't you know there's a [code tag in this forum that will permit a better view of your code?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

anyway, to make just a single function with parameter.... what are you supposed to pass as parameters?
let's see ... you have 2 knobs, you want one function... uhmmm, it's hard ... don't know what parameter must be passed. Oh, wait, i have an idea, i wonder if it works. Ah, and btw, look how a [code tag looks
Code: [ Select ]
function turn(target_mc):Void{
myMouse=_root._ymouse;
var knobRotation:Number=(myMouse-mouseStart)*9;
target_mc.gain_mc._rotation=knobStart+knobRotation;
_root.yoyo_txt.text=target_mc.nobBase.gain_mc._rotation;
_root.convt_txt.text=convertValue(target_mc);
}

function convertValue(target_mc){
Q=target_mc.nobBase.gain_mc._rotation;
if(Q>0){
n=(Q+180)-180;
}else if(Q<0){
n=(Q+360);
}
F=Math.floor(n*.3703)-3
if(F>=100){
F=100;
}
return(F);
}

//then ...
this.onMouseMove=function(){

if(turning1){
turn(knob_mc);
updateAfterEvent();
}

if(turning2){
turn(knob2_mc);
updateAfterEvent();
}

}
  1. function turn(target_mc):Void{
  2. myMouse=_root._ymouse;
  3. var knobRotation:Number=(myMouse-mouseStart)*9;
  4. target_mc.gain_mc._rotation=knobStart+knobRotation;
  5. _root.yoyo_txt.text=target_mc.nobBase.gain_mc._rotation;
  6. _root.convt_txt.text=convertValue(target_mc);
  7. }
  8. function convertValue(target_mc){
  9. Q=target_mc.nobBase.gain_mc._rotation;
  10. if(Q>0){
  11. n=(Q+180)-180;
  12. }else if(Q<0){
  13. n=(Q+360);
  14. }
  15. F=Math.floor(n*.3703)-3
  16. if(F>=100){
  17. F=100;
  18. }
  19. return(F);
  20. }
  21. //then ...
  22. this.onMouseMove=function(){
  23. if(turning1){
  24. turn(knob_mc);
  25. updateAfterEvent();
  26. }
  27. if(turning2){
  28. turn(knob2_mc);
  29. updateAfterEvent();
  30. }
  31. }

p.s.: don't get upset cos of my little sarcasm, but i'm kind of tired now :D
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

thanks professor;

Code: [ Select ]

this is in the code tag, it's nice in here
  1. this is in the code tag, it's nice in here


Thank You (I dont mind the sarcasm) 8)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 12 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.