• Home
  • Free Tutorials
  • Free Video Tutorials
Your Ad Here
Adobe Photoshop
Autodesk 3ds Max
CSS
Flash
Help Center Live
iPhone
Joomla
Make Money Online!
Mambo
mIRC
Outlook Express
SEO / SEM
Simple Machine Forum
Target Explorer

Ping Pong - Moving Pads

Category : Flash Views : 18484
Version : 8 Rating : 
2.9/5 (42 votes)
  • Currently 2.86/5
  • 1
  • 2
  • 3
  • 4
  • 5
Type : Text Source File: ping-pong-moving-pads.zip
Result: See the result


<<< PreviousIndex SeriesNext >>>

Previous | 1 | 2 | Next

In this tutorial we are going to make the Player's pad move with the arrow keys and the Opponent's Pad move automatically depending on where the ball is.

If you followed the last tutorial's instruction on where to put the Movie Clips you choose have two instances of the Pad Movie Clip inside "Pads", one named "Player" and the other "Opponent".

On the actions of the "Player" Movie Clip, paste this:
onClipEvent(enterFrame) { //If the player presses the Right Key if(Key.isDown(Key.RIGHT)) { //create an object to hold x and y values point = new Object(); //set the x and y of point to be equal to the pad point.x = this._x; point.y = this._y; //change the coordinate of point to be global localToGlobal(point); //if the point.x plus the speed of movement is bigger than the //limit for the x coordinate we "return" and the next line //of code wont be executed. if(point.x + 10 >= 220) { return; } //if we are not going to pass the limit of //movement, we move right by 10 pixels this._x += 10; } //If pressed Left else if(Key.isDown(Key.LEFT)) { point = new Object(); //create a new object and set x and y. point.x = this._x; point.y = this._y; localToGlobal(point); //if the point.x minus the speed is less than the left limit. We return. if(point.x - 10 <= -20) { return; } //move if it's not in the limit. this._x -= 10; } }

You should have notice that we used the function localToGlobal("point"); it converts the coordinates from our movie clip to be global.
Inside "Pads", the _x of the player instance may be 13px and at the same time 50px in the global coordinates.
As I have the limits as global coordinates I need to convert the Movie Clip's _x and _y to compare them.

Now open the action for the Opponent instance and paste:
//When the Movie Clip loads onClipEvent(load) { //this is the previous position of the Ball Movie Clip oldX = _root.Ball._x oldY = _root.Ball._y //speed of the pad speed = 5 } onClipEvent(enterFrame) { //don't move the Movie Clip until we set a speed speed = 0; //if the current _x of the ball is bigger than the last one //we added + 10 to the oldX to make a gap so this Movie Clip //won't move if it is close to the ball if(oldX + 10 < _root.Ball._x) { //speed positive to move to the right speed = 2.5 } //if ball moved to the left else if(oldX - 10 >= _root.Ball._x) { //speed negative to move this Movie Clip to the left speed = -2.5; } //if the ball is close to the pad if(this._y < 70) { //increase the speed speed = speed * 4; } //move the Movie Clip left or right this._x += speed //if the Movie Clip moved to far to the right if(this._x > 60) { //go back this._x -= speed } //limit to the left if(this._x < -60) { //go back this._x -= speed } //set oldX to current ball._x oldX = _root.Ball._x; }


del.icio.us digg it Reddit Stumble Upon Technorati

Previous | 1 | 2 | Next

<<< PreviousIndex SeriesNext >>>
How to Video Tutorials on software by Helpvids.com

Video tutorial: Forex trading



Sponsors



Advertisement







Your Ad Here

Studio | Advertisement | About Webzo | Contact Webzo | Terms of Use | Free Video Tutorials by Helpivds

Copyright © 2007 NR Concepts Ltd. All rights reserved.