javaでシューティングゲームを作ってる者です。
弾を単発で出すことが出来たのですが、連射することができません。
特にエラーコードが出ている訳ではなくてただ自分の知識不足なのですがどなたか回答よろしくお願いします。
コードです!
package jp.ohtsuki.minigame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Syuutingu {
Image jiki;
Image teki;
Image back;
Image tama;
JFrame frame1;
BufferStrategy bstrategy;
int tx,ty;
int x = 440;
int y = 660;
boolean tflag;
boolean keySpace,keyup,keydown,keyright,keyleft ;
Thread gamethread;
public void init(){
tflag = false; keySpace=keyup=keydown=keyright=keyleft = false; } Syuutingu(){ frame1 = new JFrame("シューティングゲーム(労作展)"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setBackground(Color.WHITE); frame1.setResizable(false); frame1.setVisible(true); Insets insets = frame1.getInsets(); frame1.setSize(900 + insets.left + insets.right, 700 + insets.top + insets.bottom); frame1.setLocationRelativeTo(null); frame1.setIgnoreRepaint(true); frame1.createBufferStrategy(2); bstrategy = frame1.getBufferStrategy(); frame1.addKeyListener(new MyKeyAdapter()); try{ jiki = ImageIO.read(getClass().getResourceAsStream("15.jpg")); teki = ImageIO.read(getClass().getResourceAsStream("ardyn-izunia.jpg")); back = ImageIO.read(getClass().getResourceAsStream("8956325_102954414000_2.jpg")); tama = ImageIO.read(getClass().getResourceAsStream("sample.png")); }catch(IOException e){ e.printStackTrace(); } Timer t = new Timer(); t.schedule(new MyTimerTask(),10,10); } public static void main(String[] args) { Syuutingu gtm = new Syuutingu(); } class MyTimerTask extends TimerTask{ public void run(){ Graphics g = bstrategy.getDrawGraphics(); if (bstrategy.contentsLost()== false) //Insets insets = frame1.getInsets(); //g.translate(insets.left, insets.top); if(keyup){ y-=6; if(y<0) y = 0; } if(keydown){ y+=6; if(y>675) y=675; } if (keyleft) { x -= 6; if (x < 0) x = 0; } if (keyright) { x += 6; if (x >860) x = 860; } if (keySpace) { if (!tflag) { tx = x + 12; ty = y; tflag = true; } } if (tflag) { if (ty < 0) tflag = false; else ty -= 8; } g.clearRect(0, 0, 900, 700); g.drawImage(back, 0, 0, frame1); g.drawImage(jiki, x, y, frame1); if (tflag) g.drawImage(tama, tx, ty, frame1); bstrategy.show(); g.dispose(); } } class MyKeyAdapter extends KeyAdapter{ @Override public void keyPressed(KeyEvent ev) { switch(ev.getKeyCode()){ case KeyEvent.VK_UP : keyup = true;break; case KeyEvent.VK_DOWN : keydown= true; break; case KeyEvent.VK_LEFT : keyleft = true; break; case KeyEvent.VK_RIGHT : keyright = true; break; case KeyEvent.VK_SPACE: keySpace = true; break; } } public void keyReleased(KeyEvent ev) { switch(ev.getKeyCode()){ case KeyEvent.VK_UP : keyup = false;break; case KeyEvent.VK_DOWN : keydown= false; break; case KeyEvent.VK_LEFT : keyleft = false; break; case KeyEvent.VK_RIGHT : keyright = false; break; case KeyEvent.VK_SPACE: keySpace = false; break; } } } }
回答2件
あなたの回答
tips
プレビュー