teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

修正

2016/07/14 23:30

投稿

nosonosolife
nosonosolife

スコア42

title CHANGED
File without changes
body CHANGED
@@ -47,13 +47,56 @@
47
47
  return map[Math.floor(y/16)][Math.floor(x/16)];
48
48
  }
49
49
 
50
+
51
+ var cPlayer = {
52
+ MAX_ANIM_ID:0,
53
+ IMG_CHANGE_STEP:0,
54
+ animID:0,
55
+ icStep:0,
56
+ x:0,
57
+ y:0,
58
+ width:0,
59
+ height:0,
50
- /*
60
+ vx:0,
51
- 当たり判定
61
+ vy:0,
62
+ dir:0,
63
+ y_prev:0,
64
+ y_temp:0,
65
+ jumpFlag:0,
66
+
67
+ init:function(x,y){
68
+ this.MAX_ANIM_ID = 2;
69
+ this.IMG_CHANGE_STEP = 3;
70
+ this.animID = 0;
71
+ this.icStep = 0;
52
- */
72
+ this.x = x;
73
+ this.y = y;
74
+ this.width = 31;
75
+ this.height = 40;
76
+ this.vx = 50;
77
+ this.vy = 0;
78
+ this.dir = "RIGHT";
79
+ this.y_prev = 0;
80
+ this.y_temp = 0;
81
+ this.jumpFlag = false;
82
+ },
83
+ draw:function(){
84
+ if(this.dir == "LEFT"){
85
+ ctx.save(); // canvas状態を保存
86
+ ctx.transform(-1, 0, 0, 1, 0, 0); // 画像を左右反転させる
87
+ ctx.drawImage(Asset.images['box'], this.width * this.animID, 0, this.width, this.height, -this.x - this.width, this.y, this.width, this.height);
88
+ ctx.restore(); // canvasの状態をsaveされた状態に戻す
89
+ }else{
90
+ ctx.drawImage(Asset.images['box'], this.width * this.animID, 0, this.width, this.height, this.x, this.y, this.width, this.height);
91
+ }
92
+ },
93
+ move:function(){
53
94
  //床衝突判定
95
+ if (this.y > this.height && this.y < SCREEN_HEIGHT-this.height &&
54
- if (collisionMap(this.x , this.y+this.height, MapChip) != 255 ||
96
+ collisionMap(this.x , this.y+this.height, MapChip) != 255 ||
97
+ this.y > this.height && this.y < SCREEN_HEIGHT-this.height &&
55
98
  collisionMap(this.x+this.width, this.y+this.height, MapChip) != 255){
56
- this.jumpFlag = false;//キャラがジャンプするフラグ
99
+ this.jumpFlag = false;
57
100
  this.y = Math.floor((this.y/16)*16);
58
101
  }
59
102
  //天井衝突判定
@@ -66,14 +109,68 @@
66
109
  this.y += 588 * delta;
67
110
  }
68
111
  }
112
+
113
+ if(keyCode["RIGHT"]){
114
+ this.dir = "RIGHT";
115
+ this.x += this.vx * delta;
116
+
69
- //壁衝突判定(右側)
117
+ //壁衝突判定(右側)
70
- if (collisionMap(this.x, this.y , MapChip) != 255 ||
118
+ if (collisionMap(this.x, this.y , MapChip) != 255 ||
71
119
  collisionMap(this.x+16, this.y+16, MapChip) != 255){
72
120
  this.x = Math.floor((this.x/16)*16);
73
121
  }
122
+ // 何回でアニメーションを変えるか
123
+ if(this.icStep >= this.IMG_CHANGE_STEP){
124
+ this.icStep = 0;
125
+ if(this.animID <= this.MAX_ANIM_ID){
126
+ this.animID++; // 次のコマへ
127
+ }
128
+ else{
129
+ this.animID = 0;
130
+ }
131
+ }else{
132
+ this.icStep++;
133
+ }
134
+ }
135
+ else if(keyCode["LEFT"]){
136
+ this.dir = "LEFT";
137
+ this.x -= this.vx * delta;
138
+
74
- //壁衝突判定(左側)
139
+ //壁衝突判定(左側)
75
- if (collisionMap(this.x, this.y , MapChip) != 255 ||
140
+ if (collisionMap(this.x, this.y , MapChip) != 255 ||
76
141
  collisionMap(this.x-16, this.y+16, MapChip) != 255){
77
142
  this.x = Math.floor((this.x/16)*16) + 16;
78
143
  }
144
+
145
+ // 何回でアニメーションを変えるか
146
+ if(this.icStep >= this.IMG_CHANGE_STEP){
147
+ this.icStep = 0;
148
+ if(this.animID <= this.MAX_ANIM_ID){
149
+ this.animID++; // 次のコマへ
150
+ }
151
+ else{
152
+ this.animID = 0;
153
+ }
154
+ }else{
155
+ this.icStep++;
156
+ }
157
+ }
158
+ else{
159
+ this.animID = 0;
160
+ }
161
+
162
+ //ジャンプ処理
163
+ if(this.jumpFlag == true){
164
+ this.y_temp = this.y;
165
+ this.y +=(this.y-this.y_prev)+1;
166
+ this.y_prev = this.y_temp;
167
+ }
168
+
169
+ if(keyCode["SPACE"] && this.jumpFlag == false){
170
+ this.jumpFlag = true;
171
+ this.y_prev = this.y;
172
+ this.y = this.y-16;
173
+ }
174
+ }
175
+ }
79
176
  ```