HitTest Paradise

Q: I want a movieclip to stop when it hits a wall?

A: Put this code inside the player-controlled movieclip. Remember, 2 should be the same as the speed of your player.

_______________________________________________________________________________________

onClipEvent (enterFrame) {
 if (_root.level.hitTest(_x+(_width/2), _y, true)) {
  this._x -= 2;
 }
 if (_root.level.hitTest(_x-(_width/2), _y, true)) {
  this._x += 2;
 }
 if (_root.level.hitTest(_x, _y+(_height/2), true)) {
  this._y -= 2;
 }
 if (_root.level.hitTest(_x, _y-(_height/2), true)) {
  this._y += 2;
 }


_______________________________________________________________________________________

Q: I want a movieclip to stop on  frame 2 inside itself when it touches a movieclip called “goo”?

A: Put this code inside the player-controlled movieclip.

_______________________________________________________________________________________

onClipEvent (enterFrame) {
 if (_root.level.hitTest(_x+(_width/2), _y, true)) {
  this.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x-(_width/2), _y, true)) {
  this.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x, _y+(_height/2), true)) {
  this.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x, _y-(_height/2), true)) {
  this.gotoAndStop(2);
 }
}

_______________________________________________________________________________________

Q: I want a movieclip to make the main timeline go to frame 2 when it touches another movielcip called “thing”?

A: Put this code inside the player-controlled movieclip.

_______________________________________________________________________________________

   onClipEvent (enterFrame) {
 if (_root.level.hitTest(_x+(_width/2), _y, true)) {
  _root.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x-(_width/2), _y, true)) {
  _root.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x, _y+(_height/2), true)) {
  _root.gotoAndStop(2);
 }
 if (_root.level.hitTest(_x, _y-(_height/2), true)) {
  _root.gotoAndStop(2);
 }
}

_______________________________________________________________________________________

