The first thing you do when you are exploring new territory in programming is prototyping. You will throw it away (you always throw away the first draft though), but you will learn from it.
I’ve been struggling with how to make my ball move, and at one point decided not to even tackle it, but to instead rely on an open source engine. But then I wouldn’t learn as much, and while I still might go in that direction, I have prototyped a system to “get the ball rolling”.
It’s acting funny though…
Here it is:
http://corsair.cs.iupui.edu:18041/chomperStomp/games/clink.html
You can make the ball roll with the arrow keys upon clicking on the flash movie if you so desire. Like I said though, it is acting a little funny. I have a few ideas about what I can do to fix it, but it’s fun to have this first iteration, and I thought I’d share it.
The method I used was to have three points that I measure against along the bottom of the ball. One to the left, one in the center, and one on the right. Dependant upon how many of these are touching “ground” will decide how the ball behaves (which way it rolls). I’ll paste the code in below, but remember that I wrote this code in 15 minutes and that it is only prototype code and thus very very crappy.
var leftSide = this._x - this._width/2;
var rightSide = this._x + this._width/2;
var bottomSide = this._y + this._width/2;
var topSide = this._y + this._width/2;
if(_root.theFloor.hitTest(leftSide, bottomSide, true)){
if(_root.theFloor.hitTest(rightSide, bottomSide, true)){
if(dy > 0){
dy = 0;
}
trace("flat surface");
}else if(_root.theFloor.hitTest(this._x, bottomSide, true)){
trace("sloped down to the right");
dx += _root.gravity;
dy += _root.gravity;
}else{
if(dx < 0){
dx = (dx * .96) * -1;
}
}
}else if(_root.theFloor.hitTest(rightSide, bottomSide, true)){
if(_root.theFloor.hitTest(leftSide, bottomSide, true)){
if(dy > 0){
dy = 0;
}
trace("flat surface");
trace(dy);
}else if(_root.theFloor.hitTest(this._x, bottomSide, true)){
trace("sloped down to the left");
dx -= _root.gravity;
dy += _root.gravity;
}else{
if(dx > 0){
dx = (dx * .96) * -1;
}
}
}else{
dy += _root.gravity;
}
if(_root.theFloor.hitTest(this._x, bottomSide, true)){
if(_root.theFloor.hitTest(this._x, bottomSide - 1, true)){
dy -= 1;
}
}











Welcome to WordPress! I forgot to add your new RSS feed, but I’m back.
I just got the ball to fly through the bottom right wall. My ball is gone.
Thanks for the welcome! I actually think I will have two separate blogs. A techie blog (this one) and a personal blog (without my name attached) somewhere else…
Yep, your ball has serious problems. If you’re lucky you can get your ball to jump up in the air by persistently banging the wall. I’ve gotten it to go up really high by banging it into the wall.
My algorithm is problematic in many many ways, and I have actually already improved it some since this prototype version. I know I am missing something really big here though, and my method is definitely not correct (or at all complete). It *kind* of works, but there has to be another way…