I have this rubberband ball that I’ve made interactive, when it bounces it also rotates to try and give a more realistic feel to it. When it’s rolling across the “table” it seems to rotate from the center, but it’s rolling like a square, as if it’s a square. I masked the image I’m using, but it still rotates like a square. I’m not sure if it’s my gravity code or what the problem is.
import flash.filters.BlurFilter;
var blurShadow:BlurFilter;
init();
function init() {
dragging = false;
friction = 0.96;
bounce = -0.7;
gravity = 2;
top = 0;
left = 100;
bottom = Stage.height - 200;
right = Stage.width + 120;
ball._x = Stage.width / 2
ball._y = Stage.height / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
}
ball.onPress = function() {
oldX = ball._x;
oldY = ball._y;
dragging = true;
ball.startDrag();
}
ball.onRelease = function() {
dragging = false;
ball.stopDrag();
}
ball.onReleaseOutside = function() {
dragging = false;
ball.stopDrag();
}
function onEnterFrame(Void):Void {
trace(Math.round(vy*1000)/1000);
if (vy >= -0.806 && vy <= -0.7) {
gravity = 0;
}
else {
gravity = 2;
}
mcShadow._x = ball._x;
mcShadow._width = (mcShadow._y - ball._y) / 1.2;
blurAmt = mcShadow._width / 8;
mcShadow.filters = [new flash.filters.BlurFilter(blurAmt, blurAmt, 3)];
if (!dragging) {
vy += gravity;
vx *= friction;
vy *= friction;
if (vx < .001 && vx > -.001) {
vx = 0;
}
if (vy < .001 && vy > -.001) {
vy = 0;
}
ball._x += vx;
ball._y += vy;
ball._rotation += vx;
if (ball._x + ball._width / 2 > right) {
ball._x = right - ball._width / 2;
vx *= bounce;
}
else if (ball._x - ball._width / 2 < left) {
ball._x = left + ball._width / 2;
vx *= bounce;
}
if (ball._y + ball._height / 2 > bottom) {
ball._y = bottom - ball._height / 2;
vy *= bounce;
}
else if (ball._y - ball._height / 2 < top) {
ball._y = top + ball._height / 2;
vy *= bounce;
}
}
else {
vx = ball._x - oldX;
vy = ball._y - oldY;
oldX = ball._x;
oldY = ball._y;
}
}