greeting all you wonderfull flash people herer at kirupa.
Ill get to the point fast, i realy need help whit som flash math.
It is a bouncing ball pong sort of thing i’m trying to make.
so on my stage i got… well,
a ball_mc and a block_mc and the problem is i have no clue how to get ball_mc to bounce off the block _mc, can anyon help me,
regards Dondeigo
herer is the fla
http://www.box.net/public/n0b2o4d6c8
and herer is the code
init();
function init() {
left = 0;
right = 140;
top = 0;
bottom = 380;
ball_mc.onEnterFrame = move;
ball_mc.onPress = drag;
block_mc.onPress = drag;
ball_mc.onRelease = ball_mc.onReleaseOutside=noDrag;
block_mc.onRelease = block_mc.onReleaseOutside=noDrag;
friction = .98; // constant drag on momentum
restitution = .99; // bounciness
grav = 1; // +Y force
smoosh=1,5 ;
squishLimit=30; // larger values=more squish
jiggle=.25; // larger values=more jiggle
}
function drag() {
this.startDrag();
this.dragging = true;
}
function noDrag() {
this.stopDrag();
this.dragging = false;
}
function move() {
// + JIGGLE
Xsquishforce+=(100-this._xscale);
Ysquishforce+=(100-this._yscale);
Xsquishforce*=jiggle;
Ysquishforce*=jiggle;
this._xscale+=Xsquishforce;
this._yscale+=Ysquishforce;
// - JIGGLE
if (!this.dragging) {
this.velY += grav;
this.velX *= friction;
this.velY *= friction;
this._x += this.velX;
this._y += this.velY;
if(ball_mc, hitTest(block_mc)) {
this.velY += grav;
this.velX *= friction;
this.velY *= friction;
this._x += this.velX;
this._y += this.velY;
collision=0; // init
if (ball_mc._x>right-(this._width/2)) {
collision=Math.abs(ball_mc.oldX-this._x);
if(collision<3){collision=0;}
ball_mc._yscale+=smoosh*collision;
ball_mc._xscale-=smoosh*collision;
ball_mc._x = right-(ball_mc._width/2);
ball_mc.velX *= -1*restitution;
}
if (ball_mc._x<left+(ball_mc._width/2)) {
collision=Math.abs(ball_mc.oldX-ball_mc._x);
if(collision<3){collision=0;}
ball_mc._yscale+=smoosh*collision;
ball_mc._xscale-=smoosh*collision;
ball_mc._x=left+(ball_mc._width/2);
ball_mc.velX *= -1*restitution;
}
if (this._y>bottom-(this._height/2)) {
collision=Math.abs(this.oldY-this._y);
if(collision<3){collision=0;}
this._xscale+=smoosh*collision;
this._yscale-=smoosh*collision;
this._y = bottom-(this._height/2);
this.velY *= -1*restitution;
}
if (this._y<top+(this._height/2)) {
collision=Math.abs(this.oldY-this._y);
if(collision<3){collision=0;}
this._xscale+=smoosh*collision;
this._yscale-=smoosh*collision;
this._y = top+(this._height/2);
this.velY *= -1*restitution;
}
if(this._xscale<100-squishLimit){this._xscale=100-squishLimit;}
else if(this._xscale>100+squishLimit){this._xscale=100+squishLimit;}
if(this._yscale<100-squishLimit){this._yscale=100-squishLimit;}
else if(this._yscale>100+squishLimit){this._yscale=100+squishLimit;}
} else {
hit="null";
this.velX = this._x-this.oldX;
this.velY = this._y-this.oldY;
}
this.oldX = this._x;
this.oldY = this._y;
}
}