PDA

View Full Version : move no further


Bunzing
06-04-2002, 11:20 AM
on stage is an an instance called 'test' AND an instance called 'wall'

now i want to move the test instance around by reacting to the arrow-keys. but as soon as it hits the wall (left, right top or bottom...) it must move no further. Now, the moving part is not the problem, the wall part is....
how do i prevent it from moving 'over' the wall?

(I know little of actionscript, so please be clear in your answer..)

sfa
06-04-2002, 11:51 AM
What code are you using exactly? You need to use if statements to check where it has arrived like -

if (test._x < 500){ //wisth if movie
test._x += 5;
}


SFA

Bunzing
06-04-2002, 12:03 PM
i'm not sure what you mean by this,

the wall surrounds the 'test'-instance completely...
like this:

-----
| T |
-----


now...the T is the 'test'-instance
and the lines are the wall, but it is not a straight line, so i can't just say: if _X<10 then _x=10................

sfa
06-04-2002, 12:07 PM
Use an if statement when moving right checking the limit on the right side with an if statement and the same thing for the rest of the walls.

SFA

Bunzing
06-04-2002, 12:14 PM
well...that's the whole trouble acctually....
i can't use that! some part of the left wall x=10
but the further you go down the wall, the higher x is....
so i need another method....

sfa
06-04-2002, 12:24 PM
Then you will need to use hittest or else you can work out a formula that checks the _y of the MC and calculate the _x boundary. Think about the wall like a right angled triangle....if you know the length of the adjacent line (_y) you can calculate the length of the opposite side using tangent. Then use this computation in the if statement.

SFA

Bunzing
06-04-2002, 12:26 PM
well....hitTest sounds like a cool option, but how exactly do i use that?

sfa
06-04-2002, 12:35 PM
You need to turn the wall into an MC use an if statemnt checking the hitTest.

if the MCs are overlapping - test.hitTest(wall._x,wall._y) will return a value of true.

SFA

Bunzing
06-04-2002, 12:39 PM
wall is allready a MC, and so is test.
and where do i put this code? on the 'wall', the 'test' or the main movie?

Abelius
06-04-2002, 12:46 PM
I think you should put the code in such a way that it tests the "hit-test" condition everytime a key is used, left, right, up or down... :)

sfa
06-04-2002, 12:46 PM
Use the hitTest inside an if statement around the code you already out to move the MC around.(the keypress thingy)

SFA

Bunzing
06-04-2002, 12:55 PM
okay...let's assume i am a total moron...(which i am)

onClipEvent (load) {
speed = 10;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
_rotation = 270;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_rotation = 90;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
_yscale = (_yscale-4);
_xscale = (_xscale-4);
if ((_yscale<=40)) {
(_yscale=40);
}
if ((_xscale<=40)) {
(_xscale=40);
}
_rotation = 0;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
_yscale = (_yscale+4);
_xscale = (_xscale+4);
_rotation = 180;
if ((_yscale>=150)) {
(_yscale=150);
if ((_xscale>=150)) {
(_xscale=150);
}
}
}
}


where and how would this hitTest-thingy come?

sfa
06-04-2002, 01:25 PM
Try something like this -

if (Key.isDown(Key.LEFT) & test.hitTest(leftwall._x,leftwall._y)ne true ) {
_x -= speed;
}

SFA

Bunzing
06-05-2002, 05:29 AM
now i am a little confused. i dont have an instance called leftwall, and the leftwall is the same instance as the rightwall. and what is 'ne true'? should that be 'not true'?:confused:

sfa
06-05-2002, 07:21 AM
Just have the walls as seperate MCs with different names and ne means not equal to,

SFA

Bunzing
06-05-2002, 07:24 AM
but, is there a possibility to do it with the wall as one MC?

i have got this now:

if (Key.isDown(Key.LEFT) & test.hitTest(leftwall._x,leftwall._y) ne true) {
_x -= speed;
_rotation = 270;
}
that doesn't work...it still ignores the wall....

sfa
06-05-2002, 08:25 AM
You have to use seperate walls because otherwise the MC will get stuck to a wall. If your MC is overlapping the left wall for example and all the walls are one MC you want be able to move right either.

SFA

Bunzing
06-05-2002, 08:30 AM
but i have tried this now:

if (Key.isDown(Key.LEFT) & test.hitTest(leftwall._x,leftwall._y) ne true) {
_x -= speed;
_rotation = 270;
}

and it doesn't work...it still goes through the wall...i have split up the wall into 2 MCs...leftwall and rightwall.....doesn't do the trick....am i missing the point?

sfa
06-05-2002, 11:05 AM
Have a look at this file

SFA

sfa
06-05-2002, 11:13 AM
Now replace the code with this and see if you get the idea -


onClipEvent(enterFrame){
if (Key.isDown(Key.LEFT) & _root.leftwall.hitTest(_root.box._x,_root.box._y+_ root.box._height,true) == false) {
_x -= 10;
}
}


the "true" in the hitTest makes it work on the pixels of the mc instead of the boundaries.

SFA

Bunzing
06-05-2002, 11:18 AM
thanx...that's a step in the right direction.
except:

my wall is all serated and not straight like yours....
now it stops at the (non-visible) edge of the square that surrounds the MC....(my wall is kinda triangular-shaped)

check out my file:

(thank you for your patience!)

Bunzing
06-05-2002, 11:29 AM
THANX! it is acctually working! :)
i am still a bit puzzled tho...

(_root.test._x, _root.test._y+_root.test._height, true)

why is _height involved here?

sfa
06-05-2002, 01:34 PM
the _y of the mc will be the position of the top right corner in this case, once the place that would hit the left wall would be the bottom left(if the wall is slanting from left to right) so you need to get the position of there = _y(top left) + height.

You will need to change this depending on with wall and point of the box you need to calculate from. Ex if the right wall is slanting from right to left you need to calculate the _x + width and the _y + height.

SFA