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

2014/8/27

using a return value

davemib123 davemib123

2014/8/27

#
Hi, I have 2 classes which this question relates to: frames and hero. I want to stop the hero from moving when the frame is shown on the screen. so far I have this for the frames class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Message here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Frames extends Tiles
{
    public boolean frameActive;
    public Frames(String selection)
    {
        this.selection = selection;
        this.directory = "Frames/";
        this.frameActive = true;
 
        if (selection == "type1")
            setImage(directory + "type1.png");
        else if (selection == "type2")
            setImage(directory + "type2.png");
    }
 
    public void act()
    {
        if (getWorld().getObjects(Text.class).size() == 0 && Greenfoot.isKeyDown("enter"))
        {
            this.frameActive = false;
            getWorld().removeObject(this);
        }
    }
 
    public boolean getFrameActive() 
    
        return frameActive; 
    
}
In the act method of the Hero I have this but not sure where I have gone wrong retuning if frameActive value. The code is as follows:
1
2
3
4
5
6
7
8
public void act()
   {
       if (((Frames) getWorld().getObjects(Frames.class).get(0)).getFrameActive() == false)
       {
           checkKeys();
       }
       mapSpecificBoundaries();
   }
Any suggestions?
danpost danpost

2014/8/27

#
It appears that you are asking if the Frame object in the world is not active. But, when you inactivate the Frame object, you remove it from the world. So, it looks like there would never be an inactive Frame object in the world to begin with.
davemib123 davemib123

2014/8/27

#
so i should check if it is true?
danpost danpost

2014/8/27

#
davemib123 wrote...
so i should check if it is true?
The condition will always be false (that 'getFrameActive' will always return true -- or you will get an error because none are in the world -- and therefore is never equal to false). Maybe you should just be asking 'are there are no Frames object in the world':
1
if (getWorld().getObjects(Frames.class).isEmpty())
From what I can tell, your Frames class can be reduced to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;
 
public class Frames extends Tiles
{
    public Frames(String selection)
    {
        this.selection = selection;
        this.directory = "Frames/";
        setImage(selection+".png");
    }
 
    public void act()
    {
        if (getWorld().getObjects(Text.class).isEmpty() && Greenfoot.isKeyDown("enter"))
        {
            getWorld().removeObject(this);
        }
    }
}
davemib123 davemib123

2014/8/27

#
Yea that is what I should have done
danpost wrote...
Maybe you should just be asking 'are there are no Frames object in the world':
1
if (getWorld().getObjects(Frames.class).isEmpty()
That's a cool way to reduce the class
danpost wrote...
From what I can tell, your Frames class can be reduced to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;
 
public class Frames extends Tiles
{
    public Frames(String selection)
    {
        this.selection = selection;
        this.directory = "Frames/";
        setImage(selection+".png");
    }
 
    public void act()
    {
        if (getWorld().getObjects(Text.class).isEmpty() && Greenfoot.isKeyDown("enter"))
        {
            getWorld().removeObject(this);
        }
    }
}
Thanks as always danpost!
You need to login to post a reply.