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

2018/10/1

Taking the Private Variable of an actor, once clicking on it.

Hyperbaba Hyperbaba

2018/10/1

#
I'm creating a Guitar Hero for my school, and I am having trouble taking the private Variable of a setting actor by clicking on it. The line with a lot of dashes on it is causing me the most problems. Thanks -Hyperbaba
World Class
public class MyWorld extends greenfoot.World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    static int noteMissed, noteHit, noteStreak,acts, random, total, input;
    static int speed = (int)(Math.random()*35 + 15);
    static int ex;
    private int increase = 50;
    static boolean start = false;
    boolean gameStart = true;
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int[] setting = {100,75,50};

    // String[] songs = {"Song.mp3"};
    GreenfootSound backGroundMusic = new GreenfootSound("Song.mp3");
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 800, 1); 
        Prepare();
    }

    public void Prepare(){
        for(int x = 1; x <= 3; x ++){
            Setting set = new Setting(x);
            addObject(set,200+(400*(x-1)) ,400);
        }
    }

    public void Prepare2(){
        for(int x = 1; x <= 5; x++){
            Button button = new Button(x);
            addObject(button,386+107*(x-1), 707);
        }
        int noteMissed = 0;
        int noteHit = 0;
        int noteStreak = 0;
        int acts = 0;    
        int random = 0;
    }

    public void act(){
    ------------------------------------------------    ex = setting[( ((Setting)(mouse.getActor())) .returnNum())-1];
        if(Setting.returnStart()){
            //ex = setting[( ((Setting)(mouse.getActor())) .returnNum())-1];
            if(gameStart == true){
                Prepare2();
                start = true;
                setBackground("fret.jpg");
                // backGroundMusic.playLoop();
                //ex = setting[( ((Setting)(mouse.getActor())) .returnNum())-1];
                gameStart = false;
            }
            if(acts % speed == 0){
                random = (int)(Math.random()*5)+1;
                Note notee = new Note(random);
                addObject(notee,526+36*(random-1),345);
                total++;
                speed = (int)(Math.random()*ex + 5);
            }
            showText("Note Streak:" + noteStreak, 1100, 100);
            showText("Notes Hit:" + noteHit + "/" + total, 100, 100);
            showText("Notes Missed:" + noteMissed, 100, 200);
            if(noteStreak >= increase){
                ex -= 2;
                increase += 10;
            }
            acts++;
        }

    }

    public static void setMiss(){
        noteMissed ++;
    }

    public static void setHit(){
        noteHit ++;
        noteStreak ++;
    }

    public static void reset(){
        noteStreak = 0;
    }

    public void startMusic(){
        backGroundMusic.playLoop();
    }

    public static boolean Starting(){
        return start;
    }

    public int returnEX(){
        return ex;
    }

}
--------------------------------------------
//Setting class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Setting here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Setting extends Actor
{
    /**
     * Act - do whatever the Setting wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int num;
    static int num2;
    MyWorld myWorld = (MyWorld) getWorld();
    static boolean  start= false;
    public Setting(int x){
        num = x;
        num2 = x;
        setImage("diff" + x + ".jpg");
    }

    public void act() 
    {
        if(Greenfoot.mouseClicked(this)){
           // myWorld.setEX(returnNum());
            start = true;
        }
        if(myWorld.Starting()){
            getWorld().removeObject(this);
        }
    }    
    
    static public int returnNum(){
        return num2;
    }
    
    static boolean returnStart(){
        return start;
    }
}
danpost danpost

2018/10/1

#
(1) you should avoid using 'static' variables and method unless you need to retain a value during a scenario reset (like for a session high score, for example); (2) lines 15 and 121 are problems in that the will be set to values prematurely; these line are set when the object of their respective classes are created and do not automatically update; remove those lines and get the values from within the act method when needed; It would be easier to check for Actor object clicks in your MyWorld act method. You could start with the following classes:
/** The MyWorld class */
import greenfoot.*;

public class MyWorld extends World
{
    private int ex;
    private int noteMissed, noteHit, noteStreak, acts, random, total, input;
    private int speed = 15+Greenfoot.getRandomNumber(35);
    private int increase = 50;
    
    public MyWorld()
    {
        super(1200, 800, 1);
        prepare1();
    }

    private void prepare1()
    {
        for (int i=0; i<3; i++)
        {
            Actor set = new SimpleActor();
            addObject(set, 200+i*400 , 400);
            set.setImage("diff"+(i+1)+".jpg";
        }
    }
    
    private void prepare2()
    {
        // prepare 2 code here (lines 35 through 43)--NO 'int' for lines 39 through 43
    }
    public void act()
    {
        if (ex == 0)
        {
            if (Greenfoot.mouseClicked(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                Actor mouseActor = mouse.getActor();
                if (mouse.Actor != null)
                {
                    ex = (new int[] { 100, 75, 50 })[mouseActor.getX()/400];
                    prepare2();
                }
            }
        }
        else
        {
            // started code here (lines 58 through 72)
        }
    }
}

/** The SimpleActor class */
public class SimpleActor extends greenfoot.Actor {}
Hope this helps.
You need to login to post a reply.