Be a Supporter!

my code is perfect but doesnt work

  • 380 Views
  • 7 Replies
New Topic Respond to this Topic
MCSHIMMY
MCSHIMMY
  • Member since: Oct. 10, 2011
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
my code is perfect but doesnt work Oct. 21st, 2011 @ 06:41 PM Reply

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

boney-man
boney-man
  • Member since: Jul. 24, 2005
  • Offline.
Forum Stats
Member
Level 11
Musician
Response to my code is perfect but doesnt work Oct. 21st, 2011 @ 06:52 PM Reply

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
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to my code is perfect but doesnt work Oct. 21st, 2011 @ 07:08 PM Reply

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.

ronk
ronk
  • Member since: Dec. 23, 2007
  • Offline.
Forum Stats
Member
Level 13
Programmer
Response to my code is perfect but doesnt work Oct. 24th, 2011 @ 12:39 AM Reply

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

yup

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to my code is perfect but doesnt work Oct. 24th, 2011 @ 04:44 AM Reply

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
MCSHIMMY
MCSHIMMY
  • Member since: Oct. 10, 2011
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to my code is perfect but doesnt work Oct. 24th, 2011 @ 06:36 AM Reply

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.

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to my code is perfect but doesnt work Oct. 24th, 2011 @ 07:00 AM Reply

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 ^

MCSHIMMY
MCSHIMMY
  • Member since: Oct. 10, 2011
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to my code is perfect but doesnt work Oct. 24th, 2011 @ 08:12 PM Reply

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.