开发者

Flash CS5 Moving an object around in a circle 1 degree at a time

开发者 https://www.devze.com 2023-03-25 10:59 出处:网络
I have an object called point that I can move around the screen with my mouse. This object has a vector mapped through it (_v1) using the center of the stage and \"point\"s x,y location in order to dr

I have an object called point that I can move around the screen with my mouse. This object has a vector mapped through it (_v1) using the center of the stage and "point"s x,y location in order to draw the vector (note all of this is working fine). My problem is occurring when I set up was a button that simply sets a value to true if you press it. The idea behind this button is that when pressed, whatever location "point" is at, it will move 1 degree clockwise in a circle around the center of the stage (this is all happening in the frameHandler so I can keep clicking the button to make it continue around the stage). The code I am using is based in an if statement listed below:

if(clockwisePressed) {
    var v1angle:Number = _v1.angle;
    v1angle++; //increase the angle by 1 degree
    if(v1angle>180) {
        v1angle -= 360;
    }
    //Now just move the crosshair and get it ready to be redrawn
    point.x = centerX - _v1.m * Math.cos(v1angle);
    point.y = centerY - _v1.m * Math.sin(v1angle);
    clockwisePressed = false; //remove button pressed boolean
}

So what is happening here is that clockwisePressed has been set to true so this fires off. My vector (_v1) is set up to tell me the angle (already converted from radians to degrees) but it's a read-only value. So I assign it (_v1.angle) to v1angle so I can mess with it and increase it by 1 so I'm moving clockwise around the center of the stage. Then I just check to make sure that it falls into a range between -180 and 180 (if I trace _v1.angle around the stage, 0 degrees is to the left, 90 degrees is straight up, 180 degrees is to the right, and -90 is straight down). Now using the formula I derived from this thread but converted to work with 0 degrees pointing to the left and the value for Y going up the further down the stage you go, I came up with

x = centerX - radius * cos(angle)
y = centerY - radius * sin(angle)

Now when I make a sample chart on paper and use these values on my calculator, this works like a charm. If I plug in an angle, the radius, and center I get a point right on my circle I draw on a sheet of graph paper. However, when I actually try this in flash, the "point" jumps all over the stage. (Note that _v1.m is just the magnitude of the vector between the center of the stage and "point" which is the same as the radius).

So I trace everything and the values for cos and sin don't seem to be coming out right. For example, if my angle is -1.9581 and I take the cos of that on my calculator I get something along the lines of 0.9994 but flash traces Math.cos(angle) to be -0.3777. Isn't using Math.cos(-1.9581) the same as putting -1.9581 into your calculator and hitting cos?

So in the case of my example above, my point.x went from 139.95 to 261.3. Quite a big jump for what was supposed to be 1 degree. So I'm at a loss he开发者_如何学Pythonre as to what is causing this code to fail so drastically. Am I doing something wrong here?

UPDATE: If anyone who searches this topic is interested, below is the corrected code that works:

if(clockwisePressed) {
    var v1RadAngle:Number = Math.atan2(_v1.vy, _v1.vx); //get angle in radians
    v1RadAngle += 0.0174; //increase the angle by ~1 degree more or less
    //Now just move the crosshair and get it ready to be redrawn
    point.x = centerX - _v1.m * Math.cos(v1RadAngle); //_v1.m = radius
    point.y = centerY - _v1.m * Math.sin(v1RadAngle);
    clockwisePressed = false; //remove button pressed boolean
}


The trig functions in Flash expect input in radians, but you're using degrees. You must convert your degree value to radians before using it:

var angle:Number = -1.9581;
var radAngle:Number = angle * Math.PI / 180;
trace(Math.cos(radAngle)); // 0.9994160815411552
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号