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

2013/7/26

Respawning???????

1
2
Kaiwalya Kaiwalya

2013/7/26

#
I need help to respawn the aliens in my scenario http://www.greenfoot.org/scenarios/9060 in a proper way can anyone help please .
Kartoffelbrot Kartoffelbrot

2013/7/26

#
You could write aa method into your world class, which spawns them randomly at the right side of the screen. Like this:
public void spawnAliens(){
//this is the chance a new alien is spawned per act
if(Greenfoot.getRandomNumber(200)==0)
addObject(new Alien(), getWidth(), Greenfoot.getRandomNumber(getHeight()+1));
}
And if you don't want to have it like this and you want to appear a new alien if one alien dies, then write into your method, where your alien dies by hitting a bullet, only this: addObject(new Alien(), getWidth(), Greenfoot.getRandomNumber(getHeight()+1)); And afterwards the dying alien can remove itself.
Kaiwalya Kaiwalya

2013/7/26

#
Will it display in a tabular form like in a space invader game but in right side?
Kaiwalya Kaiwalya

2013/7/26

#
It should be like a new wave. Like in the first wave three for eg:- (* means aliens) * (! means plane) ! * * second wave six:- * * ! * * * *
Kartoffelbrot Kartoffelbrot

2013/7/26

#
Then you need: - an integer variable in the world called (perhaps) wave - a method that spawns the waves
int wave = 0;

public void act(){
if(numOfAliens()==0){
wave++;
nextWave(wave);
}

public int numOfAliens(){
return getObjects(Alien.class).size();
}

public void nextWave(int wave){
if(wave==1){
//insert here the spawning of the aliens like:
addObject(new Alien(), getWidth(), getHeight()/4);
addObject(new Alien(), getWidth(), getHeight()/2);
addObject(new Alien(), getWidth(), 3*getHeight()/4);
//this is like you "painted" the first wave
}else if(wave==2){
//same here. change the number of aliens and their place
}else if(wave==3){
//and here
}
//etc.
}
}
All the code should be in your world.
Kaiwalya Kaiwalya

2013/7/26

#
Well thats not working.
Kartoffelbrot Kartoffelbrot

2013/7/26

#
Why?
Kaiwalya Kaiwalya

2013/7/26

#
i dont know, I will post the world class codes
Kaiwalya Kaiwalya

2013/7/26

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class airport here. * * @author (your name) * @version (a version number or a date) */ public class airport extends World { int wave = 0; public void act() { scrollPosition -= scrollSpeed; while(scrollSpeed > 0 && scrollPosition < -picWidth) scrollPosition += picWidth; while(scrollSpeed < 0 && scrollPosition > 0) scrollPosition -= picWidth; paint(scrollPosition); if(numOfaliens()==0){ wave++; nextWave(wave); } } public int numOfaliens(){{ return getObjects(alien.class).size(); } } public void nextWave(int wave){ if(wave==1){ //insert here the spawning of the aliens like: addObject(new alien(), getWidth(), getHeight()/4); addObject(new alien(), getWidth(), getHeight()/2); addObject(new alien(), getWidth(), 3*getHeight()/4); //this is like you "painted" the first wave }else if(wave==2){ //same here. change the number of aliens and their place }else if(wave==3){ //and here } //etc. } private static final String bgImageName = "sky.png"; private static final double scrollSpeed = 2.5; private static final int picWidth = (new GreenfootImage(bgImageName)).getWidth(); private GreenfootImage bgImage, bgBase; private int scrollPosition = 0; /** * Constructor for objects of class airport. * */ public airport() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(812, 452, 1); setBackground(bgImageName); bgImage = new GreenfootImage(getBackground()); bgBase = new GreenfootImage(picWidth, getHeight()); bgBase.drawImage(bgImage, 0, 0); addObject (new alien (), 550, 50); addObject (new alien (), 550, 100); addObject (new alien (), 550, 150); addObject (new alien (), 550, 250); addObject (new alien (), 550, 300); addObject (new alien (), 550, 350); prepare(); } private void prepare() { Plane plane= new Plane(); addObject(plane, 156, 245); plane.setLocation(100, 300); } private void paint(int position) { if (!getObjects(Display.class).isEmpty()) {removeObjects(getObjects(Display.class)); } GreenfootImage bg = getBackground(); bg.drawImage(bgBase, position, 0); bg.drawImage(bgImage, position + picWidth, 0); } }
Kartoffelbrot Kartoffelbrot

2013/7/26

#
1.) import java.util.*; 2.) change the numOfAliens() method to this:
public int numOfAliens(){
List<Alien.class>aliens = getObjects(Alien.class);
return aliens.size();
}
3.) You have to insert your own things into the nextWave method. You have to design your own waves like in the constructor. 4. If it doesn't work again, inspect the world an look what value is wave and tell me.
Kaiwalya Kaiwalya

2013/7/26

#
Kartoffelbrot Kartoffelbrot

2013/7/26

#
sry: List<Alien>aliens = getObjects(Alien.class);
Kaiwalya Kaiwalya

2013/7/27

#
Thank you very much , it worked. Can u help me in two more things ??? 1. Display the text "Wave - 1 " and so on..... 2. My aliens are moving but I want them to move just half of the screen and return back.
Kartoffelbrot Kartoffelbrot

2013/7/27

#
1.) Make a new class. Name it text or however you want. Then the world has to tell the text, what value wave is. There are more ways to do this. I pick one of them:
import java.awt.Color;
import greenfoot.*;

public class Text{

airport myWorld;

public Text(airport world){//if you create a new text you have to give him an object of class airport, which is your world in this case. That way he is able to use the worlds methods and values

myWorld = world;
paintMe();
}

public void act(){
    paintMe();
}

public void paintMe(){
GreenfootImage myImage = new GreenfootImage("Wave - "+myWorld.wave,//the text
50, // the size
Color.BLACK, //its color
new Color(0,0,0,0));//its background color. here it its transparent
setImage(myImage);
}
The world has to add the text now into it. It's the best, if you do that in the prepare() method, like with all other objects. Text counter = new Text(this); //here you give the airport to the text addObject(counter, getWidth()-counter.getWidth()/2, getHeight()-counter.getHeight()/2); Try this, then I will watch on your other problem.
danpost danpost

2013/7/27

#
It is really wasteful (as far as resources are concerned) to update the image of an object every act cycle for something that will change 'once in a blue moon' (as far as how often the wave value will change compared to passing act cycles). It would be better to keep a reference to the 'Text' object that displays the wave number in the world class and reset its text when the wave number changes.
// in the world class
// add instance fields
Text waveText = new Text("Wave - 1");
int wave = 1;// you should already have this one
// in constructor (or 'prepare' method)
addObject(waveText, 100, 20); // location of your choosing
// when a new wave is to be introduced (in the world class)
wave++;
waveText.setText("Wave - "+wave);

// the Text class
import greenfoot.*;
import java.awt.Color;

public class Text extends Actor
{
    public Text(String text)
    {
        setText(text);
    }

    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));
    }
}
There are more replies on the next page.
1
2