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

2016/3/3

System runtime code acting strange

bobbyjones2000 bobbyjones2000

2016/3/3

#
Hey Greenfoot community, I have this code for a sort of flappy bird game where the walls (they're called Player) are created on the left in the space code, and in the wall code they move right. At regular intervals, a wall is created. The problem that I have is that at certain intervals, seemingly random numbers of walls are made, not just one. Can anyone see the problem? Also keep in mind I'm not exactly the best at Greenfoot, and I am kind of a newbie.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{

    Long beginTime = System.currentTimeMillis();
 
    public Space()
    {
        super(600, 400, 1);
        addObject(new Enemy1(), 300, 200);
      
    }
         
    public void act()
    {
        if ((System.currentTimeMillis() - beginTime) % 10 == 0)
        {
           addObject(new Player(), 0,200 );
            beginTime = System.currentTimeMillis();
            
        }
    }
    
}
Thank you.
danpost danpost

2016/3/3

#
I believe it would seem like at random times when the walls are made. The code is trying to match a specific interval of time with the occurrence of the act cycle (sort of like the timing of walking on a sidewalk and when you step on a line -- just a bit quicker). I suggest that you use a randomly set timer field with a minimum to determine when to create a wall instead of using the system timer. Doing it this way also keeps all your actors in sync with each other (like when lag occurs due to your system doing miscellaneous background stuff). A sample set-up might be:
// class constants
private static final int TIMER_MIN = 50; // adjust as needed
private static final int TIMER_MAX = 150; // adjust as needed

// instance fields for timer
private int timer = setTimer();
// in act method or in a method the act method calls
timer--;
if (timer == 0)
{
    addObject(new Player(), 0, 200);
    timer = setTimer();
}
// the method to set the timer value
private void setTimer()
{
    timer = TIMER_MIN+Greenfoot.getRandomNumber(TIMER_MAX-TIMER_MIN);
}
Super_Hippo Super_Hippo

2016/3/3

#
In think that the walls in flappy bird always appear after the same time interval, so you don't need to set the timer randomly. By the way, to understand that it didn't make sense what you did, try out this code:
public void act()
{
    System.out.println(System.currentTimeMillis());
}
You can see how fast the number increases and that it needs some kind of luck to let a wall spawn. As the name of the method says, it gets milliseconds. If you wanted to spawn a wall every 10 milliseconds, then there would be 100 new walls each second.
bobbyjones2000 bobbyjones2000

2016/3/7

#
@danpost I tried putting in the code, but in the parenthesis after SetTimer it gives me an error of "incompatible types: void cannot be converted to int."
Super_Hippo Super_Hippo

2016/3/7

#
Change line 12 to 'setTimer();' OR change the 'setTimer' method to the following:
private int setTimer()
{
    return TIMER_MIN+Greenfoot.getRandomNumber(TIMER_MAX-TIMER_MIN);
}
You need to login to post a reply.