-
Notifications
You must be signed in to change notification settings - Fork 1
/
bullet03.html
81 lines (78 loc) · 2.51 KB
/
bullet03.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
<!--
Author: Josh Gillham
Version: 10-15-12
Descriptio:
Repeatly fires up to 5 bullets.
Ensure that bullet.bmp is in the same folder.
-->
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Bullet Range</title>
<!-- Load Paper.js libray -->
<script type="text/javascript" src="paper.js"></script>
<!-- Start Paperscript -->
<script type="text/paperscript" canvas="myCanvas">
// Holds all the bullets in the game.
var allBullets= new Array();
// Create a instance at the center of the screen with
// an angle of 0.5 and a speed of 2.
allBullets.push( new Bullet( view.bounds.center, .5, 2 ) );
// Count each onFrame() call.
var counter= 0;
// Called every 1/60th of a second.
function onFrame( ) {
// Count every onFrame event.
++counter;
if( counter == 60 ) {
// Reset the counter
counter= 0;
// Allow a maximum of 5 bullets.
if( allBullets.length < 5 ) {
// Add a new instance into the array.
allBullets.push( new Bullet( view.bounds.center, .5, 2 ) );
}
}
// Loop through all bullets.
for( var i= 0; i < allBullets.length; ++i ) {
// Move each bullet acrossed the screen.
allBullets[ i ].update();
}
}
/**
* Creates a new bullet with these params. The image is loaded and
* placed at position. Everytime update is called, bullet is moved
* by angle at speed.
*
* @arg position the starting position.
* @arg angle the starting angle.
* @arg speed the starting speed.
*
* @return a new instance of Bullet with the following methods:
* -update() moves the bullet.
*/
function Bullet( position, angle, speed ) {
// Load the image.
var img= new Raster( 'bullet' );
// Set the image position
img.position= position;
return {
/**
* Moves the bullet along angle at a speed.
*/
update: function() {
img.position+= new Point( { angle: angle, length: speed } );
}
};
}
</script>
</head>
<body>
Fires 4 bullets.<br>
<!-- Link the canvas -->
<canvas id="myCanvas" resize=""></canvas>
<!-- Embed the image resource -->
<img style="width: 32px; height: 32px; Display: none;" alt="" src="bullet.bmp" id="bullet">
</body>
</html>