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

2013/3/10

Need My Actor to Respawn After hitting Left Wall

Nz-Tank Nz-Tank

2013/3/10

#
okay so i am making a game for my class, and im a little stuck. i have 4 enemies. they approach from the right, and your player starts on the left. The object is to throw bombs across and hit the enemies. 15 destroyed enemies is game over. I don't know how to get them to respawn if they have been hit, and also if they hit the left wall.
danpost danpost

2013/3/10

#
Instead of using the state of the enemy being either destroyed or at the left wall, just produce them randomly at a random location along the right wall. You can easily adjust the random chance of spawning to create a situation where the average number of enemies in the world are four at one time. If you wish exactly four at all times, then instead of removing them from the world when they are destroyed or hit the left wall, just relocate them at some random location along the right wall again.
Nz-Tank Nz-Tank

2013/3/10

#
I just started using Greenfoot this 2nd semester in school, and we are only on chapter 4, so we haven't even learned a lot of the stuff i'm trying to do. Hence, i don't really know what it would look like if i were to write something that would relocate them. Even getting them to randomly spawn (in different y axis along the right wall) is a bit tricky.
Nz-Tank Nz-Tank

2013/3/10

#
*this is what it looks like so far... public class Slenderman extends Mover { /** Size of Slenderman*/ private int size; /** When the stability reaches 0, Slenderman will explode */ private int stability; public void act() { move(-1); } /** * Create an Slenderman */ public Slenderman() { this(64); } /** * Create Slenderman with a given size and default speed. */ public Slenderman (int size) { this(size, new Vector(Greenfoot.getRandomNumber(360), 2)); } /** * Create Slenderman with a given size size and speed. */ private Slenderman(int size, Vector speed) { super(speed); setSize(size); } /** * Set the size of Slenderman. **/ public void setSize(int size) { stability = size; this.size = size; GreenfootImage image = getImage(); image.scale(60, 180); } /** * Return the current stability of Slenderman (If it goes down to * zero, he dies). */ public int getStability() { return stability; } /** * Hit Slenderman dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0) explode(); } /** * Explodes Slenderman */ private void explode() { Greenfoot.playSound("Explosion.wav"); if(size <= 16) { getWorld().removeObject(this); return; } else { int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45); double l = getMovement().getLength(); Vector speed1 = new Vector(r + 60, l * 1.2); Vector speed2 = new Vector(r - 60, l * 1.2); getWorld().removeObject(this); } } }
Nz-Tank Nz-Tank

2013/3/10

#
Well, i just changed it so that they move around randomly (as opposed to right to left). All i need to do is Make them respawn after they've been killed. Actually, i also want to make it so that when my player throws a bomb, it doesn't continue through one edge of the world and move down from the opposite wall (like the asteroids game where the bullet travels past the screen). I just want it to disappear at the wall.
danpost danpost

2013/3/10

#
I believe the 'Mover' class has an 'atWorldEdge' method you can use to check the state of the object being at one of the world edges. If at the edge, use the World class method 'removeObject' to remove the object. You will need to call this method on the world that the Actor object is in. The Actor class has a 'getWorld' method to return that world.
if (atWorldEdge()) getWorld().removeObject(this);
Make this the last statement or call the method with this code last from the act method. If you do not do this, there is a possibility that an exception error will occur. There are ways around this, if necessary, by using one or both of the following statements: 'return;' and 'if (getWorld() == null) return;'. You can have the world check the number of enemies in the world, and if less than four, add one. In the world class act method add the following line:
if (getObjects(Enemy.class).size()<4) addObject(new Enemy(), getWidth()-1, Greenfoot.getRandomNumber(getHeight()));
As I have not been informed as to the name of the class you are using for the enemy, I used 'Enemy' for it. You will have to adjust it to the name you gave it.
Nz-Tank Nz-Tank

2013/3/10

#
Ok, this worked really well. Thanks! Although, i have one last problem if it isn't a bother to you. I have 3 times where i want sound to play. one of the three (when the enemy dies) works. I also need ambient sounds to play, but when i did it the sound seemed to work at first, but if it played for too long the game got really slow and the sound started to cut out. Another time i need sound is when the game ends. I haven't even started that as i don't know where to begin.
danpost danpost

2013/3/10

#
When this occurs, stop the scenario, right-click on the world, and select the 'numberOfObjects' method from the drop-down menu of methods 'Inherited from World'. Post back as the what number is returned.
Nz-Tank Nz-Tank

2013/3/10

#
the number is 7... 6 enemies and the player.
danpost danpost

2013/3/10

#
If the sound file for the ambient sound is a very large sound file, that may have something to do with it; otherwise, I could not say.
You need to login to post a reply.