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

2017/1/18

Referencing a Method

1
2
BrownDwarf BrownDwarf

2017/1/19

#
does this alien class you created still allow the aliens to move back and forth across the platforms. Also, if an alien dies, does this make it spawn again on another platform. BTW I really appreciate your help on such a short basis, I have a deadline tomorrow, and even my dad wouldn't help me. So thanks a lot.
danpost danpost

2017/1/19

#
Btw, with the revised code, I deliberately left out the 'false' in the Space class constructor 'super' call. It is necessary to omit it because of the use of 'isAtEdge'. This can also be revised to allow actors to completely leave the viewing screen so the transition from one side of the world to the other looks smoother (the viewed half images are not abruptly removed from one side of the world and their opposing half images suddenly appearing on the other).
BrownDwarf wrote...
does this alien class you created still allow the aliens to move back and forth across the platforms.
Yes. I do not believe my changes altered their behavior on a platform.
Also, if an alien dies, does this make it spawn again on another platform.
No; but with a set limit of two aliens in the world, we can have a platform without an alien add one atop of itself when the number of aliens is less than the limit. In the RegularPlatform class, you can add something like the following:
if (getWorld().getObjects(Alien.class).size() < 2)
{
    setLocation(getX(), getY()-70);
    if (!isTouching(Alien.class))
    {
        getWorld().addObject(new Alien(), getX(), getY());
    }
    setLocation(getX(), getY()+70);
}
You can add into the act method. It can either go inside or outside of the 'if' (isAtEdge) code block. Inside would be the preferable place as then they would appear from the edges of the world (on a platform) instead of popping into the world at some central location onto a platform.
BrownDwarf BrownDwarf

2017/1/19

#
Sorry, where should the code go? In the alien class. I put it in the act method, and it puts the alien in the open. What do you mean by the isAtEdge block. Do you mean inside an if statement that says if at edge?
danpost danpost

2017/1/19

#
BrownDwarf wrote...
Sorry, where should the code go? In the alien class. I put it in the act method, and it puts the alien in the open. What do you mean by the isAtEdge block. Do you mean inside an if statement that says if at edge?
Read my last post again. Pay attention (especially right before the code given). Btw, if you can, please tell me what each line in the code snippet does.
BrownDwarf BrownDwarf

2017/1/20

#
Sorry. I fell asleep, then had school. I submitted my project it was working well. Thanks. And to my understanding the code checks if there are less than 2 aliens in the world, then if there are, and if the platform isn't touching an alien (doesn't already have one on it), an alien is added on the platform. It is then offsetted for the fact that it spawns in the center of the platform (+70). The initial -70 I don't understand though. Honestly. the logic I can understand, its just that I didn't fully know all the syntax yet. For example I didn't realize you could add an object from an actor class, as it kept telling me you had to be in a world class. But I didn't know you could get the world and imitate it. So thanks by the way. Helped me understand better. Really appreciate it.
danpost danpost

2017/1/20

#
BrownDwarf wrote...
It is then offsetted for the fact that it spawns in the center of the platform (+70). The initial -70 I don't understand though.
The center of the platform has nothing to do with it. By center, you are talking horizontally. These adjustments are on 'getY()'. The (-70) and (+70) are in 'setLocation' calls adjusting the vertical location of the platform. The two 'setLocation' statements offset each other so not change in location is the end result. The reason for moving the platform up is that the Alien objects were placed 70 pixels above the platform and we needed the platform to detect any Alien object on it. Moving it up guarantees that the platform will intersect any Alien object on it. After determining whether any were there, the platform is replaced at its original location.
You need to login to post a reply.
1
2