-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.js
143 lines (127 loc) · 4.16 KB
/
main.js
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
var game = new Phaser.Game(256, 256, Phaser.AUTO, 'game', { preload: preload, create: create, update: update, render:render});
function preload() {
game.load.tilemap('objects', 'assets/map1-1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('tiles', 'assets/items2.png');
game.load.spritesheet('mario', 'assets/marioSmall.png', 34, 34, 7);
}
var map;
var layer;
var cursors;
var jumpButton;
var runButton;
var result;
var mario = {
sprite: undefined,
direction: 'right',
doNothing: true
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#5C94FC';
map = game.add.tilemap('objects');
map.addTilesetImage('items', 'tiles');
layer = map.createLayer('Capa de Patrones 1');
layer.resizeWorld();
layer.wrap = true;
map.setCollisionBetween(14, 16);
map.setCollisionBetween(21, 22);
map.setCollisionBetween(27, 28);
map.setCollisionByIndex(10);
map.setCollisionByIndex(13);
map.setCollisionByIndex(17);
map.setCollisionByIndex(40);
//game.physics.p2.convertTilemap(map, layer);
//game.physics.p2.gravity.y = 300;
//game.physics.p2.friction = 5;
mario.sprite = game.add.sprite(50, 50, 'mario');
mario.sprite.scale.setTo(0.47, 0.47);
mario.sprite.anchor.x=0.5;
mario.sprite.anchor.y=0.5;
mario.sprite.animations.add('walk');
game.physics.enable(mario.sprite);
game.physics.arcade.gravity.y = 700;
mario.sprite.body.bounce.y = 0;
mario.sprite.body.linearDamping = 1;
mario.sprite.body.collideWorldBounds = true;
//mario.sprite.body.acceleration.x = 120;
mario.sprite.animations.add('left', [2,4,5], 10, true);
mario.sprite.animations.add('wait', [0], 10, true);
mario.sprite.animations.add('jump', [6], 10, true);
mario.sprite.body.fixedRotation = true;
//mario.sprite.body.onBeginContact.add(blockHit, this);
game.camera.follow(mario.sprite);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
runButton = game.input.keyboard.addKey(Phaser.Keyboard.SHIFT);
};
function update(){
game.physics.arcade.collide(mario.sprite, layer);
mario.doNothing = true;
if (cursors.left.isDown){
//mario.sprite.body.acceleration.x = -120;
if(mario.direction!='left'){
mario.sprite.scale.x *= -1;
mario.direction = 'left';
}
if(mario.sprite.body.velocity.x==0 ||
(mario.sprite.animations.currentAnim.name!='left' && mario.sprite.body.onFloor())){
mario.sprite.animations.play('left', 10, true);
}
mario.sprite.body.velocity.x -= 5;
if(runButton.isDown){
if(mario.sprite.body.velocity.x<-200){
mario.sprite.body.velocity.x = -200;
}
}else{
if(mario.sprite.body.velocity.x<-120){
mario.sprite.body.velocity.x = -120;
}
}
mario.doNothing = false;
}else if (cursors.right.isDown){
if(mario.direction!='right'){
mario.sprite.scale.x *= -1;
mario.direction = 'right';
}
if(mario.sprite.body.velocity.x==0 ||
(mario.sprite.animations.currentAnim.name!='left' && mario.sprite.body.onFloor())){
mario.sprite.animations.play('left', 10, true);
}
mario.sprite.body.velocity.x += 5;
if(runButton.isDown){
if(mario.sprite.body.velocity.x>200){
mario.sprite.body.velocity.x = 200;
}
}else{
if(mario.sprite.body.velocity.x>120){
mario.sprite.body.velocity.x = 120;
}
}
mario.doNothing = false;
}
if (cursors.up.justDown){
if(mario.sprite.body.onFloor()){
mario.sprite.body.velocity.y = -310;
mario.sprite.animations.play('jump', 20, true);
mario.doNothing = false;
}
}
if(mario.doNothing){
if(mario.sprite.body.velocity.x>10){
//mario.sprite.body.acceleration.x = 10;
mario.sprite.body.velocity.x -= 10;
}else if(mario.sprite.body.velocity.x<-10){
//mario.sprite.body.acceleration.x = -10;
mario.sprite.body.velocity.x += 10;
}else{
//mario.sprite.body.acceleration.x = 0;
mario.sprite.body.velocity.x = 0;
}
if(mario.sprite.body.onFloor()){
mario.sprite.animations.play('wait', 20, true);
}
}
}
function render() {
game.debug.bodyInfo(mario.sprite, 32, 32);
}