简体   繁体   中英

Error implementing an enemy from one class to another

I'm pretty new to java and I am trying to create a basic game where a type of enemy can be spawned more than once through another class and methods. I've succeeded so far in creating the ability for this enemy to have a set speed, x, y, and dmg, but the movement for the enemy is not working correctly. For some reason, my enemy seems to stop where the player first spawned instead of following the player. I believe it is a problem with my methods but I am not too sure. If you see any bad coding practices or anything I can improve on in my code please let me know and thanks in advance.

Main Program:

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class platformer extends Applet implements Runnable, KeyListener, MouseListener
{
    Thread main = new Thread (this);
    Graphics bufferG;
    Image buffer;
    boolean playerright=false,playerleft=false,jump=false,playerdown=false,shotleft=false,shotright=false;
    int playerx=500,playery=400,yChange=0, height=200, pageCt=1, shotrx=playerx,shotry=playery,shotlx=playerx,shotly=playery,enemyx=1500,enemyy=400,hp=5,enemyhp=2;
    Random r = new Random();
    Enemy2 enemy= new Enemy2(4,enemyx,enemyy,50,1,playerx,playery,3);
    public void init()
    {
        this.addMouseListener(this);
        this.addKeyListener(this);
        this.resize (1400,756);
        buffer= createImage(this.getWidth(),this.getHeight());
        bufferG = buffer.getGraphics();
        main.start();

    }

    public void paint(Graphics g )
    {
        Font font1 = new Font ( " Bodoni MT",1,25);
        if(pageCt==1){
            bufferG.setColor(Color.black);
            bufferG.fillRect(0,0,1400,800);
            bufferG.setColor(Color.gray);
            bufferG.fillRect(0,450,1450,350);

            enemy.draw(bufferG);
            bufferG.setColor(Color.green);
            if (playerdown==true){
                bufferG.fillRect(playerx,playery+15,50,35);
            }
            else
                bufferG.fillRect(playerx,playery,50,50);

            if(shotright==true ){
                bufferG.fillOval(shotrx,shotry,25,25);
            }
            if(shotleft==true ){
                bufferG.fillOval(shotlx,shotly,25,25);
            }
        }
        if(pageCt==2){
            bufferG.setFont(font1);
            bufferG.drawString("You lose",675,150);
        }

        g.drawImage(buffer,0,0,this);
    }

    public void run()
    {
        while(true)
        {
            repaint();
            try
            { main.sleep(10); }
            catch (Exception e ) {}
            if(pageCt==1){

                if (playerright==true ){
                    playerx+=5;
                }
                if (playerleft==true ){
                    playerx-=5;
                }
                if (jump==true ){
                    yChange++;
                    playery=playery+yChange;

                    if(playery>height){
                        jump=false;
                        playery=height;
                    }

                }

                if(shotrx>0 && shotrx<1300 && shotright==true)
                {
                    shotrx+=7;
                }
                else{
                    shotright=false;
                    shotrx=playerx;
                    shotry=playery;
                }

                if(shotlx>0 && shotlx<1300 && shotleft==true)
                {
                    shotlx-=7;
                }
                else{
                    shotleft=false;
                    shotlx=playerx;
                    shotly=playery;
                }


                enemy.move();

                if(enemyhp<=0){
                    enemyhp=2;
                    int c=r.nextInt(3);
                    if(c==1)
                        enemyx=1500;
                    if(c==2)
                        enemyx=-200;
                }

                if(hp<=0)
                {
                    pageCt=2;
                }

                Rectangle playerRect2 = new Rectangle ( playerx,playery+15,50,35);
                Rectangle playerRect1 = new Rectangle ( playerx,playery,50,50);
                Rectangle enemyRect = new Rectangle ( enemyx,enemyy,50,50);
                Rectangle shotr = new Rectangle(shotrx,shotry,25,25);
                Rectangle shotl = new Rectangle(shotlx,shotly,25,25);
                if(enemyRect.intersects(playerRect1)){
                    hp--;
                    if(enemyx>playerx)
                        enemyx+=150;
                    if(enemyx<playerx)
                        enemyx-=150;
                }
                if(shotr.intersects(enemyRect)|| shotl.intersects(enemyRect)){
                    enemyhp--;
                    shotright=false;
                    shotleft=false;
                    if(enemyx>playerx)
                        enemyx+=100;
                    if(enemyx<playerx)
                        enemyx-=100;
                }
            }

        }
    }

    public void keyReleased(KeyEvent e)
    {
        int key= e.getKeyCode();
        if (key == KeyEvent.VK_A )
        {
            playerleft=false;
        }
        if (key == KeyEvent.VK_S )
        {
            playerdown=false;
        }
        if (key == KeyEvent.VK_D )
        {
            playerright=false;
        }

    }

    public void keyPressed(KeyEvent e)
    {
        int key= e.getKeyCode();
        if (key == KeyEvent.VK_A )
        {
            playerleft=true;
        }

        if(key == KeyEvent.VK_W && jump==false && playery>=height)
        {
            jump=true;
            yChange=-20;
            height=playery;
        }
        if (key == KeyEvent.VK_S )
        {
            playerdown=true;
        }
        if (key == KeyEvent.VK_D )
        {
            playerright=true;
        }

        repaint();
    }

    public void keyTyped(KeyEvent e) {}

    public void update(Graphics g)
    {
        paint(g);
    }

    public void mouseClicked ( MouseEvent e )
    {
        int mouseX = e.getX();
        int mouseY = e.getY();
        if(mouseX>=playerx && shotright==false && shotleft==false)
            shotright=true;
        if(mouseX<playerx && shotleft==false&& shotright==false)
            shotleft=true;
    }

    public void mousePressed ( MouseEvent e ) 
    {

    }

    public void mouseReleased ( MouseEvent e ) {}

    public void mouseEntered ( MouseEvent e ) {}

    public void mouseExited ( MouseEvent e ) {}

}

