A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: _root.ME.hitTest(PROBLEM!)

  1. #1
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124

    _root.ME.hitTest(PROBLEM!)

    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.
    Attached Files Attached Files
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  2. #2
    Senior Member chriserrorplain's Avatar
    Join Date
    Aug 2002
    Location
    london village
    Posts
    623
    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

  3. #3
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    SORRY!!
    but cant u just give me the link cuz i couldnt find anything
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  4. #4
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    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.
    -Aditya

  5. #5
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    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
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  6. #6
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    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.
    Im not using any loop code im using:
    Code:
    if(Key.isDown(Key.RIGHT)&&!_root.hero.hitTest(walls)){
    _root.hero._x-=movespeed;}
    or something like that on all movements up down right left.

    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
    btw check the fla. file

    heh sry about the code mistype
    Last edited by Djali; 09-22-2004 at 02:12 PM.
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  7. #7
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    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
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  8. #8
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    Thanks alot flashl!ght that helped alot!
    THX
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  9. #9
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    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
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  10. #10
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    need MX version

    but i encourage you to try a little harder, i think you could get it with a little thinking and experimenting.
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  11. #11
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    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
    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;
    
    }
    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.
    thankZ for your help. i hope this is the right mx format.
    Attached Files Attached Files
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  12. #12
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    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.
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  13. #13
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    ThankZ for explaining how could i ever live without you lol.
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  14. #14
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    Theres one more thing.
    now i want the walls to move when ex. key up is pressed not the char
    the code is
    Code:
    if (Key.isDown(Key.UP)) {
    	_root.hero.play();
    	move(0, -s);
    	_root.hero._rotation = 90;
    }
    Do i have to change it to-->>

    Code:
    if (Key.isDown(Key.UP)) {
    	_root.hero.play();
    	_root.walls.move(0, -s);
    	_root.hero._rotation = 90;
    }
    OR...

    Code:
    if (Key.isDown(Key.UP)) {
    	_root.hero.play();
    	move(0, -s);
            _root.walls._y-=s;
    	_root.hero._rotation = 90;
    }
    I tried those but they still wouldnt work.
    thankZ in advance
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  15. #15
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    PLEASE?
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  16. #16
    the Dervish of Q??
    Join Date
    Mar 2004
    Location
    Abbotsford, BC
    Posts
    103
    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.

  17. #17
    Senior Member
    Join Date
    Dec 2003
    Location
    Uk
    Posts
    190
    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?

  18. #18
    the Dervish of Q??
    Join Date
    Mar 2004
    Location
    Abbotsford, BC
    Posts
    103
    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

  19. #19
    Senior Member Djali's Avatar
    Join Date
    Sep 2004
    Location
    Sweden, stockholm
    Posts
    124
    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]
    Area restricted-
    Tresspassers will be shot.
    Survivors will be shot again...


    http://www.geocities.com/legg3r

  20. #20
    the Dervish of Q??
    Join Date
    Mar 2004
    Location
    Abbotsford, BC
    Posts
    103
    Weird. It weirded out on me. Shortened version:

    I did what I suggested in >flashl!ght<'s version. It worked. Fla attached.

    Derek.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center