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

2012/4/2

code help please!!!

1
2
thegamer thegamer

2012/4/2

#
hello i have got some code which include get key and string key but they both do not shoot heres the code

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class penival here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class penival extends Actor
{    
    /**
     * Act - do whatever the penival wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {       
      
        if (Greenfoot.isKeyDown("left")) 
      {
       move(-3);
      } 
      if (Greenfoot.isKeyDown("right"))
      { 
       move(3);
      }
      String key = Greenfoot.getKey();  
      if ("space".equals(key))  
      {  
       fire();  
      }  
    }        
    /**
     * Fire the penival
     */
    private void fire()
    {
    
       Bullet bullet = new Bullet();
       getWorld().addObject(bullet, getX(), getY());
       bullet.setRotation(getRotation());
    }
}
That is for the penival this is for the dimentions
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Dimentions here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dimentions extends Actor
{
    /**
     * Act - do whatever the Dimentions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    
         if (Greenfoot.isKeyDown("a"))
    {
       move(-3);
    } 
    if (Greenfoot.isKeyDown("d"))
    {
        move(3);
    }   
    if ("z".equals (Greenfoot.getKey()))
     {
       fire();
     }

    }
    
    /**
     * Fire the dimentions
     */
    private void fire()
    {
    
       Bullet2 bullet2 = new Bullet2();
       getWorld().addObject(bullet2, getX(), getY());
       bullet2.setRotation(getRotation());
    }
    }   
If anyone can help that would be really helpful Thanks
thegamer thegamer

2012/4/2

#
Hello!!!!!!!!!!
Morran Morran

2012/4/2

#
You probably want to rewrite your key checking thing to this:
      String key = Greenfoot.getKey();    
      if (key.equals("space"))    
      {    
       fire();    
      }   
Also, Greenfoot.getKey() can only be called reliably once per frame. If you call it twice, it will only work with the first one that used it. Greenfoot.isKeyDown(String key) can be used as many times as you like per frame. That might work better.
danpost danpost

2012/4/2

#
Morran wrote...
You probably want to rewrite your key checking thing to this:
String key = Greenfoot.getKey();    
if (key.equals("space"))    
{    
    fire();    
}
In line 2 above, if no key was pressed (and released), 'key' will be 'null' and you will get a NullPointerException. If you use this code, change line 2 to
if("space".equals(key))
as "space" is never null.
thegamer thegamer

2012/4/3

#
It still does not work Grrrrrr!!!!!!!!
danpost danpost

2012/4/3

#
What does your code look like now, and what error message are you getting? and, what is it supposed to do and what is it actually doing (that is wrong)?
thegamer thegamer

2012/4/4

#
this is my new code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Dimentions here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dimentions extends Actor
{
    /**
     * Act - do whatever the Dimentions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    
         if (Greenfoot.isKeyDown("a"))
    {
       move(-3);
    } 
    if (Greenfoot.isKeyDown("d"))
    {
        move(3);
    }   
    String key = Greenfoot.getKey();      
    if("z".equals(key))    
    {      
       fire();      
    }  
 
    }
    
    /**
     * Fire the dimentions
     */
    private void fire()
    {
    
       Bullet2 bullet2 = new Bullet2();
       getWorld().addObject(bullet2, getX(), getY());
       bullet2.setRotation(getRotation());
    }
    }   
what i actually want is for both of them to shoot but only one of them shoots a bullet
danpost danpost

2012/4/4

#
You will need an instance boolean (true/false) variable to distinguish between the bulletShooter and the non-bulletShooter, and then "if bulletShooter, fireBullet else fireOtherProjectile" should do the trick.
davmac davmac

2012/4/4

#
this is my new code
You gave only the code for one class. I think the problem is that you are still using 'getKey' in both classes. That can't work - as soon as one of them reads the key (even if it is not the key that it is looking for), the other one will not see the key. You best bet is to use isKeyDown(...) instead of getKey().
thegamer thegamer

2012/4/4

#
here is the code for my other charter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class penival here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class penival extends Actor
{    
    /**
     * Act - do whatever the penival wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {       
      
      if (Greenfoot.isKeyDown("left")) 
      {
       move(-3);
      } 
      if (Greenfoot.isKeyDown("right"))
      { 
       move(3);
      }     
      if ("space".equals (Greenfoot.getKey()))
      {
       fire();
       } 

    }        
    /**
     * Fire the penival
     */
    private void fire()
    {
    
       Bullet bullet = new Bullet();
       getWorld().addObject(bullet, getX(), getY());
       bullet.setRotation(getRotation());
    }
}
thegamer thegamer

2012/4/4

#
Danpost could you add that in please because i don't user stand it.
davmac davmac

2012/4/4

#
Did you actually read my post?
I think the problem is that you are still using 'getKey' in both classes. That can't work - as soon as one of them reads the key (even if it is not the key that it is looking for), the other one will not see the key. You best bet is to use isKeyDown(...) instead of getKey().
thegamer thegamer

2012/4/4

#
then it sends o line of bullets and i want it to only send one bullet at a time.
davmac davmac

2012/4/4

#
You can solve that problem in a different way. For instance, use a variable to keep track of whether the key was up or down last time you checked, and only fire if it was up last time and down this time. Alternatively, you can move the getKey() into a single location (such as the world) and then have it control the other objects depending on which key was pressed:
    String key = Greenfoot.getKey();
    if ("space".equals(key)) {
       // ...
    }
    if ("z".equals(key)) {
        // ...
    }
thegamer thegamer

2012/4/4

#
it still does not work it only allows one charter to shoot
There are more replies on the next page.
1
2