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

2014/12/2

Adding an object when key is pressed

Kel123 Kel123

2014/12/2

#
Can someone please tell me what I am doing wrong as this is not working, this is my code Rocket2 Rocket=new Rocket2(); public void act(){ if(Greenfoot.isKeyDown("enter")) { getWorld().addObject(Rocket,200,300); } }
danpost danpost

2014/12/2

#
How is it not working? at all !? any error messages? May need to see more code than what is given. The class code that this snippet is in as well as the code you have that adds the object into the world that the act method is acting on.
Kel123 Kel123

2014/12/2

#
The rocket is not appearing when enter is pressed
danpost danpost

2014/12/2

#
Please post the entire class code -- and use the 'code' link below the reply box to insert your codes into your posts.
Kel123 Kel123

2014/12/2

#
that is the only code , I tried putting it in the world it worked but now it is only shooting for a few seconds ans stops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void act()
    {
       move(0);
            
        
       if(Greenfoot.isKeyDown("left"))
        {
            setRotation(getRotation()-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(getRotation()+5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
             
        }
        if("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
     
 
 
       
    }
    /**
     * Fire the laser from rocket
     */
  private void fire()
  {
      Splash robot= new Splash();
      getWorld().addObject(robot,getX(),getY());
      //the bullet angle
      robot.setRotation(getRotation());
      robot.move(210);
 
    }
danpost danpost

2014/12/2

#
That is not an entire class code -- I need everything from imports to the end. Also, I do not see any place in the code just given where the snippet above is located. Are you showing me something totally different? are you changing things in-between postings? I really cannot make heads or tails of what you have and cannot help unless we are on the same page and fully informed. Again, please show the entire class code from which the initial snippet given appears in.
Kel123 Kel123

2014/12/2

#
The problem I had first i fixed it by putting that code in the world but now it effected it from the world it is not shooting that is the whole code This is the code of the world were I put the first cod I posted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
{
 
    /**
     * Constructor for objects of class Space.
     *
     */
    public Space()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
         
       prepare();
    }
     
    private void prepare(){
        MainRocket rocket= new MainRocket();
        addObject (rocket,150,267);
        Title title= new Title();
        addObject (title, 400,150);
        Button button= new Button();
        addObject(button,700, 560);
        Story story= new Story();
        addObject (story,700, 440);
         
        Play play=new Play();
        addObject(play,700,500);
        }
         
      Rocket Rocket=new Rocket();
       public void act(){
       if("enter".equals(Greenfoot.getKey()))
        {
             
            addObject(Rocket,500,300);
        }
    }   
 
    }
Kel123 Kel123

2014/12/2

#
If you can't figure it out don't worry and thanks anyway for all the help
danpost danpost

2014/12/2

#
Ok. Your problem is that 'getKey' will only return a keystroke once. If 'space' is pressed and is returned by 'getKey' in your world class code when checking for 'enter', then 'getKey' will not return 'space' when checking for 'space'. You can only feasibly use 'getKey' once during any act cycle (a cycle being the acting of the world and the acting of all objects in that world). Either you need to save the returned value in a field in the world class and check the value of that field when checking for 'space' in the other class, or you need to use 'isKeyDown' for one (or both) of them. Unfortunately, when using 'isKeyDown', oftentimes the action taken will be done multiple times during a keypress (how ever many act cycles run in the time it takes for the key to be pressed and released -- usually three or more cycles). This must be dealt with by either placing more conditions on the action or by adding a field to track the current state of the key and using it as the 'more' condition. Fortunately, there is a 'more' condition you can add when adding the rocket into the world:
1
if (Rocket.getWorld() == null && Greenfoot.isKeyDown("enter"))
This uses the 'isKeyDown' method, but also asks if the rocket is in a world. That way, once the rocket is added, it will not try to add it again.
You need to login to post a reply.