- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的代码目前可以工作,也就是说我想为火球创建一个单独的类。我不知道如何做到这一点,而且找不到任何示例,我只是在寻求有关派生单独的类的帮助,以便我的主要代码不会太大。这是代码:
import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Image;
import java.io.*;
import javax.swing.JOptionPane;
import javax.imageio.*;
import java.awt.Rectangle;
public class Main extends JPanel implements ActionListener, KeyListener{
ImageIcon images[], imagesL[],standingImage[],punchImg[],
fireImg[],kickImg[],jumpImg[],slamImg[];
int x = 100, y = 530;
int count;
int velX,velY;
int velF;
int firey,firex;
int gravity = 20;
int totalImages =3, currentImage = 0, animationDelay = 80,
numSlam = 3,currentSlam = 0, numberImages = 1,
currentStand = 0,
numPunch = 3, currentPunch = 0;
int currentFire = 0, numFire =1,totalImagesL =3,
currentImageL = 0,currentKick = 0, numKick=2,currentJump = 0,
numJump = 1;
int height, totalHeight = -120;
int velEnemy = 5;
int counter;
int enemyX = 310;
Timer animationTimer;
boolean isMovingLeft;
boolean isKick;
boolean isFire = false, fireIs = false,isMovingRight = false,
isNotMoving = true, isPunch = false, startFire = false, isJump = false,
isSlam = false;
boolean enemyIsDead = false;
Image fireball;
Image back;
Image title;
Image alien;
long lastJumptime = 0;
long lastrJumptime = 0;
long currentTime;
long lastFiretime = 0;
private final int FRAME_DELAY = 50;
public Main() {
setFocusable(true); // make your panel focusable
addKeyListener(this); // register the key listener
setFocusTraversalKeysEnabled(false);
//WALKING/RUNNING RIGHT//
images = new ImageIcon[totalImages];
images[2] = new ImageIcon("standing.png");
images[1] = new ImageIcon("midWay.png");
images[0] = new ImageIcon("running.png");
//WALKING/RUNNING LEFT//
imagesL = new ImageIcon[totalImagesL];
imagesL[2] = new ImageIcon("standingL.png");
imagesL[1] = new ImageIcon("midWayL.png");
imagesL[0] = new ImageIcon("runningL.png");
///STANDING STILL
standingImage = new ImageIcon[numberImages];
standingImage[0] = new ImageIcon("standing.png");
//PUNCH/////
punchImg = new ImageIcon[numPunch];
punchImg[0] = new ImageIcon("midPunch.png");
punchImg[1] = new ImageIcon("punchFull.png");
punchImg[2] = new ImageIcon("midPunch.png");
//COMMENCE FIREBALL//
fireImg = new ImageIcon[numFire];
fireImg[0] = new ImageIcon("startFire.png");
//COMMENCE KICK//
kickImg = new ImageIcon[numKick];
kickImg[0]= new ImageIcon("startKick.png");
kickImg[1]= new ImageIcon("finishKick.png");
//JUMPING//
jumpImg = new ImageIcon[numJump];
jumpImg[0] = new ImageIcon("ready.png");
//TITLE THINGY///
ImageIcon title = new ImageIcon("gokuTitle.png");
//SLAM THINGY//
slamImg = new ImageIcon[numSlam];
slamImg[0] = new ImageIcon("startSlam.png");
slamImg[1] = new ImageIcon("midSlam.png");
slamImg[2] = new ImageIcon("endSlam.png");
startAnimation();
}
public void paintComponent(Graphics g) {
//SOUND FOR THIS STUFF//
Sound[] soundSelection = new Sound[4];
soundSelection[0]= new Sound("Fire Spell.wav");
soundSelection[1] = new Sound("kick effect.wav");
soundSelection[2] = new Sound("fire ball troll.wav");
soundSelection[3] = new Sound("punch sound effect.wav");
//END STUFF FOR SOUND//
super.paintComponent(g);
Graphics g2 = (Graphics) g;
try{
back = ImageIO.read(new File("backGround.png"));
title = ImageIO.read(new File("gokuTitle.png"));
}catch (IOException e) {
}
g2.drawImage(back, 0, 0, null);
g2.drawImage(title, 0, 0, null);
if (isMovingRight == true){//if moving right
if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image imgRun = images[currentImage].getImage();
g2.drawImage(imgRun, x, y, null);
currentImage = (currentImage + 1) % totalImages;
}
}
if (isMovingLeft == true){//if moving left
if (imagesL[currentImageL].getImageLoadStatus() == MediaTracker.COMPLETE)
{
Image imgRunL = imagesL[currentImageL].getImage();
g2.drawImage(imgRunL, x, y, null);
currentImageL = (currentImageL + 1) % totalImagesL;
}
}
if (isNotMoving == true){//IF not moving
if (standingImage[currentStand].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image imgStand = standingImage[currentStand].getImage();
g2.drawImage(imgStand, x, y, null);
currentStand = (currentStand + 1) % numberImages;
}
}
if (fireIs== true){
//Do a seperate class for fireball
isFire = true;
if (fireImg[currentFire].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image fireMe = fireImg[currentFire].getImage();
g2.drawImage(fireMe, x, y, null);
currentFire = (currentFire+ 1) % numFire;
soundSelection[0].play();
soundSelection[2].play();
}
try{
fireball = ImageIO.read(new File("fireball.png"));
}catch (IOException e) {
}
}
if(isFire ==true){
fireIs = false;
g2.drawImage(fireball, firex + 80, firey+30, null);
}
if(isPunch == true){
if (punchImg[currentPunch].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image punchMe = punchImg[currentPunch].getImage();
g2.drawImage(punchMe, x, y, null);
currentPunch = (currentPunch+ 1) % numPunch;
soundSelection[3].play();
}
}
if(isKick == true){
if (kickImg[currentKick].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image kickMe = kickImg[currentKick].getImage();
g2.drawImage(kickMe, x, y, null);
currentKick = (currentKick+ 1) % numKick;
soundSelection[1].play();
}
}
if (isJump == true){
if (jumpImg[currentJump].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image jumpUp = jumpImg[currentJump].getImage();
g2.drawImage(jumpUp,x, y+10, null);
currentJump = (currentJump+ 1) % numJump;
}
}
if (isSlam == true){
if (slamImg[currentSlam].getImageLoadStatus() == MediaTracker.COMPLETE) {
Image slamUp = slamImg[currentSlam].getImage();
g2.drawImage(slamUp,x, y+10, null);
currentSlam = (currentSlam+ 1) % numSlam;
}
}
//ENEMY THAT DOES NOT WANT TO WORK//
//draw rect here//
Rectangle player = new Rectangle(x,y, 60,100);
Rectangle enemies = new Rectangle(enemyX,530, 60,100);
/////
if (player.intersects(enemies)){
enemyIsDead = true;
}
if (enemyIsDead == false){
try{
alien = ImageIO.read(new File("Alien1.png"));
}catch (IOException e) {
}
g2.drawImage(alien, enemyX, 530, null);
}
}
public void actionPerformed(ActionEvent e) {
repaint();
try {
Thread.sleep(FRAME_DELAY);
} catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
x+=velX;
height += velY;
enemyX+=velEnemy;
if (height >= totalHeight){
y+=velY;
}else{
y+=gravity;
}
firex += velF;
if (y < 2){
y = 3;
}
if (y >530){
y = 531;
}
}
public void right(){
velX = 20;
}
public void left(){
velX = -20;
}
public void fire(){
velF = 30;
}
public void jump(){
velY = -30;
}
public void notMovingMethod(){
isMovingRight = false;
isNotMoving = true;
isPunch = false;
isMovingLeft = false;
isKick = false;
isJump = false;
isSlam = false;
}
public void everythingFalse(){
isPunch = false;
isNotMoving = false;
isMovingRight = false;
isMovingLeft = false;
isKick = false;
isJump = false;
isSlam = false;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
//FIND OUT ABOUT COUNTER FOR ENEMIES NEARBY,LIKE IF AN ENEMY IS NEAR YOU FOR
2 SEC YOU LOSE HEALTH
if(code == KeyEvent.VK_Z){
currentTime=System.currentTimeMillis();
if (currentTime >= lastFiretime + 5000){
firex = x;
firey = y;
fire();
fireIs = true;
everythingFalse();
lastFiretime=System.currentTimeMillis();
}
}
if (code == KeyEvent.VK_P){
System.out.println("Paused");
stopAnimation();
}
if (code == KeyEvent.VK_A){
left();
everythingFalse();
isMovingLeft = true;
}
if (code == KeyEvent.VK_F){
everythingFalse();
isPunch = true;
}
if(code == KeyEvent.VK_D){
right();
everythingFalse();
isMovingRight = true;
}
if(code == KeyEvent.VK_Q){
everythingFalse();
isKick = true;
}
if(code == KeyEvent.VK_W){
currentTime=System.currentTimeMillis();
if (currentTime >= lastJumptime + 1000){
jump();
everythingFalse();
isJump = true;
lastJumptime=System.currentTimeMillis();
}
}
if (code == KeyEvent.VK_E){
everythingFalse();
isSlam = true;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
int code = e.getKeyCode();
if(code == KeyEvent.VK_D){
notMovingMethod();
}
if (code == KeyEvent.VK_P){
startAnimation();
}
if (code == KeyEvent.VK_A){
notMovingMethod();
}
if(code == KeyEvent.VK_Q){
notMovingMethod();
}
if (code == KeyEvent.VK_F){
notMovingMethod();
}
if (code == KeyEvent.VK_E){
notMovingMethod();
}
if(code == KeyEvent.VK_Z){
notMovingMethod();
}
if(code == KeyEvent.VK_W){
currentTime=System.currentTimeMillis();
if (currentTime >= lastrJumptime + 1000){
notMovingMethod();
height = 0;
}
lastrJumptime = System.currentTimeMillis();
}
velX = 0;
}
public void startAnimation() {
if (animationTimer == null) {
currentImage = 0;
currentImageL = 0;
currentStand = 0;
currentPunch=0;
currentFire = 0;
currentKick = 0;
currentJump = 0;
currentSlam = 0;
animationTimer = new Timer(animationDelay, this);
animationTimer.start();
} else if (!animationTimer.isRunning())
animationTimer.restart();
}
public void stopAnimation() {
animationTimer.stop();
}
}
最佳答案
嗯,我很高兴您询问如何使用单独的类,因为根据您发布的代码,您非常需要它们。不只是为了火球。对于声音,对于动画,对于玩家,对于敌人。每个类应该只有一个职责,这是您的代码不会做的事情,但它会使其更加清晰和可维护。
要回答您的问题,请使用新类创建一个新的 .java 文件,如下所示:
public class Fireball {
public Fireball() {
//do whatever you need to do in the constructor
}
//you can declare other methods of fireballs here
}
然后使用 import
语句将其包含在您的主类中。
有关单一职责原则的更多信息:https://en.wikipedia.org/wiki/Single_responsibility_principle
类的基本概述 - 如果您刚刚决定第一次使用不同的类,这是最好的起点:https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
关于java - 如何在代码中为火球创建一个单独的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598258/
Java 专家需要您的帮助。 今天我在一次采访中被问到这个问题,但我无法解决。所以我需要一个解决方案来解决这个问题; 反转字符串 Input : Hello, World! Output : oll
目标:单击按钮并将成分作为单独的项目添加到数组中。 当前设置: 这给出:蓝莓芒果柠檬汁 然后我希望能够通过单击按钮将成分作为单独的项目添加到数组中: var allI
如何编写正则表达式来匹配它(参见箭头): "this is a ->'' this is a "test' there is another "test' 第二种情况 /\b'/ Regex Demo
我有一个数组,其中包含有限数量的项目。我想随机删除项目,直到所有项目都被使用过一次。 示例 [1,2,3,4,5] 使用了随机数 5,所以我不想再这样了。使用了随机数 2,所以我不想再这样了。等等..
首先,抱歉,如果这太主观了,我只是不知道还能怎么问/去哪里问。 无论如何,鉴于我最近的所有问题,我准备很快发布一个 Android 应用程序,并且大部分测试都是在我的手机 Droid 上完成的。我真的
这可能不是这个问题的正确位置,如果不合适请随意移动它。我标记为 Delphi/Pascal 因为这是我在 atm 中工作的内容,但这可能适用于我猜的所有编程。 无论如何,我正在做一些代码清理,并考虑将
我像这样分隔了其余 api 的路由。有没有更好的方法来组织路由器?还是我现在的做法没问题? app.js app.use('/api/auth',auth); 应用程序/ Controller /au
我在 2 个单独的工作表中包含以下数据: 表1: A B C D a ff dd ff ee b 12 10 10 12 表2: A B C
我正在使用 jQuery,并在位于单独 HTML 文件中的表中获取了几行。单击时,每一行都会成功重定向到本地 HTML 文件。 (使用window.location) 我想要实现的目标 我想要完成的是
我有重叠背景图像的问题,当它们重叠时会导致阴影比不重叠时更暗,从而产生不均匀的阴影。 我有一个高度灵活的盒子,带有一些透明的背景图像和阴影以创建漂亮的边框。盒子本质上是 3 个元素。 您可以在此处找到
按照正常的微服务框架,我们希望将每个微服务放入其自己的 git 存储库中,然后为 Service Fabric 项目创建一个存储库。当我们更新其中一个微服务时,Service Fabric 项目将仅重
我想将多个片段嵌入到一个指令中。这是我的设置方式。 Everyone Development (3)
我希望在保留原件的同时将多个文件 gzip 到一个目录中(到多个 .gz 文件中)。 我可以使用这些命令来处理单个文件: find . -type f -name "*cache.html" -exe
有没有办法分别知道每个 Eclipse 插件消耗了多少内存? 最佳答案 进行堆转储并使用例如分析它Eclipse Memory Analyser . 如需更多信息,请参阅 Analyzing Equi
我们使用cusrom插件并以这种方式定义脚本(这是一个近似的伪代码): //It is common part for every script (1) environments { "env1"
我在控制台应用程序中托管了一个集线器,并有一个 WPF 应用程序连接到它。它工作得很好。然后我将集线器移到一个单独的项目中,并将主机的引用添加到新项目中。现在我收到 500 错误,没有其他详细信息。
是否可以在单独的 JAR 文件中为 JavaBean 构建类?具体来说,JavaBean 在一个 JAR 文件中具有 Bean 和 BeanInfo 类,而自定义属性编辑器类位于另一个 JAR 文件中
好的,所以我有一个 MAF 应用程序,它在单独的应用程序域中加载每个插件。这非常适合我的需要,因为它允许我在运行时动态卸载和重新加载我的插件。 问题是,我需要能够在子应用域中处理未处理的异常,捕获它,
在参加在线数据库类(class)(针对初学者)时,我注意到一个问题,我必须查找涉及...至少两个不同值的查询...例如, ELMASRI 书中的 COMPANY 数据库指出:查找至少从事两个不同项目的
(首先:我已经尝试了涉及边距、边框等的所有选项。) Link to problematic page. Link to similarly constructed, non-problematic p
我是一名优秀的程序员,十分优秀!