00:00
00:00
Newgrounds Background Image Theme

LOCKdev just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

my code is perfect but doesnt work

608 Views | 7 Replies
New Topic Respond to this Topic

So, this is basic code for moving right and left and jumping. By all definition it should also include wall collision, but...when i collide with a wall i go right through it. I can stand on it fine, i dont fall through the floor. no matter how high, so long as the floor is thick enough. However i can jump through the ceiling, and g through walls to the left and right. Please i need an expert to read through this code and tell me if they can see any flaws with it, because i cant. But it still doesnt work, any idea on how to make it work, what i am missing. The syntax is perfect...

AS2 by the way.

onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 7;
var maxJump:Number = -12;
var touchingGround:Boolean = false;

}
onClipEvent (enterFrame) {
_y += grav;
grav += gravity;
while (ground.hitTest(_x, _y, true)) {
_y -= gravity;
grav = 0;
}
if (ground.hitTest(_x, _y+5, true)) {
touchingGround = true;
} else {
touchingGround = false;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.UP) && touchingGround) {
grav = maxJump;
}
if (ground.hitTest(_x+(_width/2), _y-(_height/2), true)) {
_x -= speed;
}
if (ground.hitTest(_x-(_width/2), _y-(_height/2), true)) {
_x += speed;
}
if (ground.hitTest(_x, _y-(_height), true)) {
_y -= speed;
}
}

Response to my code is perfect but doesnt work 2011-10-21 18:52:14


To fix your ceiling issue:

if (ground.hitTest(_x, _y-(_height), true)) {
    _y += speed;
}

You should be increasing _y, not decreasing it; you had _y -= speed.


#1286129 // soundcloud.com/1shibumi

BBS Signature

Response to my code is perfect but doesnt work 2011-10-21 19:08:29


Without looking at your post:
"my code is perfect but doesnt work"

Well if your code does not work, its not perfect. Please be careful about the name you give to your topic, you dont want to look like an idiot.

Response to my code is perfect but doesnt work 2011-10-24 00:39:46


At 10/21/11 07:08 PM, Sandremss128 wrote:
you dont want to look like an idiot.

yup

Response to my code is perfect but doesnt work 2011-10-24 04:44:08


Weird definition of perfect.


#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);}

BBS Signature

Response to my code is perfect but doesnt work 2011-10-24 06:36:47


If everyone would take a step back and realise that it was the "wierd" title that drew you here in the first place you would realise that i named it such so as to draw people in. I just assumed that this community was more helpful than spiteful, thankyou, the three of you, for proving that wrong.

Response to my code is perfect but doesnt work 2011-10-24 07:00:29


At 10/24/11 06:36 AM, MCSHIMMY wrote: If everyone would take a step back and realise that it was the "wierd" title that drew you here in the first place you would realise that i named it such so as to draw people in. I just assumed that this community was more helpful than spiteful, thankyou, the three of you, for proving that wrong.

The title may have drawn people in, but it drew them in for the wrong reasons.

Now to actually answer your question, I think the problem may reside in what you're doing once you find a collision.

You're subtracting the x by the speed variable. Now, there's a lot of things that could be happening here, I'm guessing the character moves through walls slowly or something. If he simply ignores the wall, you might want to add in a:

trace('a')

To see if it's actually detecting collision. If it *is* detecting the collision, a good solid alternative to use would be a while loop. To ensure that as long as the player is touching the wall, he gets pushed away, and only once he is not touching the wall any more does flash player move onto the next frame. Something like:

while(collision){
Move_Player_Away
}

Pseudo Code ^

Response to my code is perfect but doesnt work 2011-10-24 20:12:36


Ok guys. So thankyou to the help given, the collision works. But only when the ground is moving. The entire premise of the game is that the ground moves, so when it does then the collision no longer works, and the more ground there is the more the character shoots through. And the faster the ground moves the glitchier it is. For example at a speed of 1, the character hits the edge of the ceiling, and it wil shoot him up and he will only go up to the next floor, or the floor above that. at a speed of 5 then he just jumps, goes through the ceiling, and goes all the way to the top without stopping. Please help.