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

2013/2/19

HELP! "Connect Four" Add Object with Key???

1
2
Prish950 Prish950

2013/2/19

#
So I´ve got one problem. I want to add every time when the player hit 1 that a greenstone is spawning on the first cell and I want that every time when the player hit q that a red stone is spawning on the first cell. I wrote this:
public void act() 
  {

      if (Greenfoot.isKeyDown("1"))
      {
          getWorld().addObject(new redStone(), 1,0); 
          
      }
      
     
Probably this works but there is one fail in it, if I press to long the Key it is going to spawn many of red stones. How can i fix it ?? I just want that there is just one stone spawning???
danpost danpost

2013/2/19

#
Using 'isKeyDown' is intended for action that are to be continuously performed. Use the 'getKey' method to perform one action per keystroke. Bear in mind that you can only use it once per act method, as any successive call to 'getKey' will return 'null'. Therefore, if checking multiple keys, you need to save the first return of 'getKey' in a local field.
String key = Greenfoot.getKey();
if ("1".equals(key)) // act on '1'
if ("q".equals(key)) // act on 'q'
// etc.
By the way, would it not be better to keep track of whose move it it and use the same key for both?
danpost danpost

2013/2/19

#
Even better might be:
String key = Greenfoot.getKey();
if (key != null)
{
    if (!"1234567".indexOf(key)>=0) // act on key
    if (!"qwertyu".indexOf(key)>=0) // act on key
}
Prish950 Prish950

2013/2/19

#
Im not so good to use the same key for both i cant program it. I tried with this method too but i got one Problem it takes much time and often it won't appear. Can you help me ??
public void act() 
  {
      String key = Greenfoot.getKey();
      
    if ("1".equals(key))
      {
          getWorld().addObject(new RoterStein(), 1,0); 
          
      }
    
    if ("2".equals(key))
      {
          getWorld().addObject(new RoterStein(), 2,0); 
          
      }
      
    if ("3".equals(key))
      {
          getWorld().addObject(new RoterStein(), 3,0); 
          
      }
   
    if ("4".equals(key))
      {
          getWorld().addObject(new RoterStein(), 4,0); 
          
      }
      
    if ("5".equals(key))
      {
          getWorld().addObject(new RoterStein(), 5,0); 
          
      }
      
    if ("6".equals(key))
      {
          getWorld().addObject(new RoterStein(), 6,0); 
          
      }
      
    if ("7".equals(key))
      {
          getWorld().addObject(new RoterStein(), 7,0); 
          
      }
RoterStein = Red Stone
Prish950 Prish950

2013/2/19

#
String key = Greenfoot.getKey();  
if (key != null)  
{  
    if (!"1234567".indexOf(key)>=0) // act on key  
    if (!"qwertyu".indexOf(key)>=0) // act on key  
}  
with this code what should i type for the red zero?? i wrote:


String key = Greenfoot.getKey();  
if (key != null)  
{  
    if (!"1234567".indexOf("1")>=0) 
{ 
    Add Object...
} 

}
Is this right ??
Prish950 Prish950

2013/2/19

#
Need HELP
danpost danpost

2013/2/20

#
String key = Greenfoot.getKey();  
if (key != null)  
{  
    if (!"1234567".indexOf(key)>=0) 
    { 
        addObject(new RoterStein(), "1234567".indexOf(key)+1, 0);
    }
    if (!"qwertyu".indexOf(key)>=0)
    {
        addObject(new SchwartzStein(), "qwertyu".indexOf(key)+1, 0);
    }
}
Prish950 Prish950

2013/2/20

#
They say to me operator can not be applied to int
Prish950 Prish950

2013/2/20

#
public void act() 
  {
    String key = Greenfoot.getKey();
    
    if (key != null)
    { 
        
    if (!"1234567".indexOf(key)>=0) 
    
    { 
       addObject(new RoterStein(), "1234567".indexOf(key)+1, 0);
    }
    
}
operator can not be applied to int ?????? why
Prish950 Prish950

2013/2/20

#
if (!"1234567".indexOf(key)>=0) they say to me that this line is wrong. AND MY 0 is not red ???
danpost danpost

2013/2/20

#
My fault, here is corrected code:
String key = Greenfoot.getKey();  
if (key != null)  
{  
    if ("1234567".indexOf(key)>=0) 
    { 
        addObject(new RoterStein(), "1234567".indexOf(key)+1, 0);
    }
    if ("qwertyu".indexOf(key)>=0)
    {
        addObject(new SchwartzStein(), "qwertyu".indexOf(key)+1, 0);
    }
}
Prish950 Prish950

2013/2/20

#
OMG PERFECT THANK YOU SO MUCH!!!!! But now i want to add that this redstone to drop down automaticly to the bottom.
public void act() 
  {
    String key = Greenfoot.getKey();  
   if (key != null)  
   {  
     if ("1234567".indexOf(key)>=0) 
     { 
        getWorld().addObject(new RoterStein(), "1234567".indexOf(key)+1, 0);
     }
     
   }
   
   aktion();

 }
AND AKTION IS MY DROP DOWN CODE !!!!!!! I WROTE IT BEFORE IN THE OVERCLASS BUT IT DOESNT WORK WITH THE KEY CODE??? NEED HELP!! :D
public void aktion()
    {
         if (roterSteininfront() || gelberSteinInront())
         {
           
         } 
         else
         {
           move();
         }
        
    }
This works when it stays alone and after i pressed 2 times the 1 button it works too but why ?? pls help
danpost danpost

2013/2/20

#
You probably should have a timer to allow the visual of the dropping to be apparent and a flag to indicate whose turn it is. These two field should be declared near the top of the class (in the class code but outside of any methods or constructors). By placing them in such manner, they will persist for the object as long as the object is in the world.
// example
import greenfoot.*;

public class RotenStein extends Actor
{
    private boolean istGelber; // default is false
    private int dropTimer;  // to slow the dropping of the stone

    public RotenStein()
    {
        // constructor code, if any
    }

    public void act()
    {
        String key = Greenfoot.getKey();
        if (key != null)  
        {  
            if ("1234567".indexOf(key)>=0) 
            { 
                Actor stein = null;
                if (istGelber)
                {
                    stein = new GelberStein();
                }
                else
                {
                    stein = new RoterStein();
                }
                addObject(stein, "1234567".indexOf(key)+1, 0);
                istGelber = !istGelber; // switch turns
            }
        }
        if (!roterSteinInfront() && !gelberSteinInfront())
        {
            if (dropTimer>0)
            {
                dropTimer--;
            }
            if (dropTimer == 0)
            {
                move();
                dropTimer = 10;
            }
        }
    }

    // other methods (like 'move', 'roterSteinInfront' and 'gelberSteinInfront')
}
Prish950 Prish950

2013/2/20

#
THANKS SO MUCH BUT WHAT SHOULD I YPE IN THE private boolean istGelber; // default is false ?? private int dropTimer; // to slow the dropping of the stone ?? ????
danpost danpost

2013/2/20

#
Prish950 wrote...
THANKS SO MUCH BUT WHAT SHOULD I TYPE IN THE private boolean istGelber; // default is false ?? private int dropTimer; // to slow the dropping of the stone ??????
You do not have to change that part at all. Class fields of primary types are assigned either zero or false by default (object references default to null). Fields that are local to methods need to be assigned values programmatically, or the compiler will complain.
There are more replies on the next page.
1
2