conditional expression question...

  • tykomusic
  • Newbie
  • Newbie
  • User avatar
  • Posts: 6
  • Loc: Estes Park, CO USA

Post 3+ Months Ago

how would I express the following condition?

Code: [ Select ]
if (myVar approaches 0) {
 substatement1;
  }else{
   substatement2;
}
  1. if (myVar approaches 0) {
  2.  substatement1;
  3.   }else{
  4.    substatement2;
  5. }


My goal is to invoke substatement1 if the numeric value of myVar is getting smaller...if not, then invoke substatement2...so how would I write the "condition" part of the conditional?

thanks,

tykomusic
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

The only way to see which direction an integer is heading is to compare it to the value it was before you incremented or decremented it.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

If you only need to know what the outcome was & don't need the modified value of myvar in further application flow, do any math involved inside the conditional & test the variables value against the anonymous result returned by the equation.
Code: [ Select ]
// Declare variable
var myvar:Number = 10;

// Setup either a positive or negative number randomly
var polarity:Number = Math.round(Math.random()) === 1 ? 1 : -1;

// Test the value of 'myvar' against an equation using the random polarity
if(myvar > myvar+polarity){
    trace('Went down \nmyvar : '+ myvar + '\npolarity : '+polarity);
}else{
    trace('Went up \nmyvar : '+ myvar + '\npolarity : '+polarity);
}

// Note: For AS1, remove ':Number' from the variables
  1. // Declare variable
  2. var myvar:Number = 10;
  3. // Setup either a positive or negative number randomly
  4. var polarity:Number = Math.round(Math.random()) === 1 ? 1 : -1;
  5. // Test the value of 'myvar' against an equation using the random polarity
  6. if(myvar > myvar+polarity){
  7.     trace('Went down \nmyvar : '+ myvar + '\npolarity : '+polarity);
  8. }else{
  9.     trace('Went up \nmyvar : '+ myvar + '\npolarity : '+polarity);
  10. }
  11. // Note: For AS1, remove ':Number' from the variables


If you need the use the modified value of myvar in further application flow, hold reference to the variable before doing any math, then compare the reference against the modified myvar as I believe lostinbeta insinuated.
Code: [ Select ]
// Declare the 'spotlight var'
var myvar:Number = 10;
// Declare a variable to hold reference before equations
var myvarref:Number;
// Stack for testing purposes
var mystack:Number = 5;
// Declare the loop iterator beforehand
var i:Number = 0;

for(; i<10; i++){
    // Save the reference
    myvarref = myvar;

    // Modify myvar randomly
    myvar += Math.round(Math.random()) === 1 ? 1 : -1;

    // Test conditional, do somthing involving the variable
    if(myvar > myvarref){
        mystack += myvar;
    }else{
        mystack += myvarref;
    }
}
trace(mystack);

// Note: For AS1, remove ':Number' from the variables
  1. // Declare the 'spotlight var'
  2. var myvar:Number = 10;
  3. // Declare a variable to hold reference before equations
  4. var myvarref:Number;
  5. // Stack for testing purposes
  6. var mystack:Number = 5;
  7. // Declare the loop iterator beforehand
  8. var i:Number = 0;
  9. for(; i<10; i++){
  10.     // Save the reference
  11.     myvarref = myvar;
  12.     // Modify myvar randomly
  13.     myvar += Math.round(Math.random()) === 1 ? 1 : -1;
  14.     // Test conditional, do somthing involving the variable
  15.     if(myvar > myvarref){
  16.         mystack += myvar;
  17.     }else{
  18.         mystack += myvarref;
  19.     }
  20. }
  21. trace(mystack);
  22. // Note: For AS1, remove ':Number' from the variables
  • tykomusic
  • Newbie
  • Newbie
  • User avatar
  • Posts: 6
  • Loc: Estes Park, CO USA

Post 3+ Months Ago

So,

That is a lot to chew on...where do my substatements fall into the picture?

I don't mean to sound like an idiot, but that is a hell of a "condition" for what I am trying to accomplish...some more context from me might offer simpler suggestions from folks...don't get me wrong I sincerely appreciate the help...some of the high level math stuff, mixed with condensed scripting, tends to confuse me as I am still pretty green at actionscript... :oops:

Code: [ Select ]
//some vars to work with here...
var hSW = .5*Stage.width;
var hSH = .5*Stage.width;
var xM = .xmouse;
var yM = .ymouse;
var aVG = ((Math.abs(hSW - xM)) +(Math.abs(hsH - yM))/2;
//in theory...of course my paths might be all wrong...please check,
//the aVG var gives me a larger number as I get farther away from the
//center of the stage with my mouse...I want to monitor this number and
//cause MCs to move to the left or right depending on if aVG is getting
//bigger or smaller...my guess is that there is more than one way to
//make this work...I am probably doing it the hard way...

//I was thinking of a function that would be on each MC's timeline...and
//this is where the original proposition came into play...
_root.onEnterFrame = function() {
  if (aVG gets bigger) {
    this._x = this._x + (aVG/10); //moves the MC right, across the stage
  }else{
    this._x = this._x - (aVG/10); //moves the MC left, across the stage
  }
};
//I want the effect to take place the whole time the view has this movie
//loaded in the window...so about 4 MCs are always moving right or left
//when the mouse is moving...
  1. //some vars to work with here...
  2. var hSW = .5*Stage.width;
  3. var hSH = .5*Stage.width;
  4. var xM = .xmouse;
  5. var yM = .ymouse;
  6. var aVG = ((Math.abs(hSW - xM)) +(Math.abs(hsH - yM))/2;
  7. //in theory...of course my paths might be all wrong...please check,
  8. //the aVG var gives me a larger number as I get farther away from the
  9. //center of the stage with my mouse...I want to monitor this number and
  10. //cause MCs to move to the left or right depending on if aVG is getting
  11. //bigger or smaller...my guess is that there is more than one way to
  12. //make this work...I am probably doing it the hard way...
  13. //I was thinking of a function that would be on each MC's timeline...and
  14. //this is where the original proposition came into play...
  15. _root.onEnterFrame = function() {
  16.   if (aVG gets bigger) {
  17.     this._x = this._x + (aVG/10); //moves the MC right, across the stage
  18.   }else{
  19.     this._x = this._x - (aVG/10); //moves the MC left, across the stage
  20.   }
  21. };
  22. //I want the effect to take place the whole time the view has this movie
  23. //loaded in the window...so about 4 MCs are always moving right or left
  24. //when the mouse is moving...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 3 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
 
cron
 

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