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

2019/4/11

Variable, Hitbox, and Actor problems

NovaEnigma NovaEnigma

2019/4/11

#
Firstly, i have one shuriken that sometimes wont even appear at the start of the game or when it hits the boss and i have variables that won't reset when i start my game. Secondly, whenever i try to fix these problems, it creates more problems. Finally, there are some hitbox issues i have and everything i read doesn't make sense unless i see code with my game actors and code. plz help
NovaEnigma NovaEnigma

2019/4/11

#
Shuriken Class
public class Shuriken extends Character
{
    static int move=0;
    /**
     * Act - do whatever the Shuriken wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        delete();
        hide();
        thrown();
        hurt();
    }    
    public Shuriken()
    {
        GreenfootImage image = getImage();  
        image.scale(30, 30);
        setImage(image); 
    }
    public void thrown()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
       if(Greenfoot.mouseClicked(null))
       {
           move=1;
           setRotation(90);
           turnTowards(mouse.getX(), mouse.getY());
       }
       if(move==1)
       {
           move(5);
        }
       
    }
    public void delete()//deletes the shuriken if touching the world edge
    {
        if(getX()<10||getX()>590||getY()<10||getY()>390)
        {
            getWorld().removeObject(this);
            Character.shuriken=0;
        }
    }
    public void hide()//hides the shuriken from the start screen
    {
        if(MyWorld.screen==0)
        {
            getImage().setTransparency(0);
        }
        else
        {
            getImage().setTransparency(255);
        }
    }
    public void hurt()
    {
        if(isTouching(Boss.class))
        {
            getWorld().removeObject(this);
            Boss.bosshealth=Boss.bosshealth-1;
        }
    }
}

Character Class
public class Character extends Actor
{
    static int shuriken=0;
    /**
     * Act - do whatever the Character wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        run();
        threw();
        
    }    
    public void run()//allows character to move
    {
        if (Greenfoot.isKeyDown("a"))
        {
            move(-7);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            move(7);
        }
    }
    public void threw()
    {
       MouseInfo mouse = Greenfoot.getMouseInfo();
       if (mouse==null)return;
       if(shuriken==0)
       {
       if(Greenfoot.mouseClicked(null))
       {
           getWorld().addObject(new Shuriken(), getX() , getY());
           shuriken=1;
       }
    }
    
    
    }
}

Boss Class
public class Boss extends Actor
{
    static int walltouch=0;
    static int bosshealth=10;
    /**
     * Act - do whatever the Boss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hide();
        fly();
        atWorldEdge();
        attack1(); 
    }  
    public void hide()//hides the boss from the start screen
    {
        if(MyWorld.screen==0)
        {
            getImage().setTransparency(0);
        }
        else
        {
            getImage().setTransparency(255);
        }
    }
    public void fly()//allows the boss to move around and use fire0
    {
        move(5);
        Fireball.loop1=1;
        if(atWorldEdge())
        {
            turn(180);
            getImage().mirrorVertically();
            walltouch++;
        }
        if(walltouch==6)
        {
            turnTowards(300,100);
            if(getX()==300 && getY()==100)
            {
            setRotation(0);
            move(5);
            walltouch=0;
            Fireball.loop1=1;
        }
        }
    }
    public boolean atWorldEdge()//checks to see if the boss is at world edge
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    public void attack1() //movement for the first attack
    {
        if(walltouch==5 && getX()==300)
        {
            turnTowards(300, 400);
        }
        if(getX()==300 && getY()==240)
        {
        int i = 0;
        for (i=0; i<70; i++)
        {
           move(0);  
        }
        }
        }
}

Fireball Class
public class Fireball extends Boss
{
    private int loop0=0;
    static int loop1=0;
    
    /**
     * Act - do whatever the Fireball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hide();
        fire0();
        fire1();
        delete();
        //hurt();
    }    
    public Fireball()//sets the size of the fireball
    {
        GreenfootImage image = getImage();  
        image.scale(60, 60);
        setImage(image);
    }
    public void fire1()//attacks the character with 4 fireballs
    {
        Actor Boss = getWorld().getObjects(Boss.class).get(0);
        Actor Character = getWorld().getObjects(Character.class).get(0);
    if (Boss.getX()==300 && Boss.getY()==240)
    {
        getImage().setTransparency(255);
    }
    if(loop0==0)
    {
       turnTowards(Character.getX(), Character.getY());
       loop0=1;
    }
    move(5);
    }
    public void fire0()
    {
        Actor Boss = getWorld().getObjects(Boss.class).get(0);
        Actor Character = getWorld().getObjects(Character.class).get(0);
        loop1=0;
        if (loop1==1)
        {  
            turnTowards(Character.getX(), Character.getY());
            loop1=0;
        }
        move(5);
    }
    public void delete()
    {
        if(getX()<5||getX()>595||getY()<5||getY()>395)
        {
            getWorld().removeObject(this);
        }
        
    }
    
}
NovaEnigma NovaEnigma

2019/4/11

#
UPDATE: I've fixed my problem with the shuriken and variables already tho it is not a very good fix, it is one. I now only need to work on a hitbox for the fireball because when it does hit the character, it isnt even touching the character. So if someone could help that would be great
Super_Hippo Super_Hippo

2019/4/11

#
Removing every "static" from your code would be a good start. None of these variable should be a class variable. Also, a Shuriken is not a Character and a Fireball is not a Boss. So they shouldn't be subclasses.
NovaEnigma wrote...
Finally, there are some hitbox issues i have and everything i read doesn't make sense unless i see code with my game actors and code.
I don't really understand this sentence. In your code, I only see the Shuriken checks if it hit the Boss (hurt method). Obviously it will also hurt the boss if it is only hitting a Fireball because your Fireball is a subclass of Boss... I can't find where the Fireball is damaging the Character. However, "hitbox" issues usually happen when images have transparent areas around the visible image. Or if the images have nothing in common with a rectangle. Images are always rectangles, so if your image has a shape of an X and the rest of it is transparent, then the transparent parts still cause intersections with other Actors.
You need to login to post a reply.