Forum Topic: [AS2] hitTest Displaced

(181 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

Archon68

Reply To Post Reply & Quote

Posted at: 6/29/09 06:37 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

I'm making an overhead game with a scrolling map.

Here's my code, on the "player" instance. Obstacles are named "wall1", "wall2", etc. The game and player are in a movieclip, covered by an interface, which is the line you see across the middle of the screenshot below. The entire thing is in a movieclip on the _root layer.

onClipEvent (load) {
	var speed = 5;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		_parent._x += speed;
		this._x -= speed;
	}
	if (Key.isDown(Key.RIGHT)) {
		_parent._x -= speed;
		this._x += speed;
	}
	if (Key.isDown(Key.UP)) {
		_parent._y += speed;
		this._y -= speed;
	}
	if (Key.isDown(Key.DOWN)) {
		_parent._y -= speed;
		this._y += speed;
	}
	for (i in this._parent) {
		if (_parent[i]._name.indexOf("wall") != -1) {
			while (_parent[i].hitTest(this._x, this._y+this._height/2, true)) {
				_parent._y+=speed;
				this._y-=speed;
			}
			while (_parent[i].hitTest(this._x, this._y-this._height/2, true)) {
				_parent._y-=speed;
				this._y+=speed;
			}
			while (_parent[i].hitTest(this._x+this._width/2, this._y, true)) {
				_parent._x+=speed;
				this._x-=speed;
			}
			while (_parent[i].hitTest(this._x-this._width/2, this._y, true)) {
				_parent._x-=speed;
				this._x+=speed;
			}
		}
	}
}

The problem is, the ball is being stopped in an odd location (see below). As I walk around, there are "ghosts" of my barriers and walls, displaced a few hundred pixels from the real movieClips.

Screenshot:

[AS2] hitTest Displaced

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Archon68

Reply To Post Reply & Quote

Posted at: 6/29/09 06:40 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

.FLA: http://spamtheweb.com/ul/upload/290609/6 7235_Overhead_Help.fla

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/29/09 08:23 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

I spent probably, 5-10 minutes trying to figure this out. I've got no idea, I havn't done collision detection in over a year and I really dislike it.

If you and no one else can figure it out, then I would try putting everything on the top level and try hittesting again, and then just rewrite the scrolling code.

Mibbygames.com
panterA

BBS Signature

None

Archon68

Reply To Post Reply & Quote

Posted at: 6/29/09 08:28 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

At 6/29/09 08:23 PM, Woadraiders wrote: If you and no one else can figure it out, then I would try putting everything on the top level and try hittesting again, and then just rewrite the scrolling code.

I actually did that about an hour ago, but still nothing...

I code in AS2, in case I forgot to mention it.

BBS Signature

None

polym

Reply To Post Reply & Quote

Posted at: 6/29/09 10:22 PM

polym LIGHT LEVEL 10

Sign-Up: 10/02/07

Posts: 805

That's because of the this.-y + this.height/2 code you have...

it's displaced by half of the height of the circle


None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/29/09 10:55 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

No, that's not right, its displaced far more than that.

Mibbygames.com
panterA

BBS Signature

None

Andy-Parker

Reply To Post Reply & Quote

Posted at: 6/29/09 11:09 PM

Andy-Parker DARK LEVEL 22

Sign-Up: 05/21/02

Posts: 1,160

At 6/29/09 10:55 PM, Woadraiders wrote: No, that's not right, its displaced far more than that.

while (_parent[i].hitTest(this._x+this._width/
2, this._y, true)) {

im not entirely sure what this is referring to. but if its not on the top layer that will be the problem. hitTest takes coordinates relative to the stage, regardless of what is being tested. however this._x and ._y will be relative to whatever this's parent is, and any parents above that. assuming none of the parents have been rotated, inverted, or stretched, you can fix it by going.

while (_parent[i].hitTest(this._x+this._parent ._x+this._width/2, this._y+this._parent._y, true)) {

I've covered wars, ya know.


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/30/09 08:30 AM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

At 6/29/09 11:09 PM, Andy-Parker wrote:
At 6/29/09 10:55 PM, Woadraiders wrote: No, that's not right, its displaced far more than that.
while (_parent[i].hitTest(this._x+this._width/
2, this._y, true)) {

im not entirely sure what this is referring to. but if its not on the top layer that will be the problem. hitTest takes coordinates relative to the stage, regardless of what is being tested. however this._x and ._y will be relative to whatever this's parent is, and any parents above that. assuming none of the parents have been rotated, inverted, or stretched, you can fix it by going.

while (_parent[i].hitTest(this._x+this._parent ._x+this._width/2, this._y+this._parent._y, true)) {

That's really odd how hitTesting works. The code you gave me doesn't work, however. (And yes, to those people who lurk around help topics looking to flame, I did manipulate it into each of my while() actions). Is it because I have the game nested inside of two movieclips? Should I add another set of "displacers"?

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Archon68

Reply To Post Reply & Quote

Posted at: 7/1/09 07:06 AM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

Bump.

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Andy-Parker

Reply To Post Reply & Quote

Posted at: 7/1/09 11:17 AM

Andy-Parker DARK LEVEL 22

Sign-Up: 05/21/02

Posts: 1,160

At 6/30/09 08:30 AM, Archon68 wrote: That's really odd how hitTesting works. The code you gave me doesn't work, however. (And yes, to those people who lurk around help topics looking to flame, I did manipulate it into each of my while() actions). Is it because I have the game nested inside of two movieclips? Should I add another set of "displacers"?

might be worth a try. all i can tell you for sure is that hitTest takes coordinates relative to stage.

I've covered wars, ya know.


None

Archon68

Reply To Post Reply & Quote

Posted at: 7/2/09 09:45 AM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,482

At 7/1/09 11:17 AM, Andy-Parker wrote:
At 6/30/09 08:30 AM, Archon68 wrote: That's really odd how hitTesting works. The code you gave me doesn't work, however. (And yes, to those people who lurk around help topics looking to flame, I did manipulate it into each of my while() actions). Is it because I have the game nested inside of two movieclips? Should I add another set of "displacers"?
might be worth a try. all i can tell you for sure is that hitTest takes coordinates relative to stage.

But wait, both movieclips I want to check for hitTests with are in the same movieclip. They should have the same relative position, shouldn't they?

I code in AS2, in case I forgot to mention it.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 05:35 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!