ForumsProgramming Forum[HELP] hitTest / Instance

4 4718
maffegozer
offline
maffegozer
64 posts
Nomad

Hello,

I have been working on a simple (Very simple) game.
Basically: You play a ball, and have to get it through a maze.
Now there are walls, and if you hit them, you are supposed to be resetted to your original position.

So I had this code:

if (_root.wall.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_x = 250;
_y = 177;
}


Now my problem: To change the environment, i have several instances all named 'wall'.
But only the one I first placed works with the hittest. I can just move through the other ones.

Can anyone help me with this?

  • 4 Replies
PixelSmash
offline
PixelSmash
566 posts
Nomad

If you have multiple walls setup like this, you'll need to look through all the walls and hittest them... you'll need to rename them to wall1, wall2 etc then ofcourse.

PixelSmash
offline
PixelSmash
566 posts
Nomad

I meant loop through all the walls, sorry

maffegozer
offline
maffegozer
64 posts
Nomad

Hmm, Ok.
Well I changed some things and added alot of code.
Came out with this:

onClipEvent (load) {
yspeed = 0;
xspeed = 0;
wind = 0.00;
power = 0.65;
gravity = 0.25;
upconstant = 0.75;
friction = 0.99;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
xspeed = xspeed-power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed = xspeed+power;
}
if (Key.isDown(Key.UP)) {
yspeed = yspeed-power*upconstant;
}
if (Key.isDown(Key.DOWN)) {
yspeed = yspeed+power*upconstant;
}
xspeed = (xspeed+wind)*friction;
yspeed = yspeed+gravity;
if (xspeed>15) {
xspeed = 15;
}
if (xspeed<-15) {
xspeed = -15;
}
if (yspeed>15) {
yspeed = 15;
}
if (yspeed<-15) {
yspeed = -15;
}
_root.wallleft._y -= yspeed;
_root.wallleft._x -= xspeed;
_root.wallright._y -= yspeed;
_root.wallright._x -= xspeed;
_root.wall2._y -= yspeed;
_root.wall2._x -= xspeed;
_root.wall3._y -= yspeed;
_root.wall3._x -= xspeed;
_root.wall4._y -= yspeed;
_root.wall4._x -= xspeed;
_root.wall5._y -= yspeed;
_root.wall5._x -= xspeed;
_root.wall6._y -= yspeed;
_root.wall6._x -= xspeed;
_root.wall7._y -= yspeed;
_root.wall7._x -= xspeed;
_root.wall8._y -= yspeed;
_root.wall8._x -= xspeed;
_root.wall9._y -= yspeed;
_root.wall9._x -= xspeed;
if (_root.wallleft.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wallleft._x = 208.50;
_root.wallleft._y = -3.50;
_x = 269;
_y = 27;
}
if (_root.wallright.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wallright._x = 352.55;
_root.wallright._y = -2.50;
_x = 269;
_y = 27;
}
if (_root.wall2.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall2._x = 208.50;
_root.wall2._y = 385.35;
_x = 269;
_y = 27;
}
if (_root.wall3.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall3._x = 352.65;
_root.wall3._y = 395.30;
_x = 269;
_y = 27;
}
if (_root.wall4.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall4._x = 250.05;
_root.wall4._y = 542.50;
_x = 269;
_y = 27;
}
if (_root.wall5.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall5._x = 379.50;
_root.wall5._y = 496;
_x = 269;
_y = 27;
}
if (_root.wall6.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall6._x = 355.35;
_root.wall6._y = 854.30;
_x = 269;
_y = 27;
}
if (_root.wall7.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall7._x = 490.60;
_root.wall7._y = 616.10;
_x = 269;
_y = 27;
}
if (_root.wall8.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall8._x = 781;
_root.wall8._y = 852.05;
_x = 269;
_y = 27;
}
if (_root.wall9.hitTest(this)) {
xspeed = 0;
yspeed = 0;
_root.wall9._x = 670;
_root.wall9._y = 615;
_x = 269;
_y = 27;
}
_root.speedtext.text = (xspeed+yspeed)/2
}


I move the walls instead of the character.
This is something I got from a tutorial.
Don't know why actually.

Now the problem is fixed.
The hitTest works, but not how it is supposed.
Another problem has come up:
Instead of doing nothing, it now hits the wall, and the wall appears totally on a different location....
If you don't know what I mean, I can show it on teamviewer.

maffegozer
offline
maffegozer
64 posts
Nomad

Ha, fixed my problem.
Not bothering to tell how, hell of a work.

Showing 1-4 of 4