AS2 Tennis

In 2005 I was planning on starting up some programming tennis with someone – pass some code back and forth and see what happens. For some reason the game of tennis never got off the ground and I was left with the first thing I wrote for the game. It’s interesting to see how my code style has changed in ~6 years… Maybe I’ll recode this one and really compare the styles… Anyway have a look:

Move mouse left and right to rotate world.

This movie requires Flash Player 9

There is definitely some room for a little cleanup in the code, but overall it’s not that bad. I used to have kind of weird commenting habits. Take into account that back then there was no graphics class or drawing api so we made all our graphics in the library. I have a movieClip in the timeline that has this animation in it – each frame is used as a segment for the isometric shapes/creatures:

And here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//----------------------------------
// Tennis
// 
// code by zevan 2005 // www.shapevent.com 
// -- 
//----------------------------------
shapeNum = 4;
// world space variables:
Stage.scaleMode = "noScale";
centerX = 250;
centerY = 100;
grid._x = centerX;
grid._y = centerY+100;
rot = 0;
// get total frames of graphic0
attachMovie("graphic0", "graphic", 0);
segmentNum = graphic._totalframes;
graphic.removeMovieClip();
//----------------------------------
// color constants
//----------------------------------
MR = 0x00ff0000;
MG = 0x0000ff00;
MB = 0x000000ff;
MRB = 0x00ff00ff;
//----------------------------------
// basic constants
//----------------------------------
rads = Math.PI/180;
for (var i = 0; i<shapeNum; i++) {
	//----------------------------------
	// randomize settings for shape
	//----------------------------------
	initObject = {
		oscillationSpeed:10+Math.random()*20,
		_size:30, polarRot:Math.random()*360 
	};
	shape = attachMovie("shape0", "s"+i, i, initObject);
	//----------------------------------
	// calculate some values based on random settings
	//----------------------------------
	shape.radius = shape._size/4;
	shape.yscale = shape._size/2;
	shape.yOffset = shape._size/16;
	shape.xrad = 0;
	shape.yrad = shape.xrad/2;
 
	shape.increment = 0;
	shape.rotation = 0;
	shape.rdest = 0;
	shape.polarDest = shape.polarRot;
	shape.xradDest = shape.xrad;
	shape.ePolar = shape.polarRot;
	shape.eXrad = shape.xrad;
	shape.br = 0;
	shape.bt = 0;
	shape.num = i;
	//
	placeSegments(shape);
	shape.onEnterFrame = runShape;
}
//----------------------------------
// attach segments and colorize
//----------------------------------
function placeSegments(movieClip) {
	for (var j = 0; j<segmentNum; j++) {
		segment = movieClip.attachMovie("segment0", "seg"+j, j);
		segment._y = -(j*movieClip.yOffset);
		// move shadow graphic down
		if (j == 0) {
			segment._y += 20;
		}
		segment._graphic.gotoAndStop(j+1);
		// create gradient 
		if (j>1) {
			movieClip.segmentColor = new Color(movieClip["seg"+j]);
			col = blend(0xFFFFFF, 0x222222, j*10);
			movieClip.segmentColor.setRGB(col);
		}
	}
}
//----------------------------------
// shape loop (motion, collision, rotation)
//----------------------------------
function runShape() {
	//----------------------------------
	// randomize direction 
	//----------------------------------
	if (random(30) == 1) {
		this.rdest = random(720)-360;
		this.polarDest += random(2)-1;
	}
	if (random(5) == 1) {
		if (this.xradDest<100) {
			this.xradDest += random(100)-50;
		} else {
			this.xradDest -= 500;
		}
	}
	//----------------------------------
	// collision 
	//----------------------------------
	/*for (var i = 0; i<shapeNum; i++) {
			if (i != this.num) {
			}
		}*/
	//----------------------------------
	// translate 
	//----------------------------------
	this.eXrad += (this.xradDest-this.eXrad)/12;
	this.ePolar += (this.polarDest-this.ePolar)/12;
	this.xrad -= this.br;
	this.br = ((this.xrad-this.eXrad)/27+this.br)/1.1;
	this.polarRot -= this.bt;
	this.bt = ((this.polarRot-this.ePolar)/27+this.bt)/1.1;
	this.rotation += (this.rdest-this.rotation)/12;
	this.increment += this.oscillationSpeed;
	//----------------------------------
	// isometric w/ rotation
	//----------------------------------
	for (var i = 0; i<25; i++) {
		this.segment = this["seg"+i];
		this.segment._yscale = this.segment._xscale/2;
		this.segment._graphic._yscale = this._size+this.radius*Math.sin((i*5+this.increment)*rads);
		this.segment._graphic._xscale = this.segment._graphic._yscale;
		this.segment._graphic._rotation = this.rotation;
	}
}
//----------------------------------
// main loop
//----------------------------------
onEnterFrame = function () {
	for (var i = 0; i<shapeNum; i++) {
		shape = this["s"+i];
		// ----------------------------------
		// world rotation
		// ----------------------------------
		rot += ((-_root._xmouse*rads)-rot)/12;
		grid._graphic._rotation = -rot/rads;
		xosc = shape.xrad*Math.sin(rot+shape.polarRot);
		shape._x = centerX+xosc;
		shape.yrad = shape.xrad/2;
		yosc = centerY+shape.yrad*Math.cos(rot+shape.polarRot);
		shape._y = centerY+yosc;
		shape._xscale = shape._yscale=shape._size+shape._y/5;
		shape.swapDepths(shape._y);
	}
};
//-------------------------------------
// blend two colors 
// amount has range from 0-255
//-------------------------------------
function blend(color1, color2, amount) {
	var m1 = (color2 & MRB);
	var m2 = (color2 & MG);
	return (((amount*((color1 & MRB)-m1) >> 8)+m1) & MRB) | ((amount*((color1 & MG)-m2) >> 8)+m2) & MG;
}

And here is the fla if you’re interested: tennis0.fla

So, I figured out that this was done in flash 7 a few months before the release of flash 8. Now it’s time to go dig up some of my old flash 5 stuff.



 
 
 

Leave a Reply

Spam protection by WP Captcha-Free