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

2017/3/7

How to add objects only if a button is pressed

ObliviaOng ObliviaOng

2017/3/7

#
Hi! As the title says, uh how do you code for adding objects when pressing a button, specifically only the space button? I already have a code that after a certain amount of time an you press space, the next line of text appears (makin' a choose ur own adventure) tho my problem is that after too much time, all the lines of text appear at the same time?? so I'd like to make that the next lines of text would only appear if you press space. Here's the code I used: long lastAdded = System.currentTimeMillis(); public void ShowGameTxt() { long curTime = System.currentTimeMillis(); if(Greenfoot.isKeyDown("space")) { Gametxt gmtxt = new Gametxt(); addObject(gmtxt,267,20); if (curTime >= lastAdded + 1500 && (Greenfoot.isKeyDown("space")))//2000ms = 2s { Noiatxt ntxt = new Noiatxt(); addObject(ntxt,482,61); if (curTime >= lastAdded + 2300 && (Greenfoot.isKeyDown("space"))) { GT2 gt1 = new GT2(); addObject(gmtxt,267,93); if (curTime >= lastAdded + 3000 && (Greenfoot.isKeyDown("space"))) { Gtalk gtk = new Gtalk(); addObject(gtk,278, 127); } } } } } So uh if you could help me, that would awesome (grades are on the line,)
danpost danpost

2017/3/7

#
First, it would be easier to help if you had line numbers for you code -- so here it is again:
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
long lastAdded = System.currentTimeMillis();
public void ShowGameTxt()
{
    long curTime  = System.currentTimeMillis();
    if(Greenfoot.isKeyDown("space"))
    {
        Gametxt gmtxt = new Gametxt();
        addObject(gmtxt,267,20);
        if (curTime >= lastAdded + 1500 && (Greenfoot.isKeyDown("space")))//2000ms = 2s
        {
            Noiatxt ntxt = new Noiatxt();
            addObject(ntxt,482,61);  
            if (curTime >= lastAdded + 2300 && (Greenfoot.isKeyDown("space")))
            {
                GT2 gt1 = new GT2();
                addObject(gmtxt,267,93);
                if (curTime >= lastAdded + 3000 && (Greenfoot.isKeyDown("space")))
                {
                    Gtalk gtk = new Gtalk();
                    addObject(gtk,278, 127);
                }
            }
        }
    }
}
danpost danpost

2017/3/7

#
Now, since you are using the 'space' key and not time, remove lines 1 and 4. Next, because you are acting only when the 'space' key changes states (either going up or going down), you will need to track the state of the key with a boolean field (to replace line 1 which was removed):
1
private boolean spaceDown;
Now, to detect changes in the state, you can use:
1
2
3
4
5
6
7
8
9
10
11
// detect space key being pressed
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    // act on key press
    spaceDown = true;
}
// detect space key being released
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
    spaceDown = false;
}
Now, because you are adding different objects into the world each time the key is pressed, you will need another field to be used as a counter:
1
private int spaceCount;
Then for the action when the key is pressed, you can have this:
1
2
3
4
5
spaceCount++;
if (spaceCount == 1) addObject(new Gametxt, 267, 20);
else if (spaceCount == 2) addObject(new Noiatxt, 482, 61);
else if (spaceCount == 3) addObject(new GT2(), 267, 93);
else if (spaceCount == 4) addObject(new Gtalk(), 278, 127);
ObliviaOng ObliviaOng

2017/3/8

#
Thanks you so much for your help but I have to ask, how do you exactly code for the private int? I did code it but I'm not even sure if it's correct so I apologize if it is (i'm kinda new to coding). Also if I did code it correctly, it isn't exactly working since the first line of text is already displayed when a ran the program and when I press space the next lines don't appear.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private boolean spaceDown;
    public void ShowGameTxt()
 
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown= true;
        }
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
    }
 
    private int spaceCount;
    {
        spaceCount++;
        if (spaceCount == 1) addObject(new Gametxt(),267, 20);
        else if (spaceCount == 2) addObject(new Noiatxt(),482, 61);
        else if (spaceCount == 3) addObject(new GT2(),267, 93);
        else if (spaceCount == 4) addObject(new Gtalk(), 278, 127);
    }
Again, thank you so much for your time and help
Super_Hippo Super_Hippo

2017/3/8

#
Move line 15 to after/before line 1 (outside methods) and remove lines 13 and 16. (Make sure you call the 'ShowGameTxt' method from the act method.)
danpost danpost

2017/3/8

#
Actually, lines 17 through 21 need to go with line 7.
ObliviaOng ObliviaOng

2017/3/8

#
Oh thanks, so my code should look like this??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void ShowGameTxt()
 
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown= true;
            if (spaceCount == 1) addObject(new Gametxt(),267, 20);
            else if (spaceCount == 2) addObject(new Noiatxt(),482, 61);
            else if (spaceCount == 3) addObject(new GT2(),267, 93);
            else if (spaceCount == 4) addObject(new Gtalk(), 278, 127);
       
        }
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
 
    }
tho when I did run the program, the text wouldn't load at all when I pressed space.
danpost danpost

2017/3/8

#
ObliviaOng wrote...
Oh thanks, so my code should look like this?? < Code Omitted >
Yes.
tho when I did run the program, the text wouldn't load at all when I pressed space.
Please show the entire class code to hopefully see why.
ObliviaOng ObliviaOng

2017/3/8

#
oh ok, here's my code
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class GameWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameWorld extends World
{
 
    /**
     * Constructor for objects of class GameWorld.
     *
     */
    public GameWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage("download.jpg");
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
 
        prepare();
    }
 
    public void act()
    {
        Exit();
        ShowGameTxt();
    }
 
    private void Exit()
    {
        if (Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.setWorld(new StartScreen());
        }
 
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Instructions instructions = new Instructions();
        addObject(instructions,102,382);
        instructions.setLocation(287,381);
    }
 
     private boolean spaceDown;
    private int spaceCount;
    public void ShowGameTxt()
 
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown= true;
            if (spaceCount == 1) addObject(new Gametxt(),267, 20);
            else if (spaceCount == 2) addObject(new Noiatxt(),482, 61);
            else if (spaceCount == 3) addObject(new GT2(),267, 93);
            else if (spaceCount == 4) addObject(new Gtalk(), 278, 127);
       
        }
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
 
    }
}
danpost danpost

2017/3/8

#
Where is line 17 (the first of the lines you were to move)?
ObliviaOng ObliviaOng

2017/3/8

#
ahhh no wonder, whoops i forgot to type that down, i just ran the program and it works, thank you so much for your help! :D
You need to login to post a reply.