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

2018/10/17

Preventing Users to hold down key to gain infinite points

CodingIsNotEasy CodingIsNotEasy

2018/10/17

#
I need help having a key be pressed and only be active for that press then has to be not pressed and pressed again for the code inside work The World Class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private double notePercent = 0.0005;
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200,900, 1); 
        prepare();
    }

    public void act()
    {
        if (notePercent > (int)(Math.random()*100))
        {
            int location = (int)(Math.random()*5);
            Note note = new Note(location);
            if(location == 0)addObject(note,282,0);
            else if(location == 1)addObject(note,442,0);
            else if(location == 2)addObject(note,602,0);
            else if(location == 3)addObject(note,762,0);
            else addObject(note,922,0);
        }
    }

    private void prepare()
    {
        EndLine endline = new EndLine();
        addObject(endline,604,899);        
        TargetLine green = new TargetLine(1,"a");
        TargetLine red = new TargetLine(2,"s");
        TargetLine yellow = new TargetLine(3,"d");
        TargetLine blue = new TargetLine(4,"j");
        TargetLine orange = new TargetLine(5,"k");
        addObject(green,282,819);
        addObject(red,442,819);
        addObject(yellow,602,819);
        addObject(blue,762,819);
        addObject(orange,922,819); 
    }
    
}
The Note Class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Note extends SmoothMover
{
    private double velocity = 0.0 ;
    private int score;
    private boolean myKeyDown;
    public Note(int colorofNote)
    {
        if (colorofNote == -1) setImage("transNote.png");
        else if (colorofNote == 0) setImage("greenNote.png");
        else if (colorofNote == 1) setImage("redNote.png");
        else if (colorofNote == 2) setImage("yellowNote.png");
        else if (colorofNote == 3) setImage("blueNote.png");
        else setImage("orangeNote.png");
    }

    public void act() 
    {
        getWorld().showText("Score:"+score,50,50);
        setLocation (getExactX(),getExactY()- velocity);
        velocity = velocity -0.12;        

        hitDetection();  

        if (isTouching(EndLine.class)) getWorld().removeObject(this);        

    }

    public void movementNotes()
    {

    }

    public void hitDetection()
    {   
        if (isTouching(TargetLine.class) && Greenfoot.isKeyDown("a"))
        {
            score++;   //Testing 
            System.out.println("points: " + score);
 
        }
    }
}
The TargetLine Class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TargetLine here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TargetLine extends Actor
{
    private static int location;
    private static String myKey;

    public TargetLine(int location , String myKey)
    {
        this.location = location;
        this.myKey = myKey;
    }

    public void act() 
    {        
        setImage("transNote.png"); 
        if (Greenfoot.isKeyDown("" + getLocation()) || Greenfoot.isKeyDown(getMyKey()))  setImage(getColor()+"TargetLine.png"); 
        
    }

    public String getColor()
    {
        if (getLocation()==1) return "green";
        else if (getLocation()==2) return "red";
        else if (getLocation()==3) return "yellow";
        else if (getLocation()==4) return "blue";
        else return "orange";
    }

    public static String getMyKey()
    {
        return myKey;
    }
    
    public static int getLocation()
    {
        return location;
    }
}
Super_Hippo Super_Hippo

2018/10/17

#
Try something like this:
private boolean keyDown = false; //save current state of the key


//…
if (keyDown != Greenfoot.isKeyDown(myKey)) //something changed
{
    keyDown = !keyDown; //change
    if (keyDown) //changed from not pressed to pressed
    {
        //do something
    }
}
CodingIsNotEasy CodingIsNotEasy

2018/10/18

#
Also, I forgot to mention what I'm trying to accomplish is a rhythm game that gives point based on how close you pressed the button at the "Target" area. Right now the notes are Random but plan on making them sync to a beat later on. Here is an example of something similar to what i want to make: Youtube Link ]
You need to login to post a reply.