独学で学んでいる者です。
・試みたこと
下記のTankクラスの問題個所でプレイヤークラスから渡された(setPlayerX(),setPlayerY())はただ原点座標を返しているにすぎないと判断した私は、その座標をプレイヤーの動きと重ねればどうだろうと思い、プレイヤーのYの動きである
PlayerY +=vy;
をsetPlayerYメソッドに書きました。
すると、プレイヤーが落ちてくるよりも遅く(確認のために描画してある)爆炎がゆっくりと下がってきました。
今後、重ねる方法をとらないにしても、なぜ同じ式を入れているプレイヤーと爆炎で落ちるスピードが違うのか?という質問が2番になります。
ダブルバッファリングによるフレーム数とかの問題ですか?
・前回の質問
1playerが動いているのだからplayerYに格納されている値も変化し続けているのではないのですか?
2同じ値を使っているのに別の座標で落ちる速度が違うのはなぜですか?
3どうすれば逐一動いているplayerYの座標を取得できますか?
プレイヤークラス
java
1 2import android.app.Activity; 3import android.content.Context; 4import android.graphics.Bitmap; 5import android.graphics.BitmapFactory; 6import android.graphics.Paint; 7import android.graphics.PixelFormat; 8import android.graphics.Point; 9import android.view.Display; 10import android.view.MotionEvent; 11import android.view.SurfaceHolder; 12 13 14 15public class Players { 16private: 17 SurfaceHolder holder; 18 Graphics gr1; 19 Paint paint; 20 21 //ジャンプ 22 private int oldY; 23 private int newY; 24 25 //プレイヤー 26 public int PlayerX ; 27 public int PlayerY; 28 private int PlayerToX = PlayerX; 29 30 //横方向のカウント数 31 private int xcount = 0; 32 33 //添字 34 private int ix = 0; 35 36 //重力 37 public int vy = 27; 38 39 40 public Bitmap[] Bp = new Bitmap[30]; 41 42 public Players(int w, int h,SurfaceHolder holder,Activity P){ 43 44 45 this.holder =holder; 46 this.holder.setFormat(PixelFormat.RGBA_8888); 47 this.holder.setFixedSize(w,h); 48 paint = new Paint(); 49 paint.setAntiAlias(true); 50 51 //画面サイズの指定 52 Display display = P.getWindowManager().getDefaultDisplay(); 53 Point p = new Point(); 54 display.getSize(p); 55 56 gr1= new Graphics(p.x,p.y,holder); 57 gr1.setOrigin(p.x,p.y); 58 59 60 //ビットマップの読み込み 61 for (int i = 0; i < 30; i++) { 62 Bp [i] = readBitmap(P, "red" + i); 63 64 65 } 66 67 } 68 69 public void set1(Graphics gr11) { 70 71 gr1 = gr11 72 73 //プレイヤーの描画 74 gr11.drawBitmap(Bp[ix],PlayerX, PlayerY); 75 76 77 } 78 //歩行 79 public void move1(){ 80 //歩行 81 if (Math.abs(PlayerX - PlayerToX) < 1) { 82 PlayerX = PlayerToX; 83 } else if (PlayerX < PlayerToX) { 84 85 86 PlayerX += 27; 87 } else if (PlayerX > PlayerToX) { 88 89 PlayerX -= 27; 90 } 91 } 92 93 94 //重力加速度 95 public int goDown(){ 96 97 return PlayerY +=vy ; 98 } 99 100 //重力の付加 101 public void gravity(){ 102 PlayerY =goDown(); 103 104 } 105 106 //描画座標 107 public int setPlayerX() { 108 109 110 return PlayerX; 111 } 112 public int setPlayerY(){ 113//ここにgoDown();を書いたりしました。 114 return PlayerY; 115 116 } 117 //Xの移動終点 118 public int setPlayertoX(){ 119 return PlayerToX; 120 } 121 122 123 124 //右方向 125 public void move1right(){ 126 127 while (PlayerToX - PlayerX > 1) { 128 xcount++; 129 130 131 //カウントから添え字を求める 132 ix = Math.abs(xcount) % 6 + 1; 133 134 135 break; 136 } 137 138 139 } 140 //左方向 141 public void move1left(){ 142 143 while (PlayerX - PlayerToX > 0) { 144 145 xcount++; 146 147 //カウントから添え字を求める 148 ix = Math.abs(xcount) % 6 + 7; 149 break; 150 151 } 152 } 153 //歩行制御 154 public void save(){ 155 156 if (PlayerX > 960-176 ) { 157 PlayerX =Math.abs(960-176); 158 } 159 } 160 //タッチイベント 161 public boolean onTouchEvent(MotionEvent event) { 162 int touchX = (int) (event.getX()); 163 int touchAction = event.getAction(); 164 165 if (touchAction == MotionEvent.ACTION_DOWN) { 166 167 PlayerToX = touchX; 168 169 newY = (int) event.getY(); 170 171 172 } 173 174 175 if (touchAction == MotionEvent.ACTION_UP) { 176 177 PlayerToX = PlayerX; 178 179 oldY = (int) (event.getY()); 180 if (newY > oldY + 60) { 181 PlayerY -= 250; 182 183 } else {} 184 185 } 186 187 return true; 188 } 189 190 191 //ビットマップの読み込み 192 static Bitmap readBitmap(Context context, String name) { 193 194 int resID = context.getResources().getIdentifier(name, "drawable", context.getPackageName()); 195 return BitmapFactory.decodeResource(context.getResources(), resID); 196 197 198 } 199 200 201} 202
敵(Tank)
java
1 2 3import android.app.Activity; 4import android.content.Context; 5import android.graphics.Bitmap; 6import android.graphics.BitmapFactory; 7import android.graphics.Paint; 8import android.graphics.PixelFormat; 9import android.graphics.Point; 10import android.view.Display; 11import android.view.SurfaceHolder; 12 13import java.util.ArrayList; 14import java.util.List; 15 16 17public class Tank { 18 19private: 20 SurfaceHolder holder; 21 Graphics gra2; 22 Paint paint; 23 Players p1; 24 BackGround BG; 25 int TankX = 1600; 26 int TankFlag = 0; 27 28 29 private class Tfire { 30 int x; 31 int y; 32 int life; 33 34 private Tfire(int x, int y) { 35 this.x = x; 36 this.y = y; 37 this.life = 1; 38 } 39 } 40 private class Smg { 41 int x; 42 int y; 43 int life; 44 45 private Smg(int x, int y) { 46 this.x = x; 47 this.y = y; 48 this.life = 5; 49 } 50 } 51 52 private class Expl { 53 int x; 54 int y; 55 int life; 56 57 public Expl(int x, int y) { 58 this.x = x; 59 this.y = y; 60 this.life = 5; 61 } 62 } 63 64 65 private List<Tfire> fire = new ArrayList<Tfire>(); //戦車弾 66 private List<Smg> Smoke = new ArrayList<Smg>();//煙 67 private List<Expl> expls =new ArrayList<Expl>();//爆炎 68 69 70 public Bitmap[] Bt = new Bitmap[30]; 71 72 73 public Tank(int w, int h, SurfaceHolder holder, Activity T){ 74 75 76 this.holder =holder; 77 this.holder.setFormat(PixelFormat.RGBA_8888); 78 this.holder.setFixedSize(w,h); 79 paint = new Paint(); 80 paint.setAntiAlias(true); 81 82 //画面サイズの指定 83 Display display = T.getWindowManager().getDefaultDisplay(); 84 Point p = new Point(); 85 display.getSize(p); 86 87 88 //ビットマップの読み込み 89 for (int i = 0; i < 30; i++) { 90 Bt [i] = readBitmap(T, "red" + i); 91 92 } 93 gra2= new Graphics(w,h,holder); 94 BG = new BackGround(w, h, holder, T); 95 p1 = new Players(w, h, holder, T); 96 97 } 98 99 public void set1(Graphics gr2) { 100 101 gra2 =gr2; 102 103 104 //戦車 105 gr2.drawBitmap(Bt[16], TankX, BG.setEarthH() - 151); 106 107 108 109 //戦車の弾 110 for (int i = fire.size() - 1; i >= 0; i--) { 111 Tfire atk = fire.get(i); 112 113 gr2.drawBitmap(Bt[17], atk.x, atk.y); 114 115 } 116 //煙 117 for (int i = Smoke.size() - 1; i >= 0; i--) { 118 Smg pos = Smoke.get(i); 119 gr2.drawBitmap(Bt[18], pos.x, pos.y); 120 } 121 //爆炎 122 for (int i = expls.size() - 1; i >= 0; i--) { 123 Expl expl = expls.get(i); 124 gr2.drawBitmap(Bt[20], expl.x, expl.y); 125 } 126 } 127 128 //攻撃 129 public void attack(){ 130 131 //弾の移動 132 for (int i = fire.size() - 1; i >= 0; i--) { 133 Tfire pos = fire.get(i); 134 pos.x -= 96; 135 136 if (pos.x < 0) { 137 fire.remove(i); 138 TankFlag = 0; 139 } 140 break; 141 } 142 143 //爆炎の追加 144 if(TankFlag ==1){ 145 for (int i = fire.size() - 1; i >= 0; i--) { 146 Tfire pos = fire.get(i); 147//ここで戦車弾とプレイヤーを衝突させたかった 148 if (pos.x < 100) { 149/*しかし、衝突しなかったのでプレイヤーの座標がどこにあるのか 150調べるため、現時点は100の座標と衝突させて、プレイヤーの座標で 151爆炎が起こるようになっている。 152*/ 153 154 expls.add(new Expl(p1.setPlayerX(), p1.setPlayerY())); 155 fire.remove(i); 156 157 TankFlag = 0; 158 } 159 }} 160 //煙の遷移 161 for (int i = Smoke.size() - 1; i >= 0; i--) { 162 Smg pos = Smoke.get(i); 163 pos.life--; 164 165 if (pos.life < 0) { 166 Smoke.remove(i); 167 } 168 break; 169 } 170 171 //戦車の攻撃 172 if (TankFlag == 0) { 173 if (TankX > p1.setPlayerX()) { 174 fire.add(new Tfire(TankX - 70, BG.setEarthH()-146)); 175 Smoke.add(new Smg(TankX - 60, BG.setEarthH() - 150)); 176 TankFlag = 1; 177 178 } 179 } 180 //爆炎の遷移 181 for (int i = expls.size() - 1; i >= 0; i--) { 182 Expl expl = expls.get(i); 183 expl.life--; 184 if (expl.life < 0) { 185 expls.remove(i); 186 } 187 break; 188 } 189 190 } 191 192 public void move(){ 193 TankX -= 27; 194 for (int i = Smoke.size() - 1; i >= 0; i--) { 195 Smg pos = Smoke.get(i); 196 pos.x-=27; 197 } 198 for (int i = expls.size() - 1; i >= 0; i--) { 199 Expl expl = expls.get(i); 200 expl.x -= 27; 201 } 202 } 203 204 205 206 //ビットマップの読み込み 207 static Bitmap readBitmap(Context context, String name) { 208 209 int resID = context.getResources().getIdentifier(name, "drawable", context.getPackageName()); 210 return BitmapFactory.decodeResource(context.getResources(), resID); 211 212 213 } 214} 215 216
これらの細かいクラスをダブルバッファリングで大本のクラスに書いています
>この質問で一番聞きたい事
戦車弾とプレイヤーを衝突させたいです。
どうしたらいいですか?
回答2件
あなたの回答
tips
プレビュー