forked from PixelsCommander/FlashJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
racingTests.html
181 lines (152 loc) · 6.65 KB
/
racingTests.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html style="height:100%">
<head>
<title>FlashJS racing</title>
<script src="js/jquery.js"></script>
<script src="js/FlashJS/flashStage.js"></script>
<script src="js/FlashJS/flashDisplayObject.js"></script>
<script src="js/FlashJS/flashUtils.js"></script>
<script src="js/FlashJS/flashEvents.js"></script>
<script src="js/FlashJS/flashTween.js"></script>
<script src="js/FlashJS/flashLoader.js"></script>
<script src="js/FlashJS/flashKeyboardManager.js"></script>
<script src="js/emile.js"></script>
<script src="js/underscore.js"></script>
</head>
<body background='assets/back.gif' style='height:100%; margin: 0px;'>
<script>
flash.initStage();
stage.debug = true;
var map = new Loader('./assets/map.png');
map.width = 3328;
map.height = 4672;
stage.addChild(map);
var carObject = new car();
carObject.width = 32;
carObject.height = 46;
stage.addChild(carObject);
carObject.x = 200;
carObject.y = 200;
carObject.initializePhyzics();
var bridge = new Loader('./assets/bridge.png');
bridge.x = 704;
bridge.y = 1409;
stage.addChild(bridge);
stage.addEventListener(Event.ENTER_FRAME, updateWorld);
stage.followForObject(carObject);
function updateWorld(){
keySpace = KeyboardManager.isDown(KeyboardEvent.SPACEBAR_CODE);
keyLeft = KeyboardManager.isDown(KeyboardEvent.LEFT_CODE);
keyRight = KeyboardManager.isDown(KeyboardEvent.RIGHT_CODE);
//the reason why these two are not capitilized any is because "keyUp" and"keyDown" are commands in flash
keyup = KeyboardManager.isDown(KeyboardEvent.UP_CODE);
keydown = KeyboardManager.isDown(KeyboardEvent.DOWN_CODE);
carObject.name = "car1";
if (keySpace){
if (carObject){
var abc = stage.getChildByName("car1").alpha = 50;
}
return;
if (carObject.steering > 0){
carObject.steering = 3.5;
} else {
carObject.steering = -3.5;
}
} else {
// By pushing on certain keys, you get acceleration and decceleration
if (keyup) {
carObject.tspeed += carObject.acceleration;
}
if (keydown) {
carObject.tspeed -= 1.5;
if (carObject.tspeed < -7.5) {
carObject.tspeed = -7.5;
}
}
}
// Friction from ground; this can be any small number so that the car doesn't keep going
// if a key isn't pressed
carObject.tspeed *= .40;
// Velocity Change
carObject.idealxvelocity = carObject.tspeed*Math.cos(carObject.angle*Math.PI/180);
carObject.idealyvelocity = carObject.tspeed*Math.sin(carObject.angle*Math.PI/180);
// By taking the difference between the current velocity and ideal velocity,
// you get the actual velocity
// When you apply the friction, you get the actual velocities.
carObject.xvelocity += ((carObject.idealxvelocity-carObject.xvelocity)*carObject.tfriction);
carObject.yvelocity += ((carObject.idealyvelocity-carObject.yvelocity)*carObject.tfriction);
// By adding the velocities, you can get the new x and y positions
carObject._x += carObject.xvelocity;
carObject._y += carObject.yvelocity;
// By pushing the Left and Right keys, the car steers in that direction
if (keyLeft || keyRight) {
carObject.steering += (keyRight-keyLeft);
} else {
// When you let go of the Left and Right keys, the car goes back to going straight
carObject.steering += ((carObject.steering<0)-(carObject.steering>0));
}
// You want to make sure that when you steer that it doesn't start turning faster and faster
if (!keySpace){
if (carObject.steering<-3) {
carObject.steering = -3;
}
if (carObject.steering>3) {
carObject.steering = 3;
}
}
// Now you can make the car turn based on actual speed and turning angle
actualSpeed = Math.sqrt((carObject.xvelocity*carObject.xvelocity)+(carObject.yvelocity*carObject.yvelocity));
carObject.angle += ((carObject.steering*actualSpeed)*.1);
carObject._angle = (carObject.angle+360)%360-180;
// You need to update the angles from radians to degrees so that it can turn
carObject.rotation = (360 - carObject.angle + 270);
carObject.x = carObject._x;
carObject.y = carObject._y;
if ((carObject.tspeed > .1) && (carObject.tspeed < 14)){
carObject.showSmoke();
} else {
carObject.hideSmoke();
}
}
function car(){
object = new Loader('./assets/buggy.gif');
object.xvelocity = object.yvelocity=0;
object.tspeed = 0;
object.angle = 0;
object.tfriction = .025;
object.steering = 0;
object.acceleration = 21;
object.smokes = object.addChild(new DisplayObject);
object.smokes.y = 30;
object.smokes.left = object.smokes.addChild(new Loader('./assets/smokeLeft.gif'));
object.smokes.left.x = -20;
object.smokes.left.y = -10;
object.smokes.right = object.smokes.addChild(new Loader('./assets/smokeRight.gif'));
object.smokes.right.x = 20;
object.showSmoke = function (){
object.smokes.visible = true;
}
object.hideSmoke = function (){
object.smokes.visible = false;
}
object.initializePhyzics = function(){
object._x = object.x;
object._y = object.y;
}
return object;
}
</script>
<div style='display:none'>
<!--LiveInternet counter--><script type="text/javascript"><!--
document.write("<a href='http://www.liveinternet.ru/click' "+
"target=_blank><img src='//counter.yadro.ru/hit?t42.6;r"+
escape(document.referrer)+((typeof(screen)=="undefined")?"":
";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?
screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+
";"+Math.random()+
"' alt='' title='LiveInternet' "+
"border='0' width='31' height='31'><\/a>")
//--></script><!--/LiveInternet-->
</div>
</body>
</html>