hitTests problem
- adminz
-
adminz
- Member since: Jul. 28, 2008
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
I have had a bad time with hitTests lately. I need some help with my character not falling through the wall. Here's the code:
onClipEvent(load){
speed = 10;
grav = 0;
}
onClipEvent(enterFrame) {
if(_root.Player.hitTest(_root.Wall)){
this._y -= 10;
this._x += 10;
}
_y += grav;
grav++;
if(Key.isDown(Key.RIGHT)){
_x += speed;
}
if(Key.isDown(Key.LEFT)){
_x -= speed;
}
}
The code in put in the character. Please help soon.
- Magicalmonkeyguy
-
Magicalmonkeyguy
- Member since: May. 11, 2007
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
use this instead
onClipEvent(load){
speed = 10;
grav = 0;
wall = _root.wall
}
onClipEvent(enterFrame) {
while(wall.hitTest(_x,_y,true)){
_y-=grav;
grav=0;
}
_y += grav;
grav++;
if(Key.isDown(Key.RIGHT)){
_x += speed;
}
if(Key.isDown(Key.LEFT)){
_x -= speed;
}
}
even this isnt the best and i wouldn't use it, but it is a start.
try it out, if you dont like the results, the ultimate platformer tutorial helps a lot.
wat
- Magicalmonkeyguy
-
Magicalmonkeyguy
- Member since: May. 11, 2007
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
this code works without any serious issues
onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 15;
var maxjump:Number = -30;
var onground: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)) {
onground = true;
} else {
onground = false;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.UP) && onground) {
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)) {
grav = 3;
}
}
just rename your wall instance name ground if you want to copy paste.
with this your ground can be any shape as long as its thick enough.
it also works with walls and ceilings.
wat
- Kart-Man
-
Kart-Man
- Member since: Jan. 7, 2007
- Offline.
-
- Forum Stats
- Member
- Level 27
- Blank Slate
Make sure that your player's instance name is "Player"; it is case-sensitive.
Oh, and using shapeFlag to check whether the right or left sides of the player are colliding with the wall:
if (_root.Wall.hitTest(_x+this._width/2, _y, true)) {
_x-=speed;
}
if (_root.Wall.hitTest(_x-this._width/2, _y, true)) {
_x+=speed;
}
Simply put, we are hitTesting a certain point of the player -- the coordinate is the _x coordinate PLUS half of the its width. That is the precise point where it is going to check for collisions. This goes for the opposite side as well, but SUBTRACT half of its width.
For more information, look at Actionscript Dictionary: MovieClip.hitTest.
postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN
- Kart-Man
-
Kart-Man
- Member since: Jan. 7, 2007
- Offline.
-
- Forum Stats
- Member
- Level 27
- Blank Slate
At 2/9/09 12:07 AM, Magicalmonkeyguy wrote: this code works without any serious issues
code
Ack, beat me to it. :P
postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN
- adminz
-
adminz
- Member since: Jul. 28, 2008
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
- Magicalmonkeyguy
-
Magicalmonkeyguy
- Member since: May. 11, 2007
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
At 2/9/09 12:18 AM, adminz wrote: Neither of those scripts did anything...
then you are doing something crazy wrong.
wat
- adminz
-
adminz
- Member since: Jul. 28, 2008
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
At 2/9/09 12:30 AM, Magicalmonkeyguy wrote:At 2/9/09 12:18 AM, adminz wrote: Neither of those scripts did anything...then you are doing something crazy wrong.
I put the exact coding in my player...I don't see whats wrong
onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 15;
var maxjump:Number = -30;
var onground: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)) {
onground = true;
} else {
onground = false;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.UP) && onground) {
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)) {
grav = 3;
}
}
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Make sure you have changed your 'wall' instance name to 'ground', as Magicalmonkeyguy said. If it still doesn't work, you'll have to be more specific about exactly HOW it's failing.
Here's a more basic hitTest code that works, but doesn't include jumping etc. It will only work for downfall, not sideways, but is relatively easily adapted.
onClipEvent (load) {
var ground:MovieClip=_root.ground;
var yvel:Number=0;
var gravity:Number=1;
var speed:Number=10;
}
onClipEvent (enterFrame) {
yvel+=gravity;
_y+=yvel;
if(ground.hitTest(this)){
while(ground.hitTest(this)){
_y-=0.1;
}
}
if(Key.isDown(Key.RIGHT)){_x+=speed;}
if(Key.isDown(Key.LEFT)){_x-=speed;}
}
- roxasreaper
-
roxasreaper
- Member since: Jul. 29, 2008
- Offline.
-
- Forum Stats
- Supporter
- Level 14
- Blank Slate
hey i have the same problem and non of these codes work for me either
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
The code works, download the fla at the bottom of my last post and test it.
- Kwing
-
Kwing
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 45
- Game Developer
If you made the walls a different Movie Clip than the floor, try this:
onClipEvent(load){
speed = 10;
grav = 0;
}
onClipEvent(enterFrame) {
if(_root.Player.hitTest(_root.Wall)&&Key.isDown(Key.LEFT)){
this._x += speed;
}
if(_root.Player.hitTest(_root.Wall)&&Key.isDown(Key.RIGHT)){
this._x -= speed;
}
_y += grav;
grav++;
if(Key.isDown(Key.RIGHT)){
_x += speed;
}
if(Key.isDown(Key.LEFT)){
_x -= speed;
}
} If I offer to help you in a post, PM me to get it. I often forget to revisit threads.
Want 180+ free PSP games? Try these links! - Flash - Homebrew (OFW)


