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

2013/10/15

Help with Adding Object

1
2
gdwd3 gdwd3

2013/10/15

#
I'm trying to get my Player.class object to add Ball.class which it can do, but when I try the following it says "cannot find symbol - variable deathwall"
getWorld().addObject(new Ball(deathwall), Greenfoot.getRandomNumber(503)+48, 17);
I am trying to make the ball able to access int's in my DeathWall.class please help
danpost danpost

2013/10/15

#
Where is deathwall declared? how is it declared? does the Player 'know' what 'deathwall' is? what ints are you trying to access? how many DeathWall objects can be in the world at one time?
gdwd3 gdwd3

2013/10/15

#
deathwall is a separate class. the player class has no reference to deathwall but I get the same problem if a give it the reference. I am trying to access an int that is:
public int numberSpeed;
there are two deathwalls and no more deathwalls are added.
danpost danpost

2013/10/15

#
OK, you have a DeathWall class that you create two DeathWall object from. Each of these objects will have its own 'numberSpeed' field that can contain different values. The class itself does not have a 'numberSpeed' field with a value. It only knows that each instance will require a 'numberSpeed' field and assigns one to each instance created from the class. Now, why exactly does the ball need to know the value of 'numberSpeed', and from which DeathWall object should it be gotten from?
gdwd3 gdwd3

2013/10/16

#
The Ball is using the DeathWall basically as a clock as Balls added randomly over time so it can't have a timer:
numberSpeed++;
So i'm using the deathWall as the timer and the ball need to know that number to be able to decide what speed to move at. Would it help if there was only one DeathWall in the world?
danpost danpost

2013/10/16

#
It seems there would be an easier way to do what you want to do. Can you post the entire DeahWall class code here, please.
gdwd3 gdwd3

2013/10/16

#
The DeathWall does many jobs in the game but i have included what the ball need to know below: P.S: the varible isn't numberSpeed sorry its numberTime.
public int numberTime;

/**
*Timer for the ball class to be able to know when to speed up.
*/
public void Time()
{
    numberTime++;
}
The ball will use this to know when to speed up. For example:
/**
* In Ball.class
*/

public int speed;

public void speed()
{
    if(numberTime <= 100)
    {
        speed = 2
    }
}
if you can see an easier way please share it with me and any other help is welcome.
danpost danpost

2013/10/16

#
I asked for the entire DeathWall class so I know what we are dealing with.
gdwd3 gdwd3

2013/10/17

#
I have changed some things and the timer is now in the player. I have got the balls linked to the player but when I try to refer to the timer it says: "non-static variable timer cannot be referenced from a static context" I have put code below: Player.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public int count;
    public int timer;
    
    /**
     * 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() 
    {
        CheckMove();
        CheckWall();
        AddBall();
        CheckDeath();
        Time();
    }    
    
    public void CheckMove()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
        }
    }
    
    public void CheckWall()
    {
        if(canSee(LeftWall.class))
        {
            setLocation(getX()+2, getY());
        }
        
        if(canSee(RightWall.class))
        {
            setLocation(getX()-2, getY());
        }
    }
    
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    public void AddBall()
    {
        if(count == 30)
        {
            getWorld().addObject(new Ball(this), Greenfoot.getRandomNumber(503)+48, 17);
            getWorld().addObject(new Ball(this), Greenfoot.getRandomNumber(503)+48, 17);
            count = 1;
        }
        else
        if(count != 30)
        {
            count++;
        }
    }
    
    public void CheckDeath()
    {
        if(canSee(Ball.class))
        {
            setLocation(586, 36);
        }
    }
    
    public void Time()
    {
        timer++;
    }
}
Ball.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    public int Time;
    public int Speed;

    public Ball(Player speedTime)
    {
        
    }
    
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        SetSpeed();
        Move();
    }    
    
    public void SetSpeed()
    {
        Time = Player.timer;
    }
    
    public void Move()
    {
        setLocation(getX(), getY()+Speed);
    }
}
Please help
danpost danpost

2013/10/17

#
In line 31 of the Ball class, you need the player object (not the player class). If you only have one player in the world, you can use:
Player player = (Player)getWorld().getObjects(Player.class).get(0);
gdwd3 gdwd3

2013/10/18

#
How do I use this in my code? Can you give a copy of Ball.class with the required changes?
danpost danpost

2013/10/18

#
Replace line 31 with the line I provided and follow it with:
Time = player.timer;
Notice the lowercase 'p', which makes it refer to the Player object obtained from the line before it, instead of the Player class. You may want to review this discussion thread to understand why it has to be done this way.
gdwd3 gdwd3

2013/10/18

#
Thanks, This has worked so problem solved :D Thanks for all the help. By the way if a type: timer++; and timer started on 0 what would the number be every second?
danpost danpost

2013/10/18

#
The nominal range of frames per second for a standard scenario is between 50 and 60. At 50 fps, the timer would be at { 50, 100, 150, 200 ... } each second.
gdwd3 gdwd3

2013/10/18

#
I am trying to change the speed of Ball.class The code is below, can you help, when I run this, it is instantly speed 8. Why is this? Code below: Player.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public int count;
    public int Timer;
    
    /**
     * 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() 
    {
        CheckMove();
        CheckWall();
        AddBall();
        CheckDeath();
        Time();
    }    
    
    public void CheckMove()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
        }
    }
    
    public void CheckWall()
    {
        if(canSee(LeftWall.class))
        {
            setLocation(getX()+2, getY());
        }
        
        if(canSee(RightWall.class))
        {
            setLocation(getX()-2, getY());
        }
    }
    
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    public void AddBall()
    {
        if(count == 30)
        {
            getWorld().addObject(new Ball(this), Greenfoot.getRandomNumber(503)+48, 17);
            getWorld().addObject(new Ball(this), Greenfoot.getRandomNumber(503)+48, 17);
            count = 1;
        }
        else
        if(count != 30)
        {
            count++;
        }
    }
    
    public void CheckDeath()
    {
        if(canSee(Ball.class))
        {
            setLocation(586, 36);
        }
    }
    
    public void Time()
    {
        Timer++;
    }
}
Ball.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    public int speed;
    public int Time;
    
    public Ball(Player timer)
    {
    }
    
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Move();
        Speed();
    }    
    
    public void Speed()
    {
        Player player = (Player)getWorld().getObjects(Player.class).get(0); 
        Time = player.Timer;
        
        if(Time >= 10000)
        {
            speed = 2;
        }
        else
        if(Time <= 10001 && Time >= 20000)
        {
            speed = 3;
        }
        else
        if(Time <= 20001 && Time >= 30000)
        {
            speed = 4;
        }
        else
        if(Time <= 30001 && Time >= 40000)
        {
            speed = 5;
        }
        else
        if(Time <= 40001 && Time >= 50000)
        {
            speed = 6;
        }
        else
        if(Time <= 50001 && Time >= 60000)
        {
            speed = 7;
        }
        else
        if(Time <= 60001)
        {
            speed = 8;
        }
    }
    
    public void Move()
    {
        setLocation(getX(), getY()+speed);
    }
}
Please help!
There are more replies on the next page.
1
2