my question is a hitTest question.I have rounded corners in my hitTested MC and pacman and my ghost are getting caught up on them.
You can check it out (swf, fla, as) here, PACMAN
Printable View
my question is a hitTest question.I have rounded corners in my hitTested MC and pacman and my ghost are getting caught up on them.
You can check it out (swf, fla, as) here, PACMAN
Everyone has been really helpfull here it seems. Very cool.
Sadly, the question I came seeking an answer for was asked early in this thread, but the answer was side stepped.
I want to test the hit of two non box non circle movie clips. Anyone got a solution for this.
There's a:
anyMovieClip.hitTest(x, y, shapeFlag);
and a:
anyMovieClip.hitTest(target);
Why isn't there a:
anyMovieClip.hitTest(target, shapeFlag);
that would have been so nice. :(
because it would be too hard for the already slow flah player to compute. Director can do it with matte clips, but sadly, flash cannot.Quote:
Originally posted by homeDrone
Why isn't there a...
I hear ya. I was thinking about it the other day and that's what I figured the answer would be.
I came up with a bit of a comprimise solution though and am in the process of coding it now. Though, my skill is not tip top, so it's going a little slow. I've hit some bumps I didn't anticipate.
My solution was to attach a couple movie clips with nothing in them to my objects at key points (corners ect.) Then just test the x and y coordinates of those clips to the target shapes. Im having some problems with getting the right coordinates from the embeded movie clips. As you might guess if I call for the _x position of a one of them, it gives me the _x postion relative to the parent clip, not the screen area.. so I have to work backwards to get the right coords. Even scaling the parent clip changes things and I have to work that into the calcs. Bit of a hassle.
If I am successfull before I get bored of it ;) I'll let ya'll know.
I tried that once. I got bored after typing
I have a short attention span ;)Code:if this.hittest(_root.thinger) {
}
Chicken, hmm, chicken.
Ohh, mommy! Fire Engine!
There's a three legged antelope on the roof...
Thats a common solution homeDrone, one which works pretty nice with racing games having 4 hit points on the 4 corners of the car for collision detection. It isnt an exact shap to shap test, but when dealing with only 4 points, its not really a big deal.
:confused:
Want to be my "Hero-of-the-Day"(tm)?
I need some serious help. I understand the way that the .hitTest() method works, but I cannot get to it work the way I want it to.
Here is the .swf: http://www.markcoates.com/testEngine.swf
I got the movement of the "avatar" working the way I want it, but I need the collision detection to do a few more things.
1) The avatar should be completely unable to move through objects (such as the gray square).
2) It should not get "stuck" like it does when it gets caught in the
.hitTest() area.
3) If you hold the mouse button down, it should *still* be unable to move through objects.
Can someone please take a look at the .swf and the source and help me see what I am doing wrong or what I need to add to get the desired functionality?
Thanks once again. I am indebted!
Here is what i did. My example is a mod of your FLA, but I didn't use the mouse i used keyboard input. The AS code is what I'm going to use for all my projects. It is provided in original form by the Great Ed Mack (w00t! via los Eds!).
Ed Mack's code works great, except for when you have a wall with a thickness/width smaller than your object! So first we test the bounds of the boxes for corner hits then we test from the origin. Seems to work great for me. Take a look the modification i made to your FLA. If you select your avaContainer MC you 'll see the code I've added. Tweak it to your delight (make it work for mouse events and bring it in a little closer to the wall). It took me a second to figure it out, but I'm sure you can too.
HOPE THIS HELPS!
Thanks everyone for all the help. I got it working (a la Ed Mack) and I am pleased with the results. My game engine is well on its way now. The fun part begins! :mrpimp:
woo! can't wait to see the finished game!
Hi.Quote:
Originally posted by strangelife2k
I am going to show you how to have a hitTest on your badguy so that if your hero touches it, it will decrease your life.
1.Goto your hero's instance box and name it "player"
2.Add a dynamic text box on your scene, and set it's variable to "life".
3.Add this code to Frame 1
Edit the life if you wishCode:life = 100;
4.Bring up the object actions panel for your enemy and add these actions:
These actions cause the variable "life" on _root (the one you set the dynamic text field to display) to go down by 5 each time the player and the enemy collide. It also makes the main timeline goto frame two, if the player runs out of life. You should put a gameover screen on frame two of _root.Code:onClipEvent (enterFrame) {
if (this.hitTest(_root.player)) {
_root.life -= 5;
if (_root.life == 0) {
_root.gotoAndStop(2);
}
}
}
There you go, test it out
[ed: Just tweaking a few readability things, thanks very much for this :D]
Thanks for the example strangelife2k. I just have a question about it. I got it to work so that it deducted from the life variable every time you touch the enemy but instead of using a frame number for the game over screen, i used a frame label called "over". I could not get the game to go to the game over screen when it reached 0 and i thought i'd try changing the location that the movie should go to to "2" and it worked. Any ideas why frame labels don't seem to work for this kind of thing.
As you can see, i'm pretty new to flash.
Thanks
:)
Ahrrrrg!!!:mad:
Why is this not working?
oh, and a Merry Christmas to everyone at Flash Kit!!!
Ho Ho Ho
I need the code to detect a hitTest between two duplicated movieclips that are the same. e.g to enemy cars.
When i try to make the code myself the cars run the code for themselves. (sounds confussing!) What they are doing is running the hitTests against them aswell as any other movieclip with the same name. e.g _root["ecar"+i].
Please help me
hi
is there a way to do edge to edge collision detection of two movie clips? I am trying to detect when my first MC hits my second one but the clips might be on a diagonal angle and so i need to use the actual MC and not the bounding box. I tried using true but it doesn't work. Basically i have a MC you can drive around and another which is a shed. I want to park in the shed but only through the open door. If you hit the wall i want the first MC to stop. But the shed is on and angle and so the wall is angled. How do I get this to work?
if i put little boxes on my first MC then that works but not on the diagonal walls?!
thanks anyone
I know the theory of what to do, but I don't have code I can just give you yet.
As senocular says, it's apparently a common solution for the problem, but no one eles has yet posted some freebe code for it here either. (^_-)
In you car movie, put an object (an empty movie clip will do) at each corner. Then do hit tests for those 4 objects center points and your garrage. Since it's just a coordinate set and not a clip you are doing the hit test with, you can use the "true" setting on the hit test.
The problem I'm having with this, is that the coordinates that you get when you call from the embeded corner clip are relative to the car movie clip. It seems that the hit test wants absolute screen coordinates (does that make sense?) so you have to calculate that by adding or subtracting the coordinates of your car movie clip.
EG:
car clip center is at 50,500 in the main movie clip.
embeded corner clip is at 3,8 in the car clip.
so the corner is really at 53,508 in the main movie clip.
That's my guess anyway. I still havent got it working yet.. so I could be wrong.
my big drama is that if i put little squares say at the edges of my objects it will work but i am testing for a lot of things - corners of the car object, corners of the objet being towed, walls of the garages and so on. This means that i would run this function (to test for hits) about 200 times each frame and that makes it too big and chunky to work properly. If there is no way to do this then i wil give up and do it more simply but i thought maybe someone out there would know a workaround??
I hear ya. I will have the same problem I think. But on the example you showed me, you'd only have to test for about 4 points on the jeep and maybe 5 or 6 points on the chopper. You don't need to put any points on the buildings cause they won't be moving.
As far as I can see, using little corner objects IS the workaround when dealing with movie clip hit tests without bounding boxes (;_;)
GEEZ!!!
JUST LOOK A FEW PAGES BACK!!!
I've been working on this one part of my short game in which dynamic text tells how many times the ball has hit a certain movieclip on the stage. I just can't get it to work. I've consulted tutorials, help in flash, and I can't figure it out. I sent my fla with this letter. Could someone please take a look and try to help? If you find the problem could you fix it? I have no idea. The actionscript I'm mainly having trouble with is hittest, target paths and dynamic text. Please someone! I'm quite new at this.
If you can't be bothered downloading it, then heres the situation. I'm getting some dynamic text to tell how many times you've hit a ball. I figure the only way is to use
if(this.hittest(_root.ball));
_root.p1=_rootp1+1
Telltarget(_root.ball)
Play;
p1 is the var for the dynamic text box, all the actionscript is under the actions for the bat that hits the ball, and _roor.ball is the target path for the ball.
It dosen't seem to work. I also write a few frames earlier that _root.p1=0
If you can help me from all that, please do, but please download the fla. to get a better look. (I probably made a typo somewhere)
You wrote the code like this:
It should be written like this:Code:if(this.hittest(_root.ball));
_root.p1=_rootp1+1
Telltarget(_root.ball)
Play;
Code:if(this.hitTest(_root.ball)){
_root.p1=_root.p1+1
tellTarget(_root.ball){
play()
}
}
Um that was a typo. I didn't actually write that in the fla. but, I figured out the problem. There was a line of code in the timeline that I missed. I had been looking within the movieclip, but it was on the outside. it works now. Thanks for your help though!
hey im having a problem with my enemy dieing when he is hit by my "player" here is the code
onClipEvent(load){
_x = Math.round(_x/5)*5;
_y = Math.round(_y/5)*5;
}
onClipEvent(enterFrame){
dx = _parent.player._x - _x; // distance between them
d = Math.sqrt(dx*dx+dy*dy);
// I take _width as being the distance between the two clip's centers
if(d > 20 && d < 250){
// Find angle to travel: (180/Math.PI) converts radians to degrees
_rotation = Math.atan2(dx, -dy)*(359/Math.PI);
// Travel that way
_x -= Math.sin(_rotation)*20; // 5 is speed
}
if(_x-25 > _root.player._x+15){
_x-=5;
} else {
this.gotoAndStop("attack")
}
} \\BELOW IS WHERE IM HAVING THE PROBLEM AND IT'S PROBABLY EASY TO FIX
onClipEvent(enterFrame) {
if(hitTest(_root.windmillring)) {
gotoAndPlay ("die");
}
}
i have the "die" movieclip in the enemy and when he hits the windmillring i want it to play "die"
any help would be great
thanks
I was bored so i made this rather cool little game! :)
Remember those games with the metal thing that was looped around wire, which u had to follow?
Well, using some nice shapeflag hit test, i made it - so if your abit miffed by it all, download this .fla!
I hope this is the right place, im sure it will help someone!
I have a problem in my Shoot-down-enemy-space-ships game. if i have 10 space ships flying down the screen, and a gun which shoots bullets, how can i quikly find out if the ship has been hit. this is the code that i have used so far:
the problem is to make the if() code use the variable i. can anyonehelp me?Code:for( i; i<11; i++){
if(_root.ship.i.hitTest(this._x,this._x,true)){
gotoAndPlay("explode"):
}
}
if your ships are named "1", "2", and "3" you can do this:
not too sure how you named your ships. If they were named "ship1", "ship2", etc. do this:Code:for( i; i<11; i++){
if(_root.ship[i].hitTest(this._x,this._x,true)){
gotoAndPlay("explode"):
}
}
Code:for( i; i<11; i++){
if(_root["ship"+i].hitTest(this._x,this._x,true)){
gotoAndPlay("explode"):
}
}
Heya, quick question...
So heres the set up---
There's a movie clip as the mouse called mouse1, and inside that, there's a dynamic text box called mousehit. There is another movie clip on the screen, a painting, and I want it so that when the mouse is over the painting, the dynamic text box says "Painting"
Yeesh...
ok, the directory of the dynamic text box is:
_root.mouse1.mousehit
On the little painting, make it an MC, and insert this code:
This should work...... my syntax maybe wrongish (just edit it in advanced mode)Code:OnClipEvent(enterframe){
if(this.hittest(_root.mouse1)){
_root.mouse1.mousehit = "painting"
{
{
Good luck!
Ah haha! Thats what I was doing wrong... Thanks!
no problem, i do suggest you start a new thread so people will see it!
Good luck with what that code was for!
i've got this problem and i don't know how to fix it, my guy keeps sticking to the walls
http://www32.brinkster.com/mrmalee/test.html
That depends on what your code is and what you want it to be :p
Show the code you have now...
Or:
My favorite... wich I first got of Magnetos (I guess) and wich is used a lot (I guess)
onClipEvent(load){
jumping = false;
velY = 0;
}
onClipEvent(enterFrame){
//-->
//Walking code goes here
//<--
//
//-->
//Jumping code
if(Key.isDown(Key.UP)){
jumping = true;
velY = -8;
}
if(jumping == true){
_y += velY
velY++;
}
//<--
//
//-->
//HitTest
if(_root.ground.hitTest(_x, _y, true)){
while(_root.ground.hitTest(_x, _y, true)){
_y--;
}
jumping = false;
velY = 0;
} else {
jumping = true;
}
//<--
}
I hope this i the right code cuz I just typed without testing... but it should be working :)
Greetz,
SaphuA
Can someone help me I want it so that if i collide with something then after a few seconds it'll go to another frame
can someone tell me if there is anything wrong with this code?
Code:setProperty (laser, _y, getProperty(laser, _y)-5);
if (_root.mgame.mHero.Zaber.hitTest(this._x, this._y, true)) {
tellTarget (_root.mgame.mHero) {
gotoAndStop (1);
}
}
Do you think you could give us some more information?
Visually there are no errors, but then again, it depends in what context you use the AS!
I hope i can help, but i need more information :eek:
alright, alright. I figured instead of starting a new thread, i would just post here, since my problem is a hitTest problem.
I printed David Doull's tutorial here in Flashkit on making a spaceship side scroller. I went through it a few months ago, and got the game to work....for the most part. But i lost the files and decided to redo it. Here is my problem:
I have the baddies coded to hitTest for the "spaceship". If so, then the movie goes to frame 3 "gameOver".
that didn't work. So, i put the hitTest on the spaceship to test against the enemy. That didn't work.
I even tried changing the gotoAndStop from "gameOver" to frame number 3. Still, no dice. And this worked before! Maybe its a stupid mistake im overlooking? here is my code:
Code://enemy test for spaceship
if(this.hitTest(_root.spaceship)){
_root.gotoAndStop("gameOver");
}
I know this code is right. They are both under onClipEvent(enterFrame) actions. But it still doesn't work!Code://spaceship test for enemy
if(this.hitTest(_root["enemy"+i])){
_root.gotoAndStop("gameOver");
}
Furthermore, i know that all the movie clips are named properly in the code, and the frame label is not misspelled....i quadruple checked all of this!!
This maybe workes fine with balls but if you want to check irregular shaped Mc's for collision... like this two:Quote:
Originally posted by flashGaul
that worked, that really worked.
thx edmack that was gr8 help.
this is what I tried and got it. The important thing is that I have totally eluded the hitTest function of flash.
onClipEvent (enterFrame) {
x = _x-_root.two._x;
y = _y-_root.two._y;
h = Math.sqrt((x*x)+(y*y));
rad = _width/2;
radTwo = _root.two._width/2;
sumRad = rad+radTwo;
if (sumRad>=h) {
trace("yes");
}
}
onClipEvent(load){
startDrag("", true);
}
two is the other movie clip on the stage.
Infact I am working on a pool game with this help I can really move ahead. I will start posting threads based on this.
thx again.
http://www.supervisiondesign.com/pic...ollision01.gif
I don't want them to react when their bounding boxes collide...
http://www.supervisiondesign.com/pic...ollision02.gif
or...
http://www.supervisiondesign.com/pic...ollision03.gif
This is the collision I'm looking for:
http://www.supervisiondesign.com/pic...ollision04.gif
Is there a way?
Didn't read all messages before posting... (only read the first page. didn't see that there was more).
The other posts answered my question... that it's not possible.
um....are you sure you read right?
I thought it was possible. (this.hitTest(x,y,shapeflag))
the collision I mensioned should look like homeDrone says on page 3 in this thred...
anyMovieClip.hitTest(target, shapeFlag);
the hitTest you mension only checks one specific point.
Okay, here is my code for the hittest in my game. The problem is it checks the bounding box and not the actual image shape. So I get collisions when it does not look like I should. How would I fix this.
Oh and the link to the game (not finished) is
http://www.sfhq.net/sfhq/game.swf
and http://www.sfhq.net/sfhq/game.fla
onClipEvent (enterFrame) {
if (this.hitTest(_root.sovereign)) {
shipVel=0
if (_root.shield>0) {
_root.rshield.moveMovieClip (rshield);
_root.rshield._x = this._x;
_root.rshield._y = this._y;
_root.rshield._rotation = this._rotation;
_root.shield -= 1;
}if (_root.shield<1) {
_root.life -= 1;
_root.rshield.moveMovieClip (rshield);
_root.rshield._x = _root.non._x;
_root.rshield._y = _root.non._y;
_root.rshield._rotation = _root.non._rotation;
}
}}