-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·136 lines (100 loc) · 4.34 KB
/
index.html
File metadata and controls
executable file
·136 lines (100 loc) · 4.34 KB
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
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>hello phaser!</title>
<script src="phaser.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render })
function preload() {
// Here we load the Starling Texture Atlas and XML file
//game.load.atlasXML('seacreatures', 'assets/sprites/seacreatures.png', 'assets/sprites/seacreatures.xml');
// Here is the exact same set of animations but as a JSON file instead
game.load.atlas('seacreatures', 'assets/sprites/seacreatures_json.png', 'assets/sprites/seacreatures_json.json');
// Just a few images to use in our underwater scene
game.load.image('undersea', 'assets/pics/undersea.jpg');
game.load.image('coral', 'assets/pics/seabed.png');
game.load.image('fishFood1', 'assets/sprites/fishMedium1.png');
game.load.image('fishFood2', 'assets/sprites/fishMedium2.png');
game.load.image('fishFood3', 'assets/sprites/fishLarge1.png');
game.load.image('fishFood4', 'assets/sprites/fishSmall2.png');
}
var sprite;
var fishArr = new Array();
var MAX_FISH = 10;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.add.image(0, 0, 'undersea');
game.add.image(0, 466, 'coral');
sprite = game.add.sprite(400, 300, 'seacreatures');
sprite.animations.add('swim', Phaser.Animation.generateFrameNames('purpleFish', 0, 20, '', 4), 30, true);
sprite.animations.play('swim');
sprite.scale.x = -1;
sprite.anchor.setTo(0.5, 0.5);
// Enable Arcade Physics for the sprite
game.physics.enable(sprite, Phaser.Physics.ARCADE);
// Tell it we don't want physics to manage the rotation
sprite.body.allowRotation = true;
sprite.body.checkCollision = true;
sprite.body.collideWorldBounds.right = true;
sprite.body.bounce.setTo(1,1);
}
function NewFish() {
var direction = -1;
var startX = 0;
if(game.rnd.integer() % 2 == 0) {
direction = 1;
startX = 800;
}
var startY = game.rnd.integer() % 600;
var rndFish = game.rnd.integerInRange(1, 4);
var fish = game.add.sprite(startX, startY, 'fishFood'+rndFish);
game.physics.enable(fish, Phaser.Physics.ARCADE);
var scale = game.rnd.realInRange(0.3, 1);
fish.scale.x = direction * scale;
fish.scale.y = scale;
fish.speed = direction * ((game.rnd.integer() % 4) + 1);
fish.anchor.setTo(0.5, 0.5);
fish.body.collideWorldBounds = true;
fish.events.onOutOfBounds.add(KillFish, this);
return fish;
}
function KillFish(fish) {
fish.kill();
console.log(fish+" killed!");
}
function update() {
// sprite.rotation = game.physics.arcade.moveToPointer(sprite, 200, game.input.activePointer, 1500);
var spriteDir = sprite.deltaX;
if (spriteDir < 0) {
sprite.scale.y = -1;
} else {
sprite.scale.y = 1;
}
if(game.rnd.integer() % 3 >= 1 && fishArr.length < MAX_FISH) {
fishArr.push(NewFish());
}
var aliveFish = new Array();
for(var i = 0; i < fishArr.length; i++) {
var fish = fishArr[i];
game.physics.arcade.collide(sprite, fish);
fish.x -= fish.speed;
aliveFish.push(fish);
}
fishArr = aliveFish;
}
function collisionHandler (obj1, obj2) {
console.log("Hit! ");
}
function render() {
game.debug.spriteInfo(sprite, 32, 32);
}
};
</script>
<div id="phaser-example">
</div>
</body>
</html>