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

2020/12/7

Trouble with getters

Hissss Hissss

2020/12/7

#
Hey all, I'm fairly new to Java and am getting some issues with implementing getters properly. I am designing a game where the character, kara, cannot pass rocks and although I implemented it in a somewhat messy way (simply tossing the character backwards instead of cutting off that direction), it is the best I can come up with. However, I also want to include a few powerups including a speed powerup where the character can go faster and this is the issue: I need to boost that backwards kicking based on the speed so the character cannot phase through. I keep getting a "cannot find symbol" error even though the character's class has the method in it. Here's the code for the rock:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Rock extends Actor
{
    boolean touch = false;
    public void act() 
    {
        getImage().scale(100, 110);
        touch();
    }
    public void touch(){
        Actor kara = null;
        Actor enemy = null;
        Actor projectile = null;
        int speed = 0;
        
        if (getOneIntersectingObject(Kara.class) != null)
        {
            kara = getOneIntersectingObject(Kara.class);
        }
        if (kara != null){
            int speed = kara.getSpeed();
            kara.move(-4);
        }
        
        if (getOneIntersectingObject(Enemy.class) != null)
        {
            enemy = getOneIntersectingObject(Enemy.class);
        }
        if (enemy != null){
            enemy.move(-2);
        }
        
        if (getOneIntersectingObject(Projectile.class) != null)
        {
            projectile = getOneIntersectingObject(Projectile.class);
        }
        if (projectile != null){
            World world;
            world = getWorld();
            world.removeObject(projectile);
        }
    }
}
And here's the code for the main character:
import greenfoot.*;

Any and all help is well appreciated!

/**
 * This class creates a user-controlled bug that traverses a 3x3 grid of worlds
 */
public class Kara extends Actor
{   
    World world;
    private int attSpeed = 50;
    private int speed = 3;
    private int attSpeedDelay = 0;
    /**
     * Sizes image this actor
     */
    public Kara()
    {
        getImage().scale(30, 30);
    }
    public int returnAngle(){
        return getRotation();
    }
    public int getSpeed(){
        return speed;
    }
    /**
     * Gets user input to control movement and rotation
     */
    public void act()
    {
        int dx = 0, dy = 0, x = 0, y = 0;
        if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")){
            dx--;
        }
        if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")){
            dx++;
        }
        if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")){
            dy--;
        }
        if (Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")){
            dy++;
        }
        if (Greenfoot.isKeyDown("space")){
            fire();
        }
        
        attSpeedDelay++;
        
        
        if (dx == 0 && dy == 0){
            return;
        }
        setLocation(getX()+dx*speed, getY()+dy*speed);
        setRotation((90-90*dx)*dx*dx*(1-dy*dy)+(90*dy)*dy*dy*(1-dx*dx)+(135-90*dx)*dx*dy);
    }
    private void fire()
    {
        if(attSpeedDelay >= attSpeed){
            int angle = getRotation();
            Projectile proj = new Projectile();
            getWorld().addObject(proj, getX(), getY());    
            proj.setRotation(angle);
            attSpeedDelay = 0;
        }
    }
    
}
danpost danpost

2020/12/7

#
Have Kara look for rock after moving. Do not have a rock look for Kara.
Hissss Hissss

2020/12/7

#
Thank you so much! I just came to that conclusion myself and your confirmation really helps!
You need to login to post a reply.