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

2014/1/9

Making actors appear/activate upon actions.

Reverie Reverie

2014/1/9

#
Hello, I'm trying to create a mini-game for school where I want an image to appear in a certain place once another actor reaches a certain point in the word (using the move method). To elaborate, in my game, a container moves along a conveyor belt into a scanner. Once the container reaches the middle, I'd like a random image to come onto the screen at a given coördinate. I'd also like a light to turn on once the container enters the scanner. What are some methods I could use to achieve this? Any help would be greatly appreciated.
Reverie Reverie

2014/1/11

#
Bump.
danpost danpost

2014/1/11

#
Actually, methods would be harder to work with than Actors, here. If you had an actor (for a light object) and set its image to a transparent point and place it at the location when the edge of the container would be when the light turns on, then you could control the image of the light (between the transparent point and the actual light) depending upon the detection of the container in the light class. If you had an actor (for the panel that displays the contents of the scanned container), again detection methods can be used in the panel class to control the image (from a blank screen to a random image and back).
Reverie Reverie

2014/1/13

#
What would be an example of the code required to do that? Don't mean to come off as lazy, but I've been tinkering around with it and I can't seem to do it.
danpost danpost

2014/1/13

#
A sample light class might be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;
 
public class Light extends Actor
{
    private GreenfootImage light;
 
    public Light()
    {
        light = getImage(); // save light image
        setImage(new GreenfootImage(1, 50)); // start light in 'off' position
    }
 
    public void act();
    {
        if (getIntersectingObjects(Container.class).isEmpty() && getImage().getWidth() > 1)
        { // not intersecting container and light is on
            setImage(new GreenfootImage(2, 50)); // turn light off
        }
        if (!getIntersectingObjects(Container.class).isEmpty() && getImage().getWidth() == 1)
        { // intersecting container and light is off
            setimage(new GreenfootImage(light)); // turn light on
        }
    }
}
It would be close to the same thing with for a Scanner class except for what the image changes to when on.
You need to login to post a reply.