i got a character that moves up down left right but i want him to stop when he hits the wall... well ive managed doing that but now when he touch the wall he stays stopped and cant move.
please help me!
ThankZ
heres the fla. so you understand.
Printable View
i got a character that moves up down left right but i want him to stop when he hits the wall... well ive managed doing that but now when he touch the wall he stays stopped and cant move.
please help me!
ThankZ
heres the fla. so you understand.
there are a ton of free scripts on this forum to work out these sort of things. Please dont pm me randomly unless you ahve a specific question for me or relating to my work.
Chris
SORRY!!:(
but cant u just give me the link cuz i couldnt find anything
If iam guessing right, you must be using a while loop, which is getting stuck in a loop infinitly, try to figure out the error in that code, Iam only guessing, aight.
sounds more like his hittest stops the character rather than bounces, and he has one central hittest, so once you hit a wall, thats it. simplest solution is un your if(hittest) code add a backup script if he hits a wall, like
_x-=xSpeed;
_y-=ySpeed;
of course depending on how you calculate x,y speeds, that muight not back up the character to previous position, but thats the idea
Im not using any loop code im using:Quote:
adit_ya_sharma wrote:
If iam guessing right, you must be using a while loop, which is getting stuck in a loop infinitly, try to figure out the error in that code, Iam only guessing, aight.
or something like that on all movements up down right left.Code:if(Key.isDown(Key.RIGHT)&&!_root.hero.hitTest(walls)){
_root.hero._x-=movespeed;}
and flashl!ght u were right about when the hero touch the wall he cant move anymore but i didnt understand wut im supposed to about it.
THANKZ GUYS:D
btw check the fla. file
heh sry about the code mistype
okay, here's the deal...
well first of all, your hittest is:
_root.hero.hitTest{walls) ???
theres a { where there should be a (
please copy paste your actual code
additionally, that hitTest will not work if your walls are more than a single rectangle
secondly, according to your conditional setup, heres what happens:
frame 1
1) player hits right arrow
2) player is not hitting wall, so...
3) ...player moves right, into the wall
frame 2
4) player still pressing right arrow
5) player is in wall because of last frame, so collision detect does not allow move
now that you are in the wall, you can never move again. makes sense? there are lots of ways to fix(change) your collision detection, and lots of tuts and threads explaining.
heres a ultra simple example file i made for people just getting started, demostrating:
- radial movement
- basic math physics
- basic collision detection
- basic projectiles
- basic enemy turrets with look-at AI
HTH
Thanks alot flashl!ght that helped alot!
THX:D :D :D
that file u gave me helped alot with the ai and the fire but still cant put the wall hittest into my game please can u look at the fla and try to make it work.
please
ThankZ
need MX version
but i encourage you to try a little harder, i think you could get it with a little thinking and experimenting.
What youre saying is totally right flashlight.
i do have another code that makes you stop on wall hittest but i dont use it, why? cause i dont understand it
This peace of code should work perfectly with walls hittest but i dont understand it thats why i dont use it thats why im asking u to see if u can do it in my fla. so i can look at the code and learn.Code:hero = _root.character;
s = 5;
b = _root.character.getBounds(_root.character);
function move(x, y) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymax-1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymax-1, true)) {
hero._x += x;
hero._y += y;
}
}
}
}
}
if (Key.isDown(Key.UP)) {
_root.hero.play();
move(0, -s);
_root.hero._rotation = 90;
}
if (Key.isDown(Key.DOWN)) {
_root.hero.play();
move(0, s);
_root.hero._rotation =-90;
}
if (Key.isDown(Key.LEFT)) {
_root.hero.play();
move(-s, 0);
_root.hero._rotation =0;
}
if (Key.isDown(Key.RIGHT)) {
_root.hero.play();
move(s, 0);
_root.hero._rotation = 180;
}
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 140;
}
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -140;
}
thankZ for your help. i hope this is the right mx format.
try this(attached FLA)
heres how it works(uses modified version of above code)
onLoad it does this:
1) defines the speed (s)
2) finds the 'bounds' of the object(minimum and maximum X and Y values)
3) defines a function "move" which does this when called(used):
- you give it the position you want to move to in X,Y
- it checks to see if none of the for sides are colliding with the level:
- - current position + desired position(move to) + size of player(using 'bounds') + 1(to add a small cushion_
- if none are, move, else, it wont move
the main reason it works is because it checks where it wants to move before moving, where as yours checked where it is to continue moving.
ThankZ for explaining how could i ever live without you lol.:D :D :D
Theres one more thing.
now i want the walls to move when ex. key up is pressed not the char
the code isDo i have to change it to-->>Code:if (Key.isDown(Key.UP)) {
_root.hero.play();
move(0, -s);
_root.hero._rotation = 90;
}
OR...Code:if (Key.isDown(Key.UP)) {
_root.hero.play();
_root.walls.move(0, -s);
_root.hero._rotation = 90;
}
I tried those but they still wouldnt work.Code:if (Key.isDown(Key.UP)) {
_root.hero.play();
move(0, -s);
_root.walls._y-=s;
_root.hero._rotation = 90;
}
thankZ in advance
PLEASE?
K. Lemme think. For your first attempt at modifying the code, it wouldn't work at all, I'm guessing, maybe the guy turned around and played the walk animation, but nothing else happened. For the second attempt, the whole map moved around with him at the same rate, right?
Ok. Now I *think* that all you need to do to change it to scrolling is to modify the move function slightly. In the final two lines, you should be able to change
code:
hero._x += x;
hero._y += y;
to
code:
_root.walls._x-=x;
_root.walls._y-=y;
What this does is moves the walls the opposite direction of what you would move the hero. As I'm feeling slightly off today, I may be accidentally misleading you totally, but my gut says it should work.
Maybe it works.
Derek.
You know the code >flashl!ght< gave, how would I be able to get the hero to play frame 2 instead of just stoping?
So, instead of stopping against a wall he would go to and play frame 2.
I've tried messing around with the code slightly but I'm having no success, can anyone help?
Ok.
Gaming monkey, to do this, you'd have to have some sort of check. It depends quite a bit on whether you'd want the hero to stop and play the animation or just continue and play the animation.
First one, you could add a boolean value, say hitWall at the beginning of the function and declare it true. When you get to the movement code, it must mean you haven't hit a wall, so declare hitWall to be false. Then, at the end of the function, test hitWall to see if its true, and if it is, play the animation.
This could be further refined to stop the animation from staying at frame 2 by adding a hittingWall (or a better name, whatever, that ones sucky and confusing)boolean value and only play the animation if that is false. Then of course, you'd need to change it back at the end of the animation to false if you wish to have that animation play again.
Wow. Long windedness.
Second one. You'd just need to add movement code to the crashing part too. Or you could just move the hero and test if he has hit the wall, and continue from there.
This has been, Me.
Derek
Derek i did what u wrote it worked!:)
but...:(
now im back to my old problem the one that makes the character stick in the wall. means when u hittest with the wall u cant move anymore.
please can u do it in the fla. file.
[ThankZ]
Weird. It weirded out on me. Shortened version:
I did what I suggested in >flashl!ght<'s version. It worked. Fla attached.
Derek.