Ground Collision Problem
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
I am running a little test, which includes, among other things, a ground collision and jumping feature. As of now I have a character that can run left and right while playing an animation, as well as jump to different platforms in different levels and so on. To do this i put all my ground MC's into an array and cycle the collision code through them in a for loop.
My problem is that for every new platform in the array, the character sinks lower and lower through them. The last MC in the array he rests perfectly on, while each previous one he sinks progressively lower, no matter their coordinates on-screen.
I'm very perplexed as to why this is happening, especially since the only working one is the last one in the array, however I'm sure it's just some kind of logic error I just can't see. I would greatly appreciate any help that anyone could provide.
Here is the code as of right now -
var i:Number;
var up:Boolean = false;
var down:Boolean = false;
var right:Boolean = false;
var left:Boolean = false;
var aKey:Boolean = false;
var sKey:Boolean = false;
var dKey:Boolean = false;
var xSpeed:Number = 0;
var xAcl:Number = 8;
var xLimit:Number = 12;
var xDecay:Number = 0.6;
var gravity:Number = 15;
var gravityMaximum:Number = 15;
var groundCollision:Boolean = false;
var groundArray:Array = new Array(ground, ground2, ground3);
function keyDowns(event:KeyboardEvent) {
if (event.keyCode == 38) {
up = true;
}
if (event.keyCode == 40) {
down = true;
}
if (event.keyCode == 39) {
right = true;
}
if (event.keyCode == 37) {
left = true;
}
if (event.keyCode == 65) {
aKey = true;
}
if (event.keyCode == 83) {
sKey = true;
}
if (event.keyCode == 68) {
dKey = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDowns);
function keyUps(event:KeyboardEvent) {
if (event.keyCode == 38) {
event.keyCode = 0;
up = false;
}
if (event.keyCode == 40) {
event.keyCode = 0;
down = false;
}
if (event.keyCode == 39) {
event.keyCode = 0;
right = false;
}
if (event.keyCode == 37) {
event.keyCode = 0;
left = false;
}
if (event.keyCode == 65) {
event.keyCode = 0;
aKey = false;
}
if (event.keyCode == 83) {
event.keyCode = 0;
sKey = false;
}
if (event.keyCode == 68) {
event.keyCode = 0;
dKey = false;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
function action(Event) {
for (i = 0; i < groundArray.length; i++) {
if (gravity <= gravityMaximum - 0.1) {
up = false;
}
if (up == true && groundCollision == true) {
gravity *= -1;
} else if (up == false) {
gravity = gravity;
}
gravity += 1;
player.y += gravity;
groundCollision = false;
if (player.y > groundArray[i].y
&& player.y < groundArray[i].y + player.height + groundArray[i].height
&& player.x <= groundArray[i].x + (groundArray[i].width/2) + (player.width/2)
&& player.x >= groundArray[i].x - (groundArray[i].width/2) - (player.width/2)) {
player.y = groundArray[i].y;
gravity = gravityMaximum;
groundCollision = true;
}
}
/*if (gravity <= gravityMaximum - 0.1) {
up = false;
}
if (up == true && groundCollision == true) {
gravity *= -1;
} else if (up == false) {
gravity = gravity;
}
gravity += 1;
player.y += gravity;
groundCollision = false;*/
if (right == true && left == false && xSpeed < 0) {
player.scaleX = -1.0;
player.gotoAndPlay(21);
xSpeed += xAcl;
} else if (right == true && left == false) {
if (player.currentFrame == 1 || player.currentFrame >= 20) {
player.scaleX = 1.0;
player.gotoAndPlay(2);
xSpeed += xAcl;
}
} else if (left == true && right == false && xSpeed > 0) {
player.scaleX = 1.0;
player.gotoAndPlay(21);
xSpeed -= xAcl;
} else if (left == true && right == false) {
if (player.currentFrame == 1 || player.currentFrame >= 20) {
player.scaleX = -1.0;
player.gotoAndPlay(2);
xSpeed -= xAcl;
}
} else if (right == false && left == false) {
player.gotoAndPlay(1);
xSpeed *= xDecay;
}
if (xSpeed > xLimit) {
xSpeed = xLimit;
} else if (xSpeed < -xLimit) {
xSpeed = -xLimit;
}
player.x += xSpeed;
}
stage.addEventListener(Event.ENTER_FRAME, action);
Note: The large section with the multiple and's that detects if the player is touching the MC are indented mainly for easier reading, and the same problem occurs weather they are indented or not.
Also, the commented part outside of the for loop is where I tried to put the gravity code outside the loop, to see if it was causing the problem. Instead, My character can jump 3 times higher, and he sinks through all platforms to the same depth.
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Redshift
-
Redshift
- Member since: Feb. 12, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Programmer
You should probably take out this stuff in the for loop:
gravity += 1;
player.y += gravity;
And also...
gravity = gravity;
Wtf?
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
- AdmittingZero
-
AdmittingZero
- Member since: Mar. 21, 2010
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
Lol, I think he wanted to basically say "Keep gravity the same".
I agree with Nano on how to fix it too.
MSN/Email-> kevin.stubbs@live.com -Need artists!
The 9th Legion
(Looking for somebody to create the site for $$$).
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
No effect with taking those two lines out, and yeah, the gravity = gravity was just keep it the same, even though it would probably do the same without that line.
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Rustygames
-
Rustygames
- Member since: May. 7, 2005
- Offline.
-
- Forum Stats
- Member
- Level 19
- Programmer
gravity = gravity huh?
Hey you should also do stage = stage just to make sure the stage stays the same too
- Matt, Rustyarcade.com
- Sam
-
Sam
- Member since: Oct. 1, 2005
- Offline.
-
- Forum Stats
- Moderator
- Level 19
- Programmer
At 8/26/10 07:29 PM, Rustygames wrote: gravity = gravity huh?
Hey you should also do stage = stage just to make sure the stage stays the same too
Laughing.
- Redshift
-
Redshift
- Member since: Feb. 12, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Programmer
At 8/26/10 07:29 PM, Rustygames wrote: gravity = gravity huh?
Hey you should also do stage = stage just to make sure the stage stays the same too
*gasp* But stage is read only! I guess we'll have to deal with stage randomly changing on its own accord...
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Kevin
-
Kevin
- Member since: Jan. 5, 2008
- Offline.
-
- Forum Stats
- Member
- Level 36
- Melancholy
At 8/26/10 07:29 PM, Rustygames wrote: gravity = gravity huh?
Hey you should also do stage = stage just to make sure the stage stays the same too
You forgot to tell him to declare variables twice so the computer really remembers them
var penis:Number = 69; //You got that?
var penis:Number = 69; //Are you sure computer?