32 Responses to “HitTest Paradise”

  1. Re2deemer Says:

    ….Maybe it’s just me but I can’t see any code under the “Put this code inside the player-controlled movieclip”….

  2. eaglevisionproductions Says:

    YES! That was my trick to see if anybody would leave a comment! AND IT WORKED!

    HA! Do you usually visit this site? Thanks for the comment.

    And don’t worry, I WILL PUT THE CODE THERE. Thanks again for the comment, really apreciate it.

  3. mushroom design Says:

    LOL ur a classic, eaglevision!!

  4. eaglevisionproductions Says:

    Thanks…I just have to put the code today…bye! :D

  5. Re2deemer Says:

    ….yeah….it worked……-_o

    Nope, I just stumbled over it while testing Google Blog search and looking for Flash-based blogs. What a strange result I got…..

  6. eaglevisionproductions Says:

    Thanks…I am glad you found it, do you like it? Any suggestions? Are you learning anything or are you a guru? Thanks…I really apreciate it, not many people leave comments, thanks for taking the time. Thanks again.

  7. hussein Says:

    hallo the good woork

  8. eaglevisionproductions Says:

    Thanks :)

  9. willywow Says:

    Hmmm… interesting tutorial to get me started of in the world of Actionscript- I’m still a newb :(
    I found this using tutorialized search :)

  10. eaglevisionproductions Says:

    Thank you!
    Please email me if you need ANY help!
    You can also sign up in the forums, which you will find in the top of this page. (where the buttons are).

    Anyways, nice to have you here, request a tutorial! I will most likely make it :D

    Cheers!

  11. dailyderek Says:

    hey eaglevision nice tutorial! i didn’t know you made tutorials only way i know you is from your movies on newground ps why so random 10 owns
    anyways this is helping me with my new zombie game that i’m
    most likley releasing, so thaks.

  12. eaglevisionproductions Says:

    Thank you for visiting! :D

    Movies on newground? Hmm..I don’t remember making any movies. Just a game called “Lonely Pong” is the only thing I have on NG so far.

    Once again, thanks for leaving a comment! :D

  13. eros_74 Says:

    hi eaglevision..
    sorry but I’m not speak english very well….(I’m Italian)
    anyway your site it’s very nice…..
    thank’s for your help….
    nice tutorial

  14. eaglevisionproductions Says:

    Nice to meet you!
    I hope you stay around.
    Don’t worry, I can understand everything.

    Thank YOU, I love comments :P
    Is there anything else you need help in, I can make a tutorial.

  15. Wellzee Says:

    Hi,
    Great tutorials, they worked well.

    Would it be possible if i could have a tutorial that when an object (controlled with keyboard, which i have already done) hits another object (named box_mc) it changes a text box with no words inside to one saying something like ‘Hello’ ?

    Thanks.

  16. eaglevisionproductions Says:

    Of course!
    I’ll start working on it right now.

  17. eaglevisionproductions Says:

    Oh, I am done….You’ll see it on the homepage. :D

  18. Answer to Wellzee « Says:

    [...] …on this post. [...]

  19. ron Says:

    i have a question
    i have a movie clip called “ball”. it moves left and right, it could also jump
    but how can i make it stay when it lands on other objects instead
    of going threw them?

    please help
    ill apriciate it!

  20. jordan Says:

    ‘Q: I want a movieclip to stop on frame 2 inside itself when it touches a movieclip called “goo”?

    A: Put this code inside the player-controlled movieclip.’

    that code there just confuses me, would this be easier?

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.goo)) {
    _root.character.gotoAndStop(2);
    }

  21. eaglevisionproductions Says:

    Yes, that would be easier.
    But that will hittest the blue box that flash puts on every movieclip, this code makes it hittest the visual part, more realistic.

    It is confusing, but once you read it carefully, you understand it. :)

    Hope this helped.

  22. Spammaster Says:

    Lol, don’t worry, if my name mis-leads you, I only Spam in Spam zones, on forums.

    NICE TUTORIAL!!
    Loved it.

  23. Spammaster Says:

    Oh and BTW, Jordan, TY, too.
    Do you use Flash MX?
    Because the code didn’t work on my Flash 8. But I made it work for it, here’s what I got:

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.goo)) {
    _root.character.gotoAndStop(2);
    }
    }

  24. eaglevisionproductions Says:

    @Spammaster

    Lol! :P
    Thanks, that might work, too, but I don’t think jordan is coming back. :(

    oh well.

  25. Ben Says:

    Hey thanks for this that was exactly what i was lookig for.

  26. eaglevisionproductions Says:

    np! :D
    Glad to help! :P lol

  27. john Says:

    ok, here is the deal. I’m kinda new to flash and i’m trying to put together a game where i have a cannon shoot asteroids. I have a movie clip named “asteroid” and i want it to play when i have another movieclip named cannonball hit into it. Inside the main timeline I have asteroid and i put this code into it

    onClipEvent (enterFrame) {
    if (_root.cannonball.hitTest(_x+(_width/2), _y, true)) {
    asteroid.gotoAndPlay(2);
    }
    if (_root.cannonball.hitTest(_x-(_width/2), _y, true)) {
    asteroid.gotoAndPlay(2);
    }
    if (_root.cannonball.hitTest(_x, _y+(_height/2), true)) {
    asteroid.gotoAndPlay(2);
    }
    if (_root.cannonball.hitTest(_x, _y-(_height/2), true)) {
    asteroid.gotoAndPlay(2);
    }
    }

    What am i doing wrong that the asteroid is not playing its movieclip when the cannonball hits into it
    remeber I’m a noob when it comes to this stuff

  28. eaglevisionproductions Says:

    Please go to eaglevision.890m.com/blog
    the blog has been moved.
    Thanks :)

  29. john Says:

    want me to re-post there?

  30. eaglevisionproductions Says:

    Yes please, thanks :D

  31. Daniel Says:

    How would you create a script where an object would go down the screen and when it was in a certain spot you had to press a button. Kinda like DDR or Guitar hero or Super crazy Guitar maniac deluxe. I would like to make a game kinda like that but cant find any scripts or tutorials for scripts like it.

    hope you can help

  32. Willie Says:

    The blog has moved to eaglevision.890m.com/blog

    See the header image.

Leave a Reply