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.
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.
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?
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.
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!
Last edited by NodeRunner; 12-20-2002 at 05:27 PM.
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
Code:
life = 100;
Edit the life if you wish
4.Bring up the object actions panel for your enemy and add these actions:
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.player)) {
_root.life -= 5;
if (_root.life == 0) {
_root.gotoAndStop(2);
}
}
}
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.
There you go, test it out
[ed: Just tweaking a few readability things, thanks very much for this ]
Hi.
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.
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].
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
You can't have everything...where would you put it?
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??
Last edited by condorcet; 01-12-2003 at 06:21 PM.
You can't have everything...where would you put it?
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 (;_;)
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)