Enemy Program:

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;

public class Enemy2{
    int speed,startX,startY,radius=50,damage=1, playerx,playery,health;
    public Enemy2(int s, int sX,int sY,int rad, int dmg, int playx,int playy, int hp){
        speed=s;
        startX=sX;
        startY=sY;
        radius=rad;
        damage=dmg;
        playerx=playx;
        playery=playy;
        health=hp;
    }

    public void draw(Graphics g){
        g.setColor(Color.red);
        g.fillRect(startX,startY,radius,radius);
    }

    /*public void moveleft(){
    if(startX>playerx)startX-=speed;    
    }

    public void moveright(){
    if(startX<playerx)startX+=speed; 
    }*/

    public void move(){
        if(startX>playerx){
            startX-=speed;
        }
        else if(startX<playerx){
            startX-=speed*-1;    
        }

    }
}

When you create an Enemy2 object you are giving the constructor the playx and playy integer values and copying them in two new integers playerx and playery.

The integers playerx and playery do not have the ability to change in the Enemy2 class (as you have not created code to do it).

You are probably thinking that they somehow connect to the variables of the Player object you took the position from but this is not true.

Instead you could try to pass the Player object in the Enemy2 constructor and use a getter method to get the current values your Player object has instead of passing the two integers (playerx and playery).

It should look like this:

public Enemy2(int s, int sX,int sY,int rad, int dmg, **Player player**, int hp) {
    //constructor code here
}

and the move() method should look something like this:

public void move(){
    if (startX > player.getPlayerX() //or player.playerx if the fields are not private) {
        startX -= speed;
    } else if (startX < player.getPlayerX()) {
        startX -= speed * (-1);    
    }
}

By the way it is advised to almost always set the fields of a Class as private and use getters and setters to get or change their value.

Just wanted to add that this is my first answer in stack overflow. Hope I helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM