Flash CS3 Tutorial, Detecting Collisions
63
Flash Tutorials
- IT Admin
IT Admin is you home for all computer related issues. Stop by and check out what is going on in the it world from a real it professional. - Flash CS4 Tutorials
Adobe Flash CS4 And CS3, The Information you really need is a website and ebook deidcated to provideing quality Flash CS4 Tutorials. - Flash CS3 Tutorials
List of Flash CS3 Tutorials and ActionScript 3.0 Tutorials - Download Source Files
Flash CS3 Tutorial Learn how to detect Collisions with the hitTestObject using ActionScript 3.0.
Flash CS3 Tutorial
Flash CS3 Tutorial, ActionSCript 3.0 and Detecting Collisions
In this Flash CS3 Tutorial I am going to go over the method hitTestObject. This is a more advance Flash CS3 Tutorial, so is you get lost feel free to check out some of the earlier flash tutorials. You have already learned how to drag and drop and object. Now, you need to be able to detect if that object has hit another object. In this flash tutorial I will be using the mouse events, but you can easily change the code to work with key board commands.
As normal you will find some of the basic work in this flash tutorial has been done for you. I have created two object, named and labeled them. Let us start out by making the Chevelle dragable. Select the first key frame of the actions layer and the code from the example.
chevelle_mc.addEventListener(MouseEvent.MOUSE_DOWN, carStart) function carStart(carMove:Event):void{
chevelle_mc.startDrag(); }; chevelle_mc.addEventListener(MouseEvent.MOUSE_UP, carStop) function carStop(carMove:Event):void{ chevelle_mc.stopDrag();
chevelle_mc.stopDrag(); };
We are creating the drag and drop controls so the Chevelle can be controlled with the mouse. I have already covered this code, but I will briefly go over it once again. You are adding two event listeners to the chevelle_mc, one event listener is listening for the mouse event, mouse down and the other is listening for the mouse event mouse up. When flash heres one of these event it will either start or stop the Drag function. You may press Control-Enter, and you should be able to drag the chevelle around.
We need to be able to tell if the Chevelle has hit the truck. Start by adding the code from the example. stage.addEventListener(Event.ENTER_FRAME, hitTest) function hitTest(yourEvent:Event):void{
if (chevelle_mc.hitTestObject(truck_mc)){ trace("they hit"); } };
In this code you are going to add an event listener to the stage, which you should be very comfortable with at this point. Next, you created a function called hitTest, the new part of This Flash Tutorial happens inside this function. You are using the method hitTestObject and are telling Flash to check if chevelle_mc has hit truck_mc. Then if they have hit output they hit. The trace is just a simple way to check and make sure they function is working.
Continue Smiley
Press Control-Enter and you will see that Flash is detecting when the two movie clips intersect.
At this point Flash knows what is going on, but the movie is still very boring. Lets make something happen when the two cars hit. Lets have the Chevelle push the truck if they hit each other. Remove the trace tag and add the new code from the example. stage.addEventListener(Event.ENTER_FRAME, hitTest) function hitTest(yourEvent:Event):void{
if (chevelle_mc.hitTestObject(truck_mc)){ truck_mc.x = chevelle_mc.x+135 } };
This new code might simple confusing but it is very simple. Flash is going to take the x position of the truck and move it to match the x position of the Chevelle - 135 px. We subtract 135px because the x position is the center of the object. We do not want the truck to end up on top of the Chevelle, we want the truck in front of the Chevelle.
Press Control-Enter and move the Chevelle so that the front of the car hits the side of the truck.
So, this Flash Tutorial is getting better. Currently the Chevelle can hit the truck, but what happens if the Chevelle backs into the truck or hits the truck from the side. Lets add a few if statements to control the way the truck moves depending on how the Chevelle hits it. Add the new code from the example code. stage.addEventListener(Event.ENTER_FRAME, hitTest) function hitTest(yourEvent:Event):void{
if (chevelle_mc.hitTestObject(truck_mc)){ if (chevelle_mc.x < truck_mc.x-75){ truck_mc.x = chevelle_mc.x+135
} else if (chevelle_mc.x > truck_mc.x+75){ truck_mc.x = chevelle_mc.x-123
} else if (chevelle_mc.y < truck_mc.y+30){ truck_mc.y = chevelle_mc.y+115
}else if (chevelle_mc.y > truck_mc.y+30){ truck_mc.y = chevelle_mc.y-130 } } };
If the Chevelle's x position is less then the trucks x position minus 75px. That code is checking to if the Chevelle is on the left side of the truck and if so, then it moves the truck same as before. The minus 75 is again based on the width of the truck. We need this if to work only if the truck is hit on the left side, therefor we -75 from the trucks position.
To fully understand you may need to play with the numbers and see what happens.
Next, if the Chevelle isn't on the left side of the truck check to see if it is on the right side. If the Chevelle's x position greater then the trucks x position plus 75, the Chevelle must be on the right side and if so when the Chevelle backs into the truck, the truck needs to move back wards. So, we set the trucks x position to the Chevelle's position - 123. This code is basically the same as if the truck were hit from the other side only we need to change the position of the truck while it is being pushed. Therefor we subtract - 123, this number is based off the width of the Chevelle and will need to be changed based on the size of your object.
Hopefully, at this point you in the Flash Tutorial you are getting the idea. Play with the last two if statements to see how the numbers effect the way the objects relate.
Press Control-Enter and you have a car that can push a truck around.
Flash Tutorials
- Wordpress Themes
1 reviews - 3 months ago
- WordPress &8250; frenchsquared & Free WordPress Themes
1 reviews - 3 months ago
- Flash Tutorial, Preloader
1 reviews - 3 months ago
- 26 Ridiculously Awesome Computer Generated Portraits | Presidia Creative
57 reviews - 3 months ago
- New flat flexible speakers might even help you catch planes & trains
118 reviews - 3 months ago
IT Admin
- CSS, How to understand inline and block level elements.
Block level elements are normally displayed as blocks with line breaks before and after. Examples of block level elements include paragraphs, headings, divs and block quotes. Inline elements are... - 3 months ago
- CSS, How To tweak css for only Safari?
If you have spent anytime dealing with CSS you have run into issues with a browser rendering code incorrectly. You may even have gone as far as to make several style sheets and use a browser check... - 3 months ago
- CSS, How to Hide Styles from Older Browsers?
Some older browsers, such as Netscape Navigator 4 and IE 4, have poor support for CSS. It is possible to hide styles from these browsers using specific media types and @import rules. All styles will... - 3 months ago
- CSS, How to use @import styles
@import Styles? Header and external style sheets also can import other style sheets using the @import rule. The @import rule must be placed before all other rules in the header or external style... - 3 months ago
- CSS, How to use External Style Sheets!
The third method of applying styles to document involves linking to external style sheets. External style sheets are the most appropriate method for styling documents. If styles need to be... - 3 months ago
- CSS, How to Use Header Styles
Header styles also can be used to style the <p> element. The CSS rules can be placed in the head of the document using the style element. Like inline styles, header styles, header styles... - 3 months ago
- CSS, How to Apply Inline Styles.
CSS, Inline styles can be applied directly to elements in the HTML code using the style attribute. However, inline styles should be avoided wherever possible because the styles are added to the HTML... - 3 months ago
- Twitter Weekly Updates for 2009-08-09
New blog post: CSS, What is a <div> http://bit.ly/Sbfu # Flash, ActionScript tutorials on loops http://bit.ly/QMq8t # Great PHP Tutorial on using include and require. Include is a great way... - 3 months ago
PrintShare it! — Rate it: up down flag this hub
Comments
Thanks for the vote of confidence, I wish more people would comment










Huni says:
17 months ago
I needed to find out more on Detecting Collisions and I to this site. I found amazing Flash CS3 Tutorials that can teach any one Flash. Thank you so much for the help.