This site requires JavaScript, please enable it in your browser!
Greenfoot back
Aaron-aid
Aaron-aid wrote ...

2022/3/17

when i add more than 1 enemy, the new one despawns

Aaron-aid Aaron-aid

2022/3/17

#
i have no clue why and its super annoying ;c
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemy extends Actor
{
    boolean metlocation = false;
    int health = 100;
    String face = "front";
    //punching stuff
    boolean hit = false;
    boolean punchready  = true;
    int def_punch_Val = 10;
    int def_punch_Val2 = 20;
    int timer = def_punch_Val;
    int timer2 = def_punch_Val+10;
    public static int num = 0;
    int tempvalFrontX;
    int tempvalFrontY;
    boolean runonce = false;
    int downOG = 100;
    int downcounter = downOG;
    int moverCounter = 20;
    //combo
    int punchcombotimer = 20;
    int hitcounter = 0;
    boolean resethit = false;
    boolean hit_check1 = false;
    int timer3 = 20;
    boolean stopattack = false;
    //public ArrayList<object> storage = new ArrayList<object>();
    
    //animation
    GifImage LeftWalk = new GifImage("enemy-wl.gif");
    GifImage RightWalk = new GifImage("enemy-wr.gif");
    /**
     * Act - do whatever the enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage("hit-L.png");
        getWorld().showText("moverCounter= "+moverCounter, 100, 100);
        getWorld().showText("hit= "+hit, 110, 100);
        getWorld().showText("x"+"left".equals(face), 110, 150);
        if(isTouching(fistHitbox.class))
        {
            hit_check1 = true;
            stopattack = true;
             if (resethit == false){
                 hitcounter++;
                 resethit = true;
            }
             
             
             
        }else{stopattack = false;}
        if (hit_check1 == true)
        {
            timer3--;
            if(timer3 <= 0)
            {
                    if(isTouching(fistHitbox.class)  == false)
                {
                hit_check1 = false;
                hitcounter = 0;
                resethit = false;
                timer3 = 20;
                hit = true;
                stopattack = false;
               }
                
            
            }
              
    }
        if(hit == false){
        test2(1);
    }   if(hit == true)
    {
            if (runonce == false){
                 //front 
                 tempvalFrontX = getX();
                 tempvalFrontY = getY();
                 runonce = true;
             }
             if("front".equals(face)){
                setImage("hit-up.png");
                
                punchready= false;
                 if(moverCounter >= 0){
                 setLocation(getX(), getY()+3);
                 moverCounter--;
                }else
                {
                    setImage("down.png");
                    downcounter--;
                    if(downcounter <= 0)
                    {
                        //reset stats for next punch
                        health = health - 10;
                        hit = false;
                        downcounter= downOG;
                        moverCounter = 20;
                        runonce = false;
                    }
                }      
             }
             if("back".equals(face)){
                 punchready= false;
               setImage("hit-down.png");
                 if(moverCounter >= 0){
                 setLocation(getX(), getY()-3);
                 moverCounter--;
                }else
                {
                    setImage("down.png");
                    downcounter--;
                    if(downcounter <= 0)
                    {
                        //reset stats for next punch
                        health = health - 10;
                        hit = false;
                        downcounter= downOG;
                        moverCounter = 20;
                        runonce = false;
                    }
                }      
             } 
             if("left".equals(face)){
                setImage("hit-R.png");
                punchready= false;
                 if(moverCounter >= 0){
                 setLocation(getX()+3, getY());
                 moverCounter--;
                }else
                {
                    setImage("down.png");
                    downcounter--;
                    if(downcounter <= 0)
                    {
                        //reset stats for next punch
                        health = health - 10;
                        hit = false;
                        downcounter= downOG;
                        moverCounter = 20;
                        runonce = false;
                    }
                }      
             } 
             if("right".equals(face)){
               setImage("hit-L.png");
               punchready= false;
                 if(moverCounter >= 0){
                 setLocation(getX()-3, getY());
                 moverCounter--;
                }else
                {
                    setImage("down.png");
                    downcounter--;
                    if(downcounter <= 0)
                    {
                        //reset stats for next punch
                        health = health - 10;
                        hit = false;
                        downcounter= downOG;
                        moverCounter = 20;
                        runonce = false;
                    }
                }      
             
        }
    }
        if(health <= 0)
        {
            num = num + 1;
            getWorld().addObject(new deadgoon(), getX(), getY());
            getWorld().removeObject(this);
            
        }
    }    
    
    void punch()
    {
        if(punchready == true){
        if("front".equals(face)){
        
        getWorld().addObject(new enemypunch(), getX(), getY()-10);
        punchready = false;
    }
    if("back".equals(face)){
        getWorld().addObject(new enemypunch(), getX(), getY()+10);
        punchready = false;
    } 
    if("left".equals(face)){
        getWorld().addObject(new enemypunch(), getX()+10, getY());
        punchready = false;
    } 
    if("right".equals(face)){
        getWorld().addObject(new enemypunch(), getX()-10, getY());
        punchready = false;
    } 
        }
        if (punchready == false)
        {
            timer--;
            timer2--;
            if(timer<= 0)
            {
                if(getWorld().getObjects(enemypunch.class).size()>=1){
                getWorld().removeObject(getWorld().getObjects(enemypunch.class).get(0));
            }
            }
            if(timer2<=0)
            {
                //this is how long till next punch is thrown
                timer = def_punch_Val;
                timer2 = def_punch_Val2;
                punchready= true;
            }
        }
        
    }
    
    void test2(int speed)
    
    {
        //make sure no punches left after moving
       if(metlocation == false)
        {
            if(getWorld().getObjects(enemypunch.class).size()>=1){
                if (Greenfoot.getRandomNumber(1) == 1 )
                {
                    getWorld().addObject(new health(), getX(), getY());
                
                }
                getWorld().removeObject(getWorld().getObjects(enemypunch.class).get(0));
            }
            
        }
    
    //moving
    
        if(isTouching(playerhitbox.class))
        {
            metlocation = true;
            if (stopattack == false){
                punch();
            }
            setImage("hit.png");
            getImage().scale(100, 100);
        }
        else{metlocation = false;}
        if(metlocation == false){
        if(getWorld().getObjects(Player.class).get(0).getX() >= getX())
        {
            move(speed);
            face = "right";
            
            setImage(LeftWalk.getCurrentImage());
            getImage().scale(100, 100);
            //getImage().scale(10, 10);
        }
        if(getWorld().getObjects(Player.class).get(0).getY() <= getY())
        {
            setLocation(getX(), getY()-speed);
            face = "front";
            setImage("front.png");
            getImage().scale(100, 100);
            //getImage().scale(10, 10);
        }
        if(getWorld().getObjects(Player.class).get(0).getY() == getY())
        {
            //metlocationx = true;
        }
        if(getWorld().getObjects(Player.class).get(0).getY() >= getY())
        {
            setLocation(getX(), getY()+speed);
            face = "back";
            setImage("back2.png");
            getImage().scale(100, 100);
            //getImage().scale(10, 10);
        }
        if(getWorld().getObjects(Player.class).get(0).getX() <= getX())
        {
            move(-speed);
            
            face = "left";
            setImage(RightWalk.getCurrentImage());
            getImage().scale(100, 100);
            //getImage().scale(10, 10);
        }
        
        
        
    }
}
}



Aaron-aid Aaron-aid

2022/3/17

#
heres the player code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    static int check1 = 0;
    static boolean debug = false;
    //playermovinghitbox checker
    public ArrayList<String> FaceStorage = new ArrayList<String>();
    boolean checklocation =false;
    int holder;
    boolean touched = true;
    //controlls
    boolean enterdown = false;
    static boolean playerWalking = true;
    GifImage LeftWalk = new GifImage("LeftWalk.gif");
    GifImage RightWalk = new GifImage("RightWalk.gif");
    String direction = "front";
    int defVal = 20;
    //player movement switches
    //movement
    boolean stat1 = true;
    //attack
    boolean stat2 = true;
    //punching
    fistHitbox punch = new fistHitbox();
    //front
    boolean frontpunch = false;
    boolean FtimerTF = false;
    int frontpunchtimer = defVal;
    
    //back
    boolean backpunch = false;
    boolean BtimerTF = false;
    int backpunchtimer = defVal;
    //left
    boolean leftpunch = false;
    boolean LtimerTF = false;
    int leftpunchtimer = defVal;
    
    //right
    
    boolean rightpunch = false;
    boolean RtimerTF = false;
    int rightpunchtimer = defVal;
    //health
    static int health = 100;
    static int boosterhealth = 200;
    boolean runonce = false;
    static int currentlv = 1;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        start();
        info();
        health();
        cleanup();
    }  
    void cleanup()
    {
    
            //actors
        if(getWorld().getObjects(BossActor.class).size() != 0){
        if(getWorld().getObjects(BossActor.class).size() > 1)
        {
            getWorld().removeObject(getWorld().getObjects(BossActor.class).get(getWorld().getObjects(BossActor.class).size()-1));
        }
    
    }
       if(getWorld().getObjects(GoonActor.class).size() > 1)
        {
            getWorld().removeObject(getWorld().getObjects(GoonActor.class).get(getWorld().getObjects(GoonActor.class).size()-1));
            if(getWorld().getObjects(GoonActor.class).size() > 4){
            getWorld().removeObject(getWorld().getObjects(GoonActor.class).get(getWorld().getObjects(GoonActor.class).size()-2));
        }
        }
        //npcs
       
        
    
    
        if(getWorld().getObjects(enemy.class).size() != 0){
        if(getWorld().getObjects(enemy.class).size() > 1)
        {
            getWorld().removeObject(getWorld().getObjects(enemy.class).get(getWorld().getObjects(enemy.class).size()-1));
        }
    
        }
        if(getWorld().getObjects(enemy.class).size() != 0){
        if(getWorld().getObjects(enemy.class).size() > 2)
        {
            getWorld().removeObject(getWorld().getObjects(enemy.class).get(getWorld().getObjects(enemy.class).size()-1));
        }
    
        }

       
    
        
    }
    void health()
    {
        
        if(isTouching(enemypunch.class)&&runonce == false)
        {
            
            health = health - 5;
            runonce = true;
        }else{runonce = false;}
        if(health<= 0)
        {
            health = 100;
            deathscreen x = new deathscreen();
            
            Greenfoot.setWorld(x);
        }
            
    }
    void info()
    {
        getWorld().showText("FP="+frontpunch+"PFT="+frontpunchtimer, 100, 100);
    }
    void start()
    {
        playerMovement(stat1,stat2);
        if(debug == true){
            gamestart(true);
        }else
        {
            stat1= true;
            stat2 = true;
        }
        
        playerReqCheck();
    }
    void gamestart(boolean start)
    {
        if(start == true)
        {
            gamestory();
        }
    }
    void gamestory()
    {
        //level one
        if(currentlv == 1){
        BossActor x = new BossActor();
        getWorld().addObject(x, 600, getY());
        if(getWorld().getObjects(BossActor.class).get(0).check1 == false && getWorld().getObjects(BossActor.class).get(0).runonce2 == false){
            x.runact = 1;
        }
        
    }
    if(currentlv == 2)
    {
        if(check1 == 1)
        {
            if(getWorld().getObjects(enemySpawner.class).size() > 0){
            getWorld().getObjects(enemySpawner.class).get(0).enemys = true;
            getWorld().getObjects(enemySpawner.class).get(1).enemys = true;
        }
        }
    }
    if(currentlv == 3)
    {
        stat1= true;
        stat2 = true;
    }
    if(currentlv == 5)
    {
        stat1= true;
        stat2 = true;
    }
    }
    void playerReqCheck()
    {
        //checks and spawns in all required items for the player to work
        if (getWorld().getObjects(playerfeet.class).size() == 0)
        {
            getWorld().addObject(new playerfeet(), 0, 0);
        }
        if (getWorld().getObjects(playerhitbox.class).size() == 0)
        {
            getWorld().addObject(new playerhitbox(), 0, 0);
        }
        if (getWorld().getObjects(healthbar.class).size() == 0)
        {
            getWorld().addObject(new healthbar(),547,13);
        }
    }
    void playerMovement(boolean movement,boolean fists)
    {
        if(movement == true){
        p_moveLeft(3);
    }
    if(fists == true){
        playerPunch();
    }
    }
    void playerPunch()
    {
       FrontPunch();
       LeftPunch();
       RightPunch();
       BackPunch();
    }
    void FrontPunch(){
        
        if (FtimerTF == false)
        {
            frontpunchtimer--;
            if(frontpunchtimer <= 0)
            {
               getWorld().removeObject(punch);
               FtimerTF = true;
               frontpunchtimer = defVal;
            }
        }
        if(Greenfoot.isKeyDown("Enter"))
            {
                if ("front".equals(direction)&&frontpunch == false && FtimerTF == true )
                {
                
                
                getWorld().addObject(punch, getX(), getY()-40);
                frontpunch = true;
                FtimerTF = false;
               }
               
            }else{frontpunch = false;}
        
    }
    
    void BackPunch(){ 
        if (BtimerTF == false)
        {
            backpunchtimer--;
            if(backpunchtimer <= 0)
            {
               getWorld().removeObject(punch);
               BtimerTF = true;
               backpunchtimer = defVal;
            }
        }
        if(Greenfoot.isKeyDown("Enter"))
            {
                if ("back".equals(direction)&&backpunch == false && BtimerTF == true )
                {
                
                
                getWorld().addObject(punch, getX(), getY()+40);
                backpunch = true;
                BtimerTF = false;
               }
               
            }else{backpunch = false;}
        
    }
    void LeftPunch(){
        if (LtimerTF == false)
        {
            leftpunchtimer--;
            if(leftpunchtimer <= 0)
            {
               getWorld().removeObject(punch);
               LtimerTF = true;
               leftpunchtimer = defVal;
            }
        }
        if(Greenfoot.isKeyDown("Enter"))
            {
                if ("left".equals(direction)&&leftpunch == false && LtimerTF == true )
                {
                
                
                getWorld().addObject(punch, getX()-30, getY());
                leftpunch = true;
                LtimerTF = false;
               }
               
            }else{leftpunch = false;}
        
    
        
    
    }
    void RightPunch()
    {
        if (RtimerTF == false)
        {
            rightpunchtimer--;
            if(rightpunchtimer <= 0)
            {
               getWorld().removeObject(punch);
               RtimerTF = true;
               rightpunchtimer = defVal;
            }
        }
        if(Greenfoot.isKeyDown("Enter"))
            {
                if ("right".equals(direction)&&rightpunch == false && RtimerTF == true )
                {
                
                
                getWorld().addObject(punch, getX()+30, getY());
                rightpunch = true;
                RtimerTF = false;
               }
               
            }else{rightpunch = false;}
        
    
    }
    void p_moveLeft(int speed){
        if(getWorld().getObjects(playerfeet.class).size() >= 1){
        if(getWorld().getObjects(playerfeet.class).get(0).touchingWalkinglocation() == false&&FaceStorage.size()>=1)
        {
            //front
            if("front".equals(FaceStorage.get( FaceStorage.size()-1)))
                {
                    setLocation(getX(), getY()+10);
                    
                }
            if("front".equals(FaceStorage.get( FaceStorage.size()-2)))
                {
                    setLocation(getX(), getY()+10);
                    
                }
            //back
            if("back".equals(FaceStorage.get( FaceStorage.size()-1)))
                {
                    setLocation(getX(), getY()-10);
                    
                }
            if("back".equals(FaceStorage.get( FaceStorage.size()-2)))
                {
                    setLocation(getX(), getY()-10);
                    
                }     
            //left
            if("left".equals(FaceStorage.get( FaceStorage.size()-1)))
                {
                    setLocation(getX()+10, getY());
                    
                }
            if("left".equals(FaceStorage.get( FaceStorage.size()-2)))
                {
                    setLocation(getX()+10, getY());
                    
                } 
            //right
            if("right".equals(FaceStorage.get( FaceStorage.size()-1)))
                {
                    setLocation(getX()-10, getY());
                    
                }
            if("right".equals(FaceStorage.get( FaceStorage.size()-2)))
                {
                    setLocation(getX()-10, getY());
                    
                } 
            
            
        }
    }
        if(playerWalking == true)
        {
        if(getWorld().getObjects(playerfeet.class).size() >= 1&&FaceStorage.size()>=1){
         if(getWorld().getObjects(playerfeet.class).get(0).touchingWalkinglocation() ==false)
         {
             
        }
    }
    if(getWorld().getObjects(playerfeet.class).size() >= 1){
         if(getWorld().getObjects(playerfeet.class).get(0).touchingWalkinglocation() == true){
         if (Greenfoot.isKeyDown("D") == false && Greenfoot.isKeyDown("A") == false)
         {
             if ("front".equals(direction)){
             setImage("IdleBack.png");
             getImage().scale(100, 140);
             
            }
            if ("back".equals(direction)){
             setImage("idleFront.png");
             getImage().scale(80, 140);
            }
            if ("left".equals(direction)){
             setImage("idleL.png");
             getImage().scale(50, 140);
            }
            if ("right".equals(direction)){
             setImage("idleR.png");
             getImage().scale(50, 140);
            }
         }
         if(Greenfoot.isKeyDown("W"))
         {
             direction = "front";
             setLocation(getX(), getY()-speed);
             FaceStorage.add("front");
             //setImage(FrontWalk.getCurrentImage());
             //getImage().scale(250, 150);
          }
         if(Greenfoot.isKeyDown("D"))
         {
             direction = "right";
             FaceStorage.add("right");
             setLocation(getX()+speed, getY());
             
             setImage(RightWalk.getCurrentImage());
             
             getImage().scale(250, 150);
             
             
         }
         if(Greenfoot.isKeyDown("A"))
         {
             direction = "left";
             FaceStorage.add("left");
             touched = false;
             setLocation(getX()-speed, getY());
             setImage(LeftWalk.getCurrentImage());
             getImage().scale(250, 150);
         }
         if(Greenfoot.isKeyDown("S"))
         {
             direction = "back";
             
             FaceStorage.add("back");
             setLocation(getX(), getY()+speed);
             //setImage(FrontWalk.getCurrentImage());
             //getImage().scale(250, 150);
          }
        }
    }
    }
    }
}

Super_Hippo Super_Hippo

2022/3/17

#
What is this part of the code trying to achieve?
        if(getWorld().getObjects(enemy.class).size() != 0){
        if(getWorld().getObjects(enemy.class).size() > 1)
        {
            getWorld().removeObject(getWorld().getObjects(enemy.class).get(getWorld().getObjects(enemy.class).size()-1));
        }
     
        }
        if(getWorld().getObjects(enemy.class).size() != 0){
        if(getWorld().getObjects(enemy.class).size() > 2)
        {
            getWorld().removeObject(getWorld().getObjects(enemy.class).get(getWorld().getObjects(enemy.class).size()-1));
        }
     
        }
Aaron-aid Aaron-aid

2022/3/18

#
holy crap im so stupid, thx man
You need to login to post a reply.