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

2018/2/28

More help with my rhythm game

Aura_ Aura_

2018/2/28

#
I realized that the "if(Greenfoot.isKeyDown)" thing registers if it's being held down. I want to make it so that when the player character in my game presses S and Z or X at the same time, they slash downward, and A and Z/X for left, W and Z/X for up, etc. Is there a way to do this? Thank you! (I'm fine with having there be only one button to slash, too - mostly want that so I can make it super fast later on)
danpost danpost

2018/2/28

#
Add a boolean field to indicate a slash key is down:
private boolean slashKeyDown;
Then in the act method (or a method it calls), you can use something like the following:
if (slashKeyDown != (Greenfoot.isKeyDown("z") || Greenfoot.isKeyDown("x")))
{
    slashKeyDown = !slashKeyDown;
    if (slashKeyDown)
    {
        if (Greenfoot.isKeyDown("w")) slashUp(); else if (Greenfoot.isKeyDown("s")) slashDown();
    }
}
The field is used to detect exactly when there is a change in the state of the slash keys (all up or any down). The "w" or "s" key must be pressed prior to the downward change in the slash keys.
Aura_ Aura_

2018/3/2

#
Alright, thank you! So far, I have this:
import greenfoot.*;

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Player()
    {
        image1 = new GreenfootImage("almidle.png");
        image2 = new GreenfootImage("almslashback.png");
        image3 = new GreenfootImage("almslashforward.png");
        image4 = new GreenfootImage("almslashleft.png");
        image5 = new GreenfootImage("almslashright.png");
        image6 = new GreenfootImage("almspecial1.png");
        image7 = new GreenfootImage("almspecial2.png");
        image8 = new GreenfootImage("almspecial3.png");
        setImage(image1);
    }
    int time = 0;
    private boolean slashKeyDown;
    public void act() 
    {
        if (slashKeyDown!= (Greenfoot.isKeyDown("z") || Greenfoot.isKeyDown("x")))
        {
            slashKeyDown = !slashKeyDown;
            if (slashKeyDown)
            {
                if (Greenfoot.isKeyDown("w"))
                {
                    slashUp();
                    setImage(image2);
                } else if (Greenfoot.isKeyDown("s"))
                {
                    slashDown();
                    setImage(image3);
                } else if (Greenfoot.isKeyDown("a"))
                {
                    slashLeft();
                    setImage(image4);
                } else if (Greenfoot.isKeyDown("d"))
                {
                    slashRight();
                    setImage(image5);
                }
            }
        }    
    }   
}
I'm sorry for relying on you so much for this project, but I'm kinda new to this. How do I make a slashUp(), slashDown(), slashLeft(), and slashRight() method? Edit: Never mind, just figured it out. Knew how to do it, just thought the name was different. Whoops. Edit 2: New question. Now that I have the player slashing around, how do I make it so that the arrows only disappear on contact when I'm slashing?
danpost danpost

2018/3/2

#
Aura_ wrote...
Edit 2: New question. Now that I have the player slashing around, how do I make it so that the arrows only disappear on contact when I'm slashing?
It might be as simple as changing line 42 to:
if (slashKeyDown && time == 0)
But, cannot be sure without seeing more of the code within the class.
Aura_ Aura_

2018/3/8

#
I redid the midterm as, looking at the rubric, it's impossible to fit a rhythm game into the guidelines. I now have one night to do the whole thing. Nice, I'm doing a portal-ish platformer with dash mechanics similar to the recently released game Celeste. If you hit a button, you dash in a direction. You can't use it again until you hit the ground. I'm getting the error "Bad operand types for binary operator '&&'" "first type: boolean; second type: int". My code (for the whole player class);
/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    int speed = 2;
    int cooldown = 0;
    /**
     * 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() 
    {
        if (Greenfoot.isKeyDown("d"))
        {
            setLocation(getX()+speed,getY());
            if (isTouching(Walls.class))
            {
                setLocation(getX()-speed,getY());
            }
        }
        if (Greenfoot.isKeyDown("a"))
        {
            setLocation(getX()-speed,getY());
            if (isTouching(Walls.class))
            {
                setLocation(getX()+speed,getY());
            }
        }
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(),getY()-speed);
            if (isTouching(Walls.class))
            {
                setLocation(getX(),getY()+speed);
            }
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(),getY()+speed);
            if (isTouching(Walls.class))
            {
                setLocation(getX(),getY()+speed);
            }
        }
        if (isTouching(Floor.class))
        {
            setLocation(getX()-speed,getY()-speed);
        }    
        if (Greenfoot.isKeyDown("q") && cooldown = 0)
        {
            setLocation(getX()-10,getY());
            cooldown = 1;
        }
        if (Greenfoot.isKeyDown("e") && cooldown = 0)
        {
            setLocation(getX()+10,getY());
            cooldown = 1;
        }
        if (isTouching(Floor.class))
        {
            cooldown = 0;
        }
    }
Thank you for helping! This is the last thing I'll need, I think. Teleportation is easy. (I'd appreciate help with gravity if you can, though.)
Yehuda Yehuda

2018/3/8

#
You need two equal signs (==) when cheking equality, one sign (=) is for storing to a variable. If the topic of your question does not have anything to do with the topic of discussion, then you should start a new discussion for your topic. It would be easier to help if you posted the entire class code (in this case it doesn't make a difference, since no line numbers were mentioned) from the beginning of line 1 to the last place the caret can go. Also, it would be helpful if you showed where the problem is, if your class was bigger it would be hard to find without mention.
You need to login to post a reply.