A Flash Developer Resource Site

Page 10 of 11 FirstFirst ... 67891011 LastLast
Results 181 to 200 of 214

Thread: The elusive hitTest explained

  1. #181
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128

    Ok I'm sure someone can help me

    Now I had a working code for this but lost it recently but I have a character you move with the keyboard keys, and I have a wall I want to stop the character when they hit it.

    Now the code I had I cant remember but it was really simple, I think it was so when the character ran into it, it had a _x=-4 and _y=-0 or something so no matter how much you tried running into the wall it would just keep pushing the character back not letting them pass through it.

    I'm sure at least one of you Guru's in here know what I'm talking about

  2. #182
    Run for your life! Phlook's Avatar
    Join Date
    Jul 2003
    Location
    Vancouver, Canada
    Posts
    679
    i wrote this one not that long ago (just change instance names)

    Code:
    onClipEvent (load) {
    	speed = 5;
    	function checkHit(x, y) {
    		if (_root.maze.hitTest(this._x+x, this._y+y, true)) {
    			return (true);
    		} else {
    			return (false);
    		}
    	}
    }
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.RIGHT) && !checkHit(speed, 0)) {
    		this._x += speed;
    	} else if (Key.isDown(Key.LEFT) && !checkHit(-speed, 0)) {
    		this._x -= speed;
    	}
    	if (Key.isDown(Key.DOWN) && !checkHit(0, speed)) {
    		this._y += speed;
    	} else if (Key.isDown(Key.UP) && !checkHit(0, -speed)) {
    		this._y -= speed;
    	}
    }

  3. #183
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128
    that doesnt work, that involves moving the character also while my character already has movement on them, I'll put a copy of it in here for you to look at, maybe you'll understand more what I mean, as I'm not the best at explaining

    This is Flash MX
    Attached Files Attached Files

  4. #184
    Video Game Addict
    Join Date
    Apr 2004
    Location
    Oregon
    Posts
    128

    fixed my problem

    I fixed the problem I was having it was super simple it was simply this

    Code:
    onClipEvent (enterFrame) {
            if (hitTest(_level0.wall1)==true) {
                    _x=_x-5;
            }
    }
    on the character and the wall has the instance name of "wall" so then you either change it to:
    _x=_x-5 for a right wall
    _x=_x+5 for a left wall
    _y=_y-5 for a lower wall
    _y=_y+5 for an upper wall

    and just change the 5 to a higher number for more resistence and a lower number for less resistence. Really simple stuff, I found the code at Actionscript.org in Tutorial 47 in the Begining level

  5. #185
    Mmm, Protozoa... The Amoeba's Avatar
    Join Date
    Nov 2004
    Location
    LA, CA, USA
    Posts
    117
    Lets say i wanted to have a hitTest but it tests if an object hit part of another object. If i had a square
    ________
    l_______l
    l_______l
    l_______l
    l_______l
    and i just wanted the middle 2 sections tested, not the top and bottom part.

    Thanks,
    The Amoeba

    ...I know bad drawing lol
    Last edited by The Amoeba; 09-12-2005 at 10:55 PM.
    --The Amoeba

  6. #186
    Mmm, Protozoa... The Amoeba's Avatar
    Join Date
    Nov 2004
    Location
    LA, CA, USA
    Posts
    117
    neone?

  7. #187
    Junior Member
    Join Date
    Oct 2005
    Posts
    6

    Clouds of Mist

    Hi, I am working on a game where the user clicks the button/mouse pointer (a spray can) and a spray comes out to shoot bugs...

    I am pretty sure the right way to go with this is with the HitTest... I am using
    PHP Code:
    onClipEvent (enterFrame) { Mouse.hide(); } onClipEvent (load) { startDrag (""true); } 
    to make the mouse pointer the actual can... can anyone give me some idea as to how to go about doing this? Thanks in advance...

  8. #188
    Junior Member
    Join Date
    Oct 2005
    Posts
    11
    I know a code that would help. However, startdrag is not used. Put it on the mc.

    PHP Code:
    onClipEvent (enterFrame) {
        
    Mouse.hide();
        
    _x _root._xmouse;
        
    _y _root._ymouse;


  9. #189
    Junior Member
    Join Date
    Oct 2005
    Posts
    6
    Thanks for the reply, however the mouse code is not the problem, I just posted that to let whoever might help see the code I am already using to see if there would be any interferences with that code that makes everything function properly...
    The mouse pointer code works fine... the problem is with how to use HitTarget to make the movie clip of the mist in the button of the spray can hit the bugs and make them disappear...

    Any help greatly appreciated...

  10. #190
    V For Victory! R1TAL1N's Avatar
    Join Date
    Jun 2005
    Location
    Chillin in Dub V...
    Posts
    230
    Your question confuses me...

    Do you mean how to get the mist to come out of the spray can? Or do you need to get it to hit the bugs?

    To hit the bugs, just make them all buttons, or something like:

    If (this.hitTest(_root.mist)){
    this.gotoAndPlay("DeathAnimation");
    this.removeMovieClip();
    }
    In the Bug MC's actions.
    My Music

    Click it.

  11. #191
    Junior Member
    Join Date
    Oct 2005
    Posts
    13

    help

    i have the wall named lft and i have this code

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.lft)){
    _x += 5; }
    }

    i have the code on the character and oviously i have the rest of the code on how to move, but when the character hits the mc lft it doesnt get hit back 5 pixels right but it just goes under

  12. #192
    God yeps's Avatar
    Join Date
    Dec 2005
    Location
    :noitacoL
    Posts
    660
    hey what if i wanted the code for a hittest to be put on the wall? (the last post was like 3 months ago so i dought ne body is gonna reply but thanx if u do)

  13. #193
    Senior Member
    Join Date
    Jul 2005
    Posts
    692
    if the wall is on the right, you'd use
    onClipEvent (enterFrame) {
    if (this.hitTest(_root.wall)){
    _x -= speed; }
    }
    left would be oppisite. and top and bottom would be with 'y' instead of 'x'.

  14. #194
    light11.ca habboguy's Avatar
    Join Date
    Apr 2002
    Posts
    384
    Quote Originally Posted by Ed Mack
    Thanks! Is there much more that we can do?

    Here's code I'm quite fond of using for moving the player. It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):

    Code:
    onClipEvent(load){
    	s = 5;
    	b = this.getBounds(this);
    
    	function move(x,y){
    		if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
    			if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
    				if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
    					if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
    						_x += x;
    						_y += y;
    					}
    				}
    			}
    		}
    	}
    }
    
    onClipEvent(enterFrame){
    	if(Key.isDown(Key.UP)){
    		move(0,-s);
    	}
    	if(Key.isDown(Key.DOWN)){
    		move(0,s);
    	}
    	if(Key.isDown(Key.LEFT)){
    		move(-s,0);
    	}
    	if(Key.isDown(Key.RIGHT)){
    		move(s,0);
    	}
    }
    Does anyone know how i can use this code to work in flash 8 or 2004? I tried it at school with MX and it worked fine. When i got home and tried with 8 it did not work. Can someone help/ pm me/ or email [email protected] thanks

  15. #195
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Tried the code with Flash8 and it works just fine.

  16. #196
    Member
    Join Date
    Sep 2004
    Location
    coventry,england
    Posts
    65

    Hit test problem, would LOVE solution!

    Hi,
    this seems like THE place to ask problems to do with hit tests, so here i go.
    on the attached fla file, ive got a purple box (follows the mouse) and a red box. i tried to make it so the purple box cannot pass through the red box, but i cant do it! sometimes it stops, but if you move the mouse fast it tends to go through. ive been messing with this problem for over a week and i really have no more ideas for a solution, id love it so much if someone can shine some light on how i can do this! id LOVE IT!
    (if its possible to stop an MC that is stuck to the mouse pointer from going through the red MC, that would be fine as well, any solution il be so happy for!)
    thanks for any help!
    Attached Files Attached Files
    'flash is da bomb'

  17. #197
    light11.ca habboguy's Avatar
    Join Date
    Apr 2002
    Posts
    384
    Quote Originally Posted by tonypa
    Tried the code with Flash8 and it works just fine.
    still doesnt seem to work for me. i even copy and pasted a whole thing that worked into a different file and then it didnt work.

  18. #198
    Senior Member
    Join Date
    Jun 2006
    Location
    USA
    Posts
    101
    Quote Originally Posted by NeotiK
    this works really nicely for art-based RPG's where you go from room to room, but is there any way to modify the code to make a large outdoor map that will start to scroll up/down/left/right as soon as the "hero" gets to the midpoint of the screen? If you still don't know what I'm talking about here's an example someone did that I'm trying to implement into my RPG engine, but i'm stumped on how to do it using the current script setup: http://www.geocities.com/mclelun/rpg.html

    Any help would be appreciated. Thanx.

    Its called art based scrolling.....

    Or you can use tiles

    check the knowledgebase

  19. #199
    Member
    Join Date
    Jun 2007
    Posts
    67
    hello everyone.
    i am makin a top down art based rpg game, and i have this code:
    code:

    onEnterFrame=function(){
    if(Key.isDown(Key.UP)){
    if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
    game.map._y += speed
    }
    }
    if(Key.isDown(Key.DOWN)){
    if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
    game.map._y -= speed
    }
    }
    if(Key.isDown(Key.LEFT)){
    if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
    game.map._x += speed
    }
    }
    if(Key.isDown(Key.RIGHT)){
    if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
    game.map._x -= speed
    var po_x = game.player._x
    }
    }
    }



    i have some white circle movie clips (instance name limit) in a movie clip called map.
    when it hits the limit movie clip i want it to stop, the problem with taht is it scrolls into the circle a bit before stoping (the player and the object partly overlap) and its easy to get stuck.
    I want it to stop as soon as one edge ofd the player hits one edge of a wall.

    tahts the first thing

    the 2nd thing is that i have 7 or something of them circle movie clips in the map movie clip (all instanced named limit), and it only works for one. I need it so i can make many diffrent walls and boundaries of diffrent shapes and locations.

    help is apreciated! ive been looking for days.

  20. #200
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Quote Originally Posted by ThrashBoy
    hello everyone.
    i am makin a top down art based rpg game, and i have this code:
    code:

    onEnterFrame=function(){
    if(Key.isDown(Key.UP)){
    if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
    game.map._y += speed
    }
    }
    if(Key.isDown(Key.DOWN)){
    if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
    game.map._y -= speed
    }
    }
    if(Key.isDown(Key.LEFT)){
    if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
    game.map._x += speed
    }
    }
    if(Key.isDown(Key.RIGHT)){
    if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
    game.map._x -= speed
    var po_x = game.player._x
    }
    }
    }



    i have some white circle movie clips (instance name limit) in a movie clip called map.
    when it hits the limit movie clip i want it to stop, the problem with taht is it scrolls into the circle a bit before stoping (the player and the object partly overlap) and its easy to get stuck.
    I want it to stop as soon as one edge ofd the player hits one edge of a wall.

    tahts the first thing

    the 2nd thing is that i have 7 or something of them circle movie clips in the map movie clip (all instanced named limit), and it only works for one. I need it so i can make many diffrent walls and boundaries of diffrent shapes and locations.

    help is apreciated! ive been looking for days.

    You don't need to post this twice. I just answered your question here .
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

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