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

2021/1/13

i get stuck by my invisible walls lmao

Sakuya Sakuya

2021/1/13

#
So in my game there are invisible walls but when I fly down I get stuck in them and only can move left and right after that, her is the code.
public void move (String direction)
    {
        if(direction.equals("Up"))
        {
            if(getY()+speed<550)
            {
              setLocation(getX(), getY() - speed);
            }
        }
        if(direction.equals("left"))
        {
            if(getX()-speed<560)
            {
               setLocation(getX() - speed, getY());
            }
        }
        if(direction.equals("down"))
        {
            if(getY()-speed<550)
            {
              setLocation(getX(), getY() + speed);
            }
        }
        if(direction.equals("right"))
        {
            if(getX()+speed<500)
            {
                setLocation(getX()+speed, getY());
            }
        }
    }
danpost danpost

2021/1/13

#
Change "Up" in line 3 to "up".
Sakuya Sakuya

2021/1/14

#
didnt work, I still get stuck when im near the ground (I can fly normaly but if I go to deep I get stuck)
danpost danpost

2021/1/14

#
Sakuya wrote...
didnt work, I still get stuck when im near the ground (I can fly normaly but if I go to deep I get stuck)
Need to provide more code (of class above and maybe class of wall).
Sakuya Sakuya

2021/1/14

#
Its at line 72 and also can you take a look at the powerBar class (line 169) for some reason only the first if code works. But thanks for the help so far I really apreciate it. Edit: I continued trying to play it afterwards and it worked, it seems for some reason to pause the game few times while the boss enters the stage
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Reimu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Reimu extends Actor
{
    private int speed = 4;
    private Hitbox hb;
    private int power;
    private int bombDelay = 0;
    private Counter counter;
    private Powerbar powerbar;
    private Portrait portrait;
    private SpellCards[] bomb = new SpellCards[4];
    private GreenfootSound powerSnd = new GreenfootSound("powerup.wav");
    private GreenfootSound bulletSnd = new GreenfootSound("bulletfire.wav");
    private GreenfootSound bombSnd = new GreenfootSound("spellactivate.wav");
    private GreenfootSound deathSnd = new GreenfootSound("death.wav");
    private GreenfootSound itemSnd = new GreenfootSound("itemget.wav");
    private int bombCount = 3;
    private boolean secondBomb = false;
    long cooldownShot = 15;
    private int animTimer = 0;
    private int SpellCardsCount = 3;
    private boolean cooldown = false;
    private boolean invuln = false;
    private boolean init = true;
    public void act() 
    {
        if (init == true)
        {
            initialize();
            init = false;
        }
        checkKeys();
        shift();
        shoot();
        powerBar();
        hb.setLocation(getX() + 2, getY());
        collectPowerup();  
        checkCollissions();
    }
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("W"))
        {
            move("Up");
        }        
        else if(Greenfoot.isKeyDown("A"))
        {
            setImage("reimuTurnL.png");
            move("left");
        }   
        else if(Greenfoot.isKeyDown("S"))
        {
            move("down");
        }
        else if(Greenfoot.isKeyDown("D"))
        {
            setImage("reimuTurnR.png");
            move("right");
        }
        else
        {
            setImage("reimu0.png");
        }      
    }
    public void move (String direction)
    {
        if(direction.equals("up"))
        {
            if(getY()+speed<550)
            {
              setLocation(getX(), getY() - speed);
            }
        }
        if(direction.equals("left"))
        {
            if(getX()-speed<560)
            {
               setLocation(getX() - speed, getY());
            }
        }
        if(direction.equals("down"))
        {
            if(getY()-speed<550)
            {
              setLocation(getX(), getY() + speed);
            }
        }
        if(direction.equals("right"))
        {
            if(getX()+speed<500)
            {
                setLocation(getX()+speed, getY());
            }
        }
    }
    public void shift()
    {
        if (Greenfoot.isKeyDown("shift"))
        {
            speed = 2;
        }else{
            speed = 4;
        }
    }
    public void initialize()
    {
        hb = new Hitbox();
        counter = new Counter();
        powerbar = new Powerbar();
        SakuyasWorld sk =(SakuyasWorld)getWorld();
        sk.addObject(hb, getX(), getY());
        sk.addObject(counter, 701, 228);
        sk.addObject(powerbar, 701, 228);
        counter.setValue(30);
        power = counter.getValue();
    }
    public void shoot()
    {
        if(Greenfoot.isKeyDown("p"))
        {
            bulletSnd.setVolume(80);
            bulletSnd.playLoop();
            spawnShoot();
        }       
        if(!Greenfoot.isKeyDown("p"))
        {
            bulletSnd.stop();
        }
    }
    public void spawnShoot()
    {   
             if(System.currentTimeMillis() - cooldownShot > 50)
        {
            getWorld().addObject(new Shot(),getX(), getY());
            cooldownShot = System.currentTimeMillis();
        }
    }
    public void spellCard()
    {

    }
    public void  collectPowerup()
    {
            if(this.isTouching(Powerup.class))
            {
              itemSnd.setVolume(80);
              itemSnd.play();
              removeTouching(Powerup.class);
              SakuyasWorld sk = (SakuyasWorld)getWorld();
              sk.addScore(100);
            
              if (counter.getValue() < 101)
              {
                counter.setValue(counter.getValue() + 1);
              }
              if (counter.getValue() == 60 || counter.getValue() == 100)
              {
                powerSnd.play();
              }
           }
    }
    public void powerBar()
        {
        if (power < 30)
        {
            powerbar.setImage("powerbar0.png");
            counter.getImage().setTransparency(255);
        }
        if (power >= 30 && power < 60)
        {
            powerbar.setImage("powerbar1.png");
            counter.getImage().setTransparency(255);
        }
        if (power >= 60 && power < 90)
        {
            powerbar.setImage("powerbar2.png");
            counter.getImage().setTransparency(255);
        }
        if (power > 90 && power < 100)
        {
            powerbar.setImage("powerbar3.png");
            counter.getImage().setTransparency(255);
        }
        if (power == 100)
        {
            powerbar.setImage("powerbar4.png");
            counter.getImage().setTransparency(0);
        }
    }
    public void checkCollissions()
    {
        if (this.isTouching(Enemys.class) || this.isTouching(Sakuya.class))
        {
            if (invuln == false)
            {
                deathSnd.setVolume(80);
                deathSnd.play();
                SakuyasWorld sk = (SakuyasWorld)getWorld();
                sk.removeObject(hb);
                Greenfoot.stop();
                sk.removeObject(this);
            }
        }
    } 
}
danpost danpost

2021/1/14

#
Your int power field is the problem. It seems to be there to mirror the value of the Counter object, which is an unnecessary complication. Remove lines 13 and 122. Then change all instances of "power " to "counter.getValue() ".
Sakuya Sakuya

2021/1/14

#
Thanks that worked, I still get stuck in the bottem but I guess that isnt that fatal. But anyways you helped a lot I cant thank you enough !
You need to login to post a reply.