timers... trying something funky

  • chopficaro
  • Novice
  • Novice
  • chopficaro
  • Posts: 30

Post 3+ Months Ago

im trying to work out how to do something really hard and i think the only way that i can do it is to pass a timer an argument. i want to create a class that moves buttons to the right on mouse over, and moves them back on roll out. but im trying to make a sort of easing effect in action script.

the last movement should be 1 pixel, the second to last move 2 pixels, the third to last be 3 pixels etc.

heres what i have in my head: the class will have a variable that stores the origional x coordinate and the desired resting coordinate for the button on roll over. on roll over, the script calculates the number of pixels between where it is and the destination, or on roll out, it would calculate the number of pixels from its current posision to the origional posision. then it would use that number to do the following calculation:
ACTIONSCRIPT Code: [ Select ]
while(numPix>countPix)
{
count++
countPix+=count
}
this.x=destinationX-countPix /*or + count pix depending on weather its moving to the right or left*/
while(count>0)
{
this.x+=count /*or -= if moving back*/
count--
/*timer would elapse here*/
}
 
  1. while(numPix>countPix)
  2. {
  3. count++
  4. countPix+=count
  5. }
  6. this.x=destinationX-countPix /*or + count pix depending on weather its moving to the right or left*/
  7. while(count>0)
  8. {
  9. this.x+=count /*or -= if moving back*/
  10. count--
  11. /*timer would elapse here*/
  12. }
  13.  

so u see, it counts the number of pixels it needs to move, then it counts the number of increments (count) it needs to move between timers for this sort of easing motion. i just dont know how to implement this in actionscript
Moderator Remark: Please wrap your script in [code][/code] tags
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

Well your doing that a really hard way. I would suggest looking into a prebuilt easing library like tweenlite that will do all of this for you.

If you really want to do this by hand you need to use the "onEnterFrame()" function that runs once per frame based on the frame rate.

I can't tell which version of Actionscript your using since yours isn't correct for either version 2 or 3. So I'll give you an example in AS2 since its a little easier to follow.

ACTIONSCRIPT Code: [ Select ]
function moveObject(targObject, targX){
   targObject.onEnterframe = function(){
      var currX = this._x;
      if(math.floor(currX) > targX){ // round so we don't try to move sub pixels.
         // you'll have to change the pluses and greater thans based on your start/end locations
         // move object half the distance to the target each frame (linear ease)
         targObject._x = (currX - targX)/2;
      }else{
         delete this.onEnterFrame; // delete the onEnterFrame so its now always using up ram
      }
   }
}
  1. function moveObject(targObject, targX){
  2.    targObject.onEnterframe = function(){
  3.       var currX = this._x;
  4.       if(math.floor(currX) > targX){ // round so we don't try to move sub pixels.
  5.          // you'll have to change the pluses and greater thans based on your start/end locations
  6.          // move object half the distance to the target each frame (linear ease)
  7.          targObject._x = (currX - targX)/2;
  8.       }else{
  9.          delete this.onEnterFrame; // delete the onEnterFrame so its now always using up ram
  10.       }
  11.    }
  12. }


I haven't tested that code but it should give you a starting point.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 13 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.