# hitTest problem (in game)

**URL:** https://forum.kirupa.com/t/hittest-problem-in-game/186620
**Category:** programming
**Created:** [April 22, 2006, 10:41pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620 "2006-04-22T22:41:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Johnsson](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnsson](https://forum.kirupa.com/u/Johnsson)
#### Post date: [April 22, 2006, 10:41pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/1 "2006-04-22T22:41:25Z")

</div>

Hi! could someone tell me how to make the character walk up the hill instead of through it!? I don’t want him to be able to walk through any part of the ground mc, from any direction…🅰

Hope some1 can help!!!:ne:

Here’s my game…😃

Link: [hem.bredband.net/ollij](http://hem.bredband.net/ollij)

---

<div class="post-metadata">

### Author: ![ricer](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@ricer](https://forum.kirupa.com/u/ricer)
#### Post date: [April 22, 2006, 10:42pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/2 "2006-04-22T22:42:47Z")

</div>

2 topics for that 0.o

---

<div class="post-metadata">

### Author: ![Johnsson](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnsson](https://forum.kirupa.com/u/Johnsson)
#### Post date: [April 22, 2006, 10:44pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/3 "2006-04-22T22:44:55Z")

</div>

sorry for duble posting!!!8-]

Didn’t think it worked the first time!

---

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [April 22, 2006, 11:15pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/4 "2006-04-22T23:15:31Z")

</div>

use a while+ hitTest, then you can just modify your ground with the bump… and ANY hill in the ground will work

```auto

while(ground.hitTest(char._x, char._y, true)){
char._y--;
}

```

😉

---

<div class="post-metadata">

### Author: ![Johnsson](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnsson](https://forum.kirupa.com/u/Johnsson)
#### Post date: [April 23, 2006, 12:03am UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/5 "2006-04-23T00:03:33Z")

</div>

I can’t get it to work!! :h:

where exactly am I going to put this as?  
:ko:

---

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [April 23, 2006, 12:46am UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/6 "2006-04-23T00:46:32Z")

</div>

whereever you have th characters current code

---

<div class="post-metadata">

### Author: ![Johnsson](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnsson](https://forum.kirupa.com/u/Johnsson)
#### Post date: [April 25, 2006, 6:34pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/7 "2006-04-25T18:34:35Z")

</div>

> [@nathan99](#):
>
> whereever you have th characters current code

It doesn’t work!  
What’s wrong?  
Can someone tell me what to do!?😉

Here’s my actionscript:

```php
 
onClipEvent (load) {
 jumping = false;
 falling = true;
 speed = 0;
 jump = 14;
 healthX = _root.health._x;
 scoreX = _root.score._x;
 Xpos = this._x;
 Ypos = this._y;
 maxmove = 15;
 _root.maxshoottime = 100;
}
onClipEvent (enterFrame) {
 _x = Xpos-_root._x;
 _root.score._x = scoreX-_root._x;
 _root.health._x = healthX-_root._x;
 if (!_root.ground.hitTest(this._x, this._y, true) && !jumping) {
  this._y += 6;
 }
 if (!_root.shooting) {
  _root.timer = 0;
  _root.mvsp = _xscale/20;
 }
 if (_root.dead) {
  this.gotoAndStop("dead");
 } else {
  speed *= .85;
  if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true)) {
   _root.health._x += speed;
   _root.score._x += speed;
   _root.icons._x += speed;
   _root.menu._x += speed;
   _root.playagain._x += speed;
   _root.mainmenu._x += speed;
   _root.BG._x += speed;
   _root.hell._x += speed;
   this._x += speed;
   _root._x -= speed;
  }
  if (speed>0) {
   dir = "right";
  } else if (speed<0) {
   dir = "left";
  }
  if (dir == "left" && !_root.rightblock.hitTest(this._x-20, this._y, true)) {
   _root.health._x += speed;
   _root.score._x += speed;
   _root.icons._x += speed;
   _root.menu._x += speed;
   _root.hell._x += speed;
   this._x += speed;
   _root._x -= speed;
  }
  if (Key.isDown(Key.LEFT)) {
   if (speed>-maxmove) {
    speed--;
   }
   this.gotoAndStop("run");
   this._xscale = -100;
  } else if (Key.isDown(Key.RIGHT)) {
   if (speed<maxmove) {
    speed++;
   }
   this._xscale = 100;
   this.gotoAndStop("run");
  } else if (Key.isDown(Key.CONTROL)) {
   this.gotoAndStop("attack");
   attacking = true;
   speed = 0;
  } else if (Key.isDown(Key.SPACE)) {
   if (_root.gotgun == true && !_root.shooting) {
    _root.attachMovie("bullet", "bulleter", 1, {_x:_root.char._x, _y:_root.char._y-25});
    _root.shooting = true;
    with (_root.bulleter) {
     onEnterFrame = function () {
      if (_root.timer>_root.maxshoottime) {
       _root.shooting = false;
       unloadMovie(this);
      }
      _root.timer++;
      _x += _root.mvsp;
     };
    }
    attacking = true;
    speed = 0;
    this.gotoAndStop("shoot");
   }
  } else if (speed<1 && speed>-1 && !attacking) {
   speed = 0;
   this.gotoAndStop("idle");
  }
  if (Key.isDown(Key.UP) && !jumping) {
   jumping = true;
  }
  if (jumping) {
   this.gotoAndStop("jump");
   this._y -= jump;
   jump -= .5;
   if (jump<0) {
    falling = true;
   }
   if (jump<-15) {
    jump = -15;
   }
  }
  if (falling) {
   this.gotoAndStop("fall");
  }
  if (Key.isDown(Key.DOWN)) {
   this.gotoAndStop("duck");
  }
  if (_root.ground.hitTest(this._x, this._y, true) && falling) {
   jump = 12;
   jumping = false;
   falling = false;
  }
 }
}
onClipEvent (keyUp) {
 if (Key.getCode() == Key.CONTROL) {
  attacking = false;
 }
}

```

---

<div class="post-metadata">

### Author: ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)
#### Post date: [April 25, 2006, 7:58pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/8 "2006-04-25T19:58:08Z")

</div>

Tried the code use it under the onClipEvent(enterFrame){

```auto

while(_root.ground.hitTest(_root.char._x, _root.char._y, true)){
_root.char._y--;
}

```

try that

---

<div class="post-metadata">

### Author: ![Johnsson](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnsson](https://forum.kirupa.com/u/Johnsson)
#### Post date: [April 25, 2006, 8:08pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/9 "2006-04-25T20:08:06Z")

</div>

> [@joppe](#):
>
> Tried the code use it under the onClipEvent(enterFrame){
> 
> ActionScript Code:
> 
> [FONT=Courier New][LEFT]\</p\>  
> \<p\>while[COLOR=#000000]([/COLOR][COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]ground[/COLOR].[COLOR=#0000ff]hitTest[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]char[/COLOR].[COLOR=#0000ff]\_x[/COLOR], [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]char[/COLOR].[COLOR=#0000ff]\_y[/COLOR], [COLOR=#000000] **true** [/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]\</p\>  
> \<p\>\_root.[COLOR=#000080]char[/COLOR].\_y–;\</p\>  
> \<p\>[COLOR=#000000]}[/COLOR]\</p\>  
> \<p\>
> 
> [/LEFT]
> 
> [/FONT]
> 
> try that

Thanks! It finaly works!!!  
:stare: =) :drool: :asian: :stare:

---

<div class="post-metadata">

### Author: ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)
#### Post date: [April 25, 2006, 8:15pm UTC](https://forum.kirupa.com/t/hittest-problem-in-game/186620/10 "2006-04-25T20:15:17Z")

</div>

you got nice graphics for that game by the way :thumb:
