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

2017/2/16

New Ball after one Timer Delay

wueschn wueschn

2017/2/16

#
I have a problem with my timer. I have set a timer variable to 70 and after this time delay a new ball should be created. It works, but after a certain time there are too many balls. So I intend that after 200 balls are created, all balls except one should be deleted and all starts from beginning on. I tried to solve the problem with a List that counts all the ball objects. But I don`t manage it .... Thanks a lot!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Balls extends Actor
{
    private int intervall = 70;
    public void act() 
    {
        this.move(4);
        this.changingDirection();
        this.controllingTimer();
    } 

    public void changingDirection()
    {
        if(this.isAtEdge())
        {
            this.turn(180);
        }
    }

    public void controllingTimer()
    {
        if(intervall > 1)
        {
            this.intervall--;
        }
        else
        {
            int x = Greenfoot.getRandomNumber(580);
            int y = Greenfoot.getRandomNumber (380);
            MyWorld mw = this.getWorldOfType(MyWorld.class);
            mw.addObject(new Balls(), x, y);
            intervall = 70;

        }
    }
}
Nosson1459 Nosson1459

2017/2/17

#
Use the getObjects method to get all the Balls and then get the size of the list, when it equals 200 remove all the balls and add one new ball. This is if you want a maximum of 200 balls on your screen, because each ball that you add will be creating it's own new balls so you will reach 200 pretty fast. If you want to have each added ball create 200 balls then use an int variable in Balls for the amount of created balls (with this, the amount of Balls on your screen at once will be a lot). Just by the way why do you use the keyword "this" so much when it's really unnecessary, because in an object, unless you specify otherwise, any method or variable is automatically going to be called from that object or the object it extends. And even if you have the same method in the class you're coding in and the one it extends the one in the current class will override the one in the parent class. So if you have any specific reason for constantly using the keyword "this" please tell me because I've seen other people do that also and am curious to know if there's a reason behind it.
valdes valdes

2017/2/17

#
Adding to what @Nosson1459 says. Variable intervall, method controllingTimer() and the call to that method don't belong in Balls class. They should be in the world class, so only the world spawns balls, not each ball spawning more balls.
valdes valdes

2017/2/17

#
To your last note @Nosson1459 I use "this" to avoid writting. I only write "this." and <Ctrl>+<Space> to get the list of methods that I can use :) In Greenfoot that works without "this." and only shows methods, in Eclipse it automatically shows the list of variables and methods. Other than that, I agree with you, it is not needed.
valdes valdes

2017/2/17

#
@wueschn, a piece of advice Do not use line 30 in ANY Greenfoot project. MyWorld mw = this.getWorldOfType(MyWorld.class); For some reason, that line works offline with no problem. But when you upload your game into this site, that line crashes ALWAYS. use
MyWorld mw = (MyWorld)this.getWorld();
instead.
valdes valdes

2017/2/17

#
Done
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class WhateverWorld extends World
{
    private int intervall = 70;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public WhateverWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(new Ball(), 20, 20);
    }
    
    public void act() 
    {
        this.controllingTimer();
        this.checkNumberOfBalls();
    } 

    public void controllingTimer()
    {
        if(intervall > 1)
        {
            this.intervall--;
        }
        else
        {
            int x = Greenfoot.getRandomNumber(580);
            int y = Greenfoot.getRandomNumber(380);
            addObject(new Ball(), x, y);
            intervall = 70;
        }
    }
    
    public void checkNumberOfBalls() {
        List list = this.getObjects(Ball.class);
        if (list.size() >= 200) {
            this.removeObjects(list);
            addObject(new Ball(), 20, 20);
        }
    }
}
wueschn wueschn

2017/2/17

#
@nosson1459: Thanks a lot for your answer. I use 'this' because that is how I learned Java and when I use Netbeans and type in 'this', all methods are shown @valdes: your solution works perfect, thanks a lot!! But I have to ask you: why is it wrong to use (except the fact that it makes the online version crash ;-)) the first line. Isn`t it more precise than doing a type casting?? I mean is it wrong in a logical context?? MyWorld mw = this.getWorldOfType(MyWorld.class); instead of MyWorld mw = (MyWorld)this.getWorld(); Greets and thanks to you!!
valdes valdes

2017/2/18

#
No, it is not wrong at all, the problem is that function getWorldOfType() wasn't recognized on the online version of Greenfoot (3.0.4). I think they fix that bug in version 3.1.1.
Nosson1459 Nosson1459

2017/2/19

#
wueschn wrote...
I use 'this' because that is how I learned Java and when I use Netbeans and type in 'this', all methods are shown
Thanks (similar to what valdes said about Ctrl+Space in Greenfoot).
wueschn wueschn

2017/2/19

#
@Nosson1459 @valdes Ok, thanks again. I am very happy (and it is a great honor to me) to meet so many experts in this forum that give help on such a high level!!
Nosson1459 Nosson1459

2017/2/20

#
wueschn wrote...
@Nosson1459 @valdes Ok, thanks again. I am very happy (and it is a great honor to me) to meet so many experts in this forum that give help on such a high level!!
thanks for the compliment!!!!!
You need to login to post a reply.