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

2014/3/19

Counter to Multiples of 5, problems referencing world and other problems

DarkMageCkmario DarkMageCkmario

2014/3/19

#
I'm attempting to make a counter that counts to multiples of 5 keypresses, then spawns a box.
public void keyspawn()
    
    {
        int keycount = 0;
        int multy = 1;
        if (Greenfoot.isKeyDown("left")||  Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("right"))
        keycount++;
        
        if (keycount == 5*multy)
        {spawnfall();
        spawnrand();
        multy++;}
        
        
        
    }
it's stored in the world class, and it does not do what it says it's going to do (which is spawn 2 enemies at multiples of 5 keypresses. when i use the method in the player superclass it gives me an error saying you can't use the methods there. i know that's because you can't reference a world method that A: spawns a new object in a player object, and B:the methods are not defined in the class. A little help making this work would be amazing Thanks in advance
danpost danpost

2014/3/19

#
The problem is that every time the method is called, the local variables 'keycount' and 'multy' are being created and set to '0' and '1' respectively. At the end of the method, 'keycount' may end up to be '1'; but, the next call to the method will again re-create the two variables, setting their values to '0' and '1'. Local variables like those declared on lines 4 and 5, have no life-span beyond the current execution of the method. Once the method is done executing, those variables are non-existent (for all intensive purposes). Fields, or variables declared within the class but not within a method or constructor, are retained throughout the life of the object with which they are assigned (refer to my Overview: Subclassing in Greenfoot scenario).
DarkMageCkmario DarkMageCkmario

2014/3/20

#
okay, counter works, more problems seem to be arising. Such as the spawning methods not working at all, I am calling the method, and it SHOULD work, there doesn't seem to be any problems with the code, it should do what it says it should. it's a lot of code for a small post.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Where the Magic Happens
 * 
 * by DMGCK 
 * beta1.0
 * 
 * feel free to modify the for loop
 */
public class bg extends World
{

    /**
     * Constructor for objects of class bg.
     * 
     */
    public bg()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1); 
       int i = 0;
       int end = 25;
       for (i=0;i<end;i++) //spawns end amount of boxes.
       spawnrand();
       Greenfoot.delay(1);
       spawnpl();
       keyspawn();
    }
       
       
    public void spawnfall()
    {
    addObject(new fallbox(),Greenfoot.getRandomNumber(600),120);    
    }
    
    public void spawnrand()
    {
    addObject(new randbox(),Greenfoot.getRandomNumber(600),Greenfoot.getRandomNumber(600));    
    }
    
    public void spawnrandps()
    {
    addObject(new randbox(),120,120);    
    }
    
    public void spawnpl()
    {
    addObject(new controllable(),80,80);    
    }
    
    
    int keycount = 0; //thanks danpost for telling me to move the variables
    int multy = 1;
    
    
    public void keyspawn()
    
    {
        if (Greenfoot.isKeyDown("left")||  Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("right"))
        keycount++;
        
        if (keycount == 5*multy)
        {spawnfall();
        spawnrand();
        multy++;}
        
        
        
    }

}
danpost danpost

2014/3/21

#
What you are saying is that it does not spawn anything after the initial 25 randbox objects and one controllable after you press 'Run' and try to spawn using the arrow keys Well, your code does not say anything about doing anything when the scenario is running. There is no 'act' method anywhere. Line 26 has little effect when used in the world constructor if the constructor is only to be executed while the scenario is stopped (or initializing/resetting). Also, line 28, calling 'keyspawn' will not accomplish anything from the constructor of the world (considering what the method is doing).
You need to login to post a reply.