PDA

View Full Version : Easy AS question too...


FLASH-MX
01-04-2008, 12:45 PM
Whats the simplest way of having an object completely stop when hitTesting with a wall?

Ive looked all over the best/worst hitTest tutorials but all of them are for having something slow down or gradually stop when hitting a wall, but im sure theres gotta be a way of just making an absoulte boundary.

I tried setting the walkspeed=0 once it hit a wall... but it just got stuck :C

I know, pretty n00b

here's the code on my char.

onClipEvent (load) {
walkSpeed = 10;
stop();
}

onClipEvent (enterFrame) {

if (Key.isDown(Key.UP)) {
play();

_y -= walkSpeed;
}
if (Key.isDown(key.RIGHT)) {
play();

_x += walkSpeed;
}
if (Key.isDown(key.LEFT)) {
play();

_x -= walkSpeed;
}
if (Key.isDown(key.DOWN)) {
play();

_y += walkSpeed;
}
if (!Key.isDown(key.UP) & !Key.isDown(key.DOWN) & !Key.isDown(key.LEFT) & !Key.isDown(key.RIGHT)) {
stop();
}
if (this.hitTest(_root.wall)) {
walkSpeed = 0;
}
}

arkhan
01-04-2008, 01:00 PM
well..without changing much of what you got..you could simply undo what you just moved if you hitTest.
can be done in 2 ways:

1st you can save the x and y position when you dont hit the wall, and when you do you replace the current x and y with the ones you saved.

2nd having a variable with a number or string..you decide, and when key is down, you set it to something like "1" or "left"..and when nothing is pressed you can use 0 or "none"..just so you know what happened last..
once you hit the wall you just see what was the last key pressed and undo it
(case up you add movespeed to y)

fisrt one is much simpler and recomended..just wanted to note the other one for some reason..

FLASH-MX
01-04-2008, 01:07 PM
ahh I see, here let me try that and ill get back to you, thanks for the reply!

Matt
01-04-2008, 01:08 PM
I prefer to use flag hittests to check before you hit the wall.

if(wall.hitTest(this._x+(this._width/2),this._y,true)){
this._x-=walkSpeed
}
if(wall.hitTest(this._x-(this._width/2),this._y,true)){
this._x+=walkSpeed
}
if(wall.hitTest(this._x,this._y+(this._height/2),true)){
this._y-=walkSpeed
}
if(wall.hitTest(this._x,this._y-(this._height/2),true)){
this._y+=walkSpeed
}

FLASH-MX
01-04-2008, 01:17 PM
Right so I did that, is this what you meant?

onClipEvent (load) {
walkSpeed = 10;
stop();
}

onClipEvent (enterFrame) {

if (Key.isDown(Key.UP)) {
play();

_y -= walkSpeed;
}
if (Key.isDown(key.RIGHT)) {
play();

_x += walkSpeed;
}
if (Key.isDown(key.LEFT)) {
play();

_x -= walkSpeed;
}
if (Key.isDown(key.DOWN)) {
play();

_y += walkSpeed;
}
if (!Key.isDown(key.UP) & !Key.isDown(key.DOWN) & !Key.isDown(key.LEFT) & !Key.isDown(key.RIGHT)) {
stop();
}
if (this.hitTest(_root.wall)) {
this._y = 195.0;
this._x = 116.0;
}
}

FLASH-MX
01-04-2008, 01:47 PM
Right so I did that, is this what you meant?

onClipEvent (load) {
walkSpeed = 10;
stop();
}

onClipEvent (enterFrame) {

if (Key.isDown(Key.UP)) {
play();

_y -= walkSpeed;
}
if (Key.isDown(key.RIGHT)) {
play();

_x += walkSpeed;
}
if (Key.isDown(key.LEFT)) {
play();

_x -= walkSpeed;
}
if (Key.isDown(key.DOWN)) {
play();

_y += walkSpeed;
}
if (!Key.isDown(key.UP) & !Key.isDown(key.DOWN) & !Key.isDown(key.LEFT) & !Key.isDown(key.RIGHT)) {
stop();
}
if (this.hitTest(_root.wall)) {
this._y = 195.0;
this._x = 116.0;
}
}

Edit: thanks matt, that worked out nicely

MOOGLEFIGHTER
01-04-2008, 02:30 PM
The best type of walls are the kind of walls you never come close to touching.

Logic
01-04-2008, 02:47 PM
In easy, short terms, you should predict when you are going to hit a wall and prevent the move from being possible/stop it before it happens. You can compensate for any awkward space that might be remaining between the moving MovieClip/Sprite by doing something like this (the condition shoud be changed but you get the idea):

// simple AS3 which can be ported to AS1 or 2
if (aboutToHitWallToTheRight)
{
// "this" refers to the MovieClip or Sprite
this.x = wallMovieClipOrSprite.x - this.width;
}

That simple code moves the active MovieClip/Sprite to being flush with a wall to the right instead of awkwardly stopping you short. You can repeat this condition for all 4 cardinal directions or any other direction you want.