A Flash Developer Resource Site

Page 11 of 11 FirstFirst ... 7891011
Results 201 to 215 of 215

Thread: The elusive hitTest explained

  1. #201
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    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

  2. #202
    Member
    Join Date
    Jun 2007
    Posts
    67
    sorry
    code:
    for (i=0; i<8; i++) {
    onEnterFrame=function(){
    if(Key.isDown(Key.UP)){
    if(! game.map["limit"+i].hitTest(game.player._x, (game.player._y + speed - playerhs), true)) {
    game.map._y += speed
    }
    }
    if(Key.isDown(Key.DOWN)){
    if(! game.map["limit"+i].hitTest(game.player._x, (game.player._y + speed + playerhs), true)) {
    game.map._y -= speed
    }
    }
    if(Key.isDown(Key.LEFT)){
    if(! game.map["limit"+i].hitTest((game.player._x + speed - playerhs), game.player._y, true)) {
    game.map._x += speed
    }
    }
    if(Key.isDown(Key.RIGHT)){
    if(! game.map["limit"+i].hitTest((game.player._x + speed + playerhs), game.player._y, true)) {
    game.map._x -= speed
    }
    }
    }
    }



    i now have another issue (i know that codes not very good above, because i just tryed to make it work) and i was wondreing if that was somewhat close to what i should be doing to make mutiple walls.

  3. #203
    Junior Member
    Join Date
    Jul 2007
    Posts
    1

    Help?

    Hey, i'm a bit of a newbie with flash and programming in general, but I'm attempting to make a 2 player game where u run around and shoot the other player. I'm fine with the moving, (p1=arrowkeys p2=wasd) and p1 can shoot, however I'm having some trouble with testing whether p2 collides with the bullet. The code I am using for the bullet is:

    Code:
    var bulletSpeed=8;
    var bulletReady=true;
    var bulletDelay=55;
    var bulletArray=[];
    var bulletCount=0;
    
    function createBullets(){
    	var bulletMc=this.attachMovie("bullet", "bullet"+bulletCount, 1000+bulletCount);
    	bulletCount++;
    	bulletMc._x+=dude._x-10;
    	bulletMc._y+=dude._y+15;
    	bulletArray.push(bulletMc);
    }
    function moveBullets(){
    	if(bulletReady&&Key.isDown(Key.SPACE)){
    		bulletReady=false;
    		currentTime=getTimer();
    		createBullets();
    	}else{
    		if(currentTime+bulletDelay<=getTimer()){
    			bulletReady=true;
    		}
    	}
    	for(var i=0; i<bulletArray.length; i++){
    		bulletArray[i]._x+=bulletSpeed;
    	}
    }
    this.onEnterFrame=function(){
    	moveBullets();
    }
    and p2 is called dude2. wondering if somebody could give some suggestions as to what code I would insert if I wanted p2 to lose 1 life per time he was hit with a bullet, with a max of 5 lives, and then game over.(note: p2 can't shoot yet, but I'm working on that later)

  4. #204
    Member
    Join Date
    Jan 2007
    Posts
    85
    you can do multiple things, what I would do, which may not be correct, is:
    Inside the original "bullet" movie clip, right click, and hit actions, and then add this code
    onClipEvent(enterFrame){
    if(this.hitTest(dude2)){
    dude2health --
    }}

    that says that every frame every bullet exists in, it will check if it is touching "dude2". and also, if it does, dude2health will decrease by one.

  5. #205
    Junior Member
    Join Date
    Jun 2008
    Location
    Why do you need to know? Are you stalking me?
    Posts
    13
    How could i get it so when a object NOT on the field, created by a script,connects with one ON the field, to get them both to disappear?

    i tryed this.

    PHP Code:
    if bob.hitTest(bullet)
    bob._visible false;
    bullet._visible false
    Bullet isnt on the field bob is.
    They both have linkage....

  6. #206
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    I dont see propper code,- perhaps you meant:
    PHP Code:
    if (bob.hitTest(bullet)){ 
        
    bob._visible false
        
    bullet._visible false;

    plus I dont get your question, what field, with the one?

  7. #207
    Junior Member
    Join Date
    Jun 2008
    Location
    Why do you need to know? Are you stalking me?
    Posts
    13
    Quote Originally Posted by renderhjs
    I dont see propper code,- perhaps you meant:
    PHP Code:
    if (bob.hitTest(bullet)){ 
        
    bob._visible false
        
    bullet._visible false;

    plus I dont get your question, what field, with the one?
    Not shown in the movie until a button is pressed...

  8. #208
    Junior Member
    Join Date
    Aug 2008
    Posts
    10
    Quote Originally Posted by Ed Mack
    Back by popular demand!

    HitTest is a very useful, and can be used for just about anything (like ductape really). There are two main forms that it takes:
    Code:
    if(this.hitTest(another_clip)){
     // Something
    }
    This just tests the bounding boxes of this, and another_clip. If they hit, then the contents of the if will run.
    Code:
    if(this.hitTest(x, y, true)){
     // Something
    }
    This tests the point x,y against the contents of this, the actual shape and not the bounding box. The true is what makes it test against the shape and not the bounding box.

    Anyone else want to continue?

    (if it's an irrelevant post, it'll be edited out as this is a guide sticky, not just a general bantering one )


    i dont understand how i am suppposed to properly use these, your explanation confused me. is there an example you can use?

  9. #209
    Now tell me whos watchin...... samvillian's Avatar
    Join Date
    Feb 2007
    Location
    Where do you live?
    Posts
    539
    hello again old friend. I will give you instructions:
    Make a square movie clip with an instance name of hero.
    make another square movie clip with an instance name of enemy.
    thats it. now to the code:
    if(hero.hitTest(enemy)){ //read this as IF HERO HITS ENEMY, THEN
    trace("you hit the wall you dope")
    }


    that should work, of course you have to allow the hero to move so you can test whether he hits the wall or not. Or you can place the hero on top of the wall and it will trace that line. Sorry i didnt put it in a code /code thing i was lazy.
    If the only tool you have is a hammer, you tend to see every problem as a nail.

    Xbox 360 Modding Controller PS3 Mod Paint Spray LED Case

  10. #210
    Junior Member
    Join Date
    Sep 2004
    Location
    Scotland
    Posts
    20
    I have tried the
    Code:
    if(this.hitTest(_x, _y, true)){
    //do something
    }
    but if dosen't work.

    If someone can modify the attached fla to work I much appreciate it.
    Attached Files Attached Files
    Foo-Wing Li

    Co-Director
    DGFX Designs LLP


    www.dgfxdesigns.com

    Website Templates

  11. #211
    Now tell me whos watchin...... samvillian's Avatar
    Join Date
    Feb 2007
    Location
    Where do you live?
    Posts
    539
    if(this.hitTest(_x, _y, true)){
    //do something
    }

    did you try changing THIS to your INSTANCE NAME? and _x and _y to the actual numbers?
    If the only tool you have is a hammer, you tend to see every problem as a nail.

    Xbox 360 Modding Controller PS3 Mod Paint Spray LED Case

  12. #212
    Junior Member
    Join Date
    Sep 2004
    Location
    Scotland
    Posts
    20
    Quote Originally Posted by samvillian
    if(this.hitTest(_x, _y, true)){
    //do something
    }

    did you try changing THIS to your INSTANCE NAME? and _x and _y to the actual numbers?

    This code is attached to one on my odd shaped Instances that is why I used the THIS.

    How would changing the _x and _y work? This would set a specific collision detection sport and not the edge of the odd shape.

    Have a quick look at the fla I have atached to my original message.

    I want my two Instances of odd shapes to colllision detect when the actual shape pixels come into contact and not when they're boundry boxes come into contact.

    Thanks
    Foo-Wing Li

    Co-Director
    DGFX Designs LLP


    www.dgfxdesigns.com

    Website Templates

  13. #213
    Now tell me whos watchin...... samvillian's Avatar
    Join Date
    Feb 2007
    Location
    Where do you live?
    Posts
    539
    go to kirupa.com and look for the hittest they have. They made it odd shaped just for you. I didn't look at the fla again hehe. but this is not for odd shaped stuff.
    If the only tool you have is a hammer, you tend to see every problem as a nail.

    Xbox 360 Modding Controller PS3 Mod Paint Spray LED Case

  14. #214
    Junior Member
    Join Date
    Sep 2004
    Location
    Scotland
    Posts
    20
    This is what I wanted to do

    http://www.flashstore.com/Flash-Comp...ollisions-324/

    http://flash-creations.com/notes/astb_bounceCollide.php

    My own solution that I have found that works.
    What I have ended up doing is placing lots of little duplicated hitTest instaces which are just circles around the outer edge of my complex odd shape.

    Only when one of these little cirles is hit the do something takes place.

    I did find a nice webpage tutorial explaining about technique this but I have lost it and can't find it agian. If you would like me to throw together an example fla file then just ask me and I will.
    Foo-Wing Li

    Co-Director
    DGFX Designs LLP


    www.dgfxdesigns.com

    Website Templates

  15. #215
    Member
    Join Date
    May 2008
    Posts
    48

    map wht

    i got the source but still coldnt figure out the root map thing

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