Jump to content
Search Community

TweenLite - can`t get right .rotation property from a rotating object

vvuk test
Moderator Tag

Recommended Posts

Hi, here is one I hope simple problem.

 

I`m using tweenlite to simple rotate some stuff and then to trigger some sound when wheel reach a certain position:

 

TweenLite.to(_myWheel, _speed, {rotation:_suspension + (_pieIndex * _pieAngle), immediateRender:true, useFrames:false, ease:easeFunct, onUpdate:playTickSound });

 

 

private function playTickSound ():void

{

trace (Math.round(_myWheel.rotation));

 

_isWhole = (Math.round(_myWheel.rotation) / Math.round(_pieAngle)) % 1 == 0;

 

if (_isWhole)

playSimpleSound();

}

 

To exactly calculate _isWhole = true, I need to have Math.round(_myWheel.rotation) exact position every time when onUpdate occurs.

 

Anyways, for some reason I don`t get those info right, so here is what I get when I try to trace it:

 

trace (Math.round(myWheel.rotation));

 

139

156

171

-176

-162

-144

-130

-119

-109

-98

-88

-78

-67

-54

-42

-30

-19

-9

1

11

21

30

39

51

63

76

86

95

103

111

118

126

135

143

150

157

165

172

179

-174

-168

-162

-155

-148

-140

-133

-127

-121

-116

-110

-104

-99

-93

-87

-81

-76

-70

-65

-61

-57

-52

-48

-43

-39

-34

-30

-27

-23

-20

-17

-14

-11

-8

-5

-2

0

2

4

5

7

9

11

12

14

15

16

17

18

19

20

21

21

22

22

22

23

Link to comment
Share on other sites

Hi,

Its hard to diagnose why those numbers are wrong without knowing what you expect.

 

Are you aware that rotation according to Flash is always a value between 0 and 180, either pos or negative

 

for instance:

 

mc.rotation = 361;
trace(mc.rotation); // 1
mc.rotation = 185;
trace(mc.rotation); // - 175
mc.rotation = -10;
trace(mc.rotation); // - 10

 

if that doesn't help please elaborate on what you are expecting from your traces. Its hard to know especially since the target rotation value in your code is a variable. Thanks

Link to comment
Share on other sites

Hi, thank you

 

I`m expecting those numbers to be 1,2,3,4,5,6,7,8,9.....180, -179,-178,-177,-176,-175..

 

How else I`m supposed to know when to trigger tick sound? What would you suggest?

Link to comment
Share on other sites

Aha, it sounds like you were expecting it to increment the value by one integer each update, but that isn't how the engine works (nor should it) because that could really bog down the processor unnecessarily. Let's take a simple example and say you've got a swf that's running at 30fps and you want to tween mc.x from 0 to 300 with a linear ease (again, to keep it simple). That means it should move 10 pixels per frame refresh (not 1). And if you were tweening from 0 to 3000, that'd be 100 pixels per frame.

 

Now let's say there are a lot of graphics flying around and the Flash Player can't render them at 30fps, dropping instead to 10fps for a little while during a complex sequence. The engine dynamically figures out EXACTLY what the values should be on each frame according to the elapsed time and total duration.

 

Now do you see why the values you saw tracing out were normal? You shouldn't write your code assuming that any particular value will be hit exactly during the tween (although the beginning and ending values will always be perfect). The Flash Player may decide to run a garbage collection routine mid-tween, causing a delay which in turn would make the values jump over a larger chunk (correctly).

 

Your code should probably look for when the value passes by some threshold. You could record the value on each onUpdate and compare it to the new one. Kinda like:

 

var prevVal:Number = (mc.rotation + 360) % 360; //normalize it from 0-360 so we don't need to worry about negative values
var triggerVal:Number = 55; //arbitrary angle that we want to sense when it goes past
TweenLite.to(mc, 5, {rotation:270, onUpdate:onTweenUpdate});
function onTweenUpdate() {
   var newVal:Number = (mc.rotation + 360) % 360;
   if ((newVal >= triggerVal && prevVal < triggerVal) || (newVal <= triggerVal && prevVal > triggerVal)) {
    trace("TRIGGER!");
   }
   prevVal = newVal;
}

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...