I have this code – below – that I borrowed for learning purposes, and I want to tweek it so that I can make the ball appear to be bouncing towards the back of a room. My idea is to increase the variable ‘radius’ after it hits a side wall while at the same time decreasing its scale. This would make the ball increasingly smaller, as well as decreasing the area it has to move around in, giving the appearance of bouncing within a room.
An FLA is also attached to this post. Thanks!
onClipEvent(load) {
var dragging:Boolean = false;
var vel:Object = { x: 0, y: 0 };
var pos:Object = { x: _x, y: _y };
var old:Object = { x: _x, y: _y };
var radius:Number = this._width / 2;
var movie:Object = { width: 400, height: 300 };
}
onClipEvent(enterFrame) {
if( !dragging ) {
vel.y += _root.gravity;
pos.x += vel.x;
pos.y += vel.y;
if( pos.y + radius > movie.height ) {
pos.y = movie.height - radius;
vel.y *= -_root.restitution;
vel.x *= _root.friction;
}
if( pos.x + radius > movie.width ) {
pos.x = movie.width - radius;
vel.x *= -_root.restitution;
//decrease scale and increase radius here?
}
if( pos.x < radius ) {
pos.x = radius;
vel.x *= -_root.restitution;
//decrease scale and increase radius here?
}
if( pos.y < radius ) {
pos.y = radius;
vel.y *= -_root.restitution;
}
_x = pos.x;
_y = pos.y;
} else {
old.x = pos.x;
old.y = pos.y;
pos.x = _x;
pos.y = _y;
vel.x = ( pos.x - old.x ) / 2;
vel.y = ( pos.y - old.y ) / 2;
}
}