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

2021/12/21

need help with getting enemy actor to respawn at original spawn point after touching bottom

roonie01 roonie01

2021/12/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    int x;
    int y;
    GreenfootImage background;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Space()
    {    
        // Create a new world with 1600x1200 cells with a cell size of 1x1 pixels.
        super(1600, 1200, 1); 

        x = getWidth()/2; 
        y = getHeight()/2;

        addObject(new Rocket(),getWidth()/2,getHeight()/2); 

        addEnemy();
        addBack();

    }

    public  void addEnemy()
    {
        int x1 = 1600 / 8;
        for(int i =0;i<=1600;i=i+x1)
    {

        addObject( new enemy1(),200+x1,30);
        }
    }

    public  void addBack()
    {
        if( enemy1.class == null )
        {
            addEnemy();  
        }
    }
}

Super_Hippo Super_Hippo

2021/12/21

#
Show code for the enemy1 class.
roonie01 roonie01

2021/12/21

#
here is source code for enemy1 class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemy1 extends Actor
{
    /**
     * Act - do whatever the enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int x = 1600 / 8;
    public void act()
    {
        // Add your action code here.
        //add movement 
        Movement();
        
        //add collision detection
        Collisions();
        
        //add respawn
        //addBack();
    
    }

    public void Movement()
    {
        setLocation(getX(),getY()+10);
    }

    public void Collisions()
    {
        if (this.isTouching(Rocket.class))
        {
            //remove enemy
            getWorld().removeObject(this);
            setLocation(getX(),getY());

        }
        else if(this.isAtEdge( ))
        {
            //reset enemy to starting position 
            getWorld().removeObject(this);
            //setLocation(getX(),getY());
            
        }
    }
    
    }
    



roonie01 roonie01

2021/12/21

#
what im trying to do is have 8 enemys spawn across the top of the window and have them progressively move down
Super_Hippo Super_Hippo

2021/12/21

#
Instead of removing it from the world, you can just move it upwards to the top again.
roonie01 roonie01

2021/12/21

#
ill try that but wont it just get stuck with the enemy at the bottom of the screen and i also need help making it so there are multiple enemys going horizontally across the screen do you know how i can do this
roonie01 roonie01

2021/12/22

#
so by moving it upwards you mean make its y value equal the y value defined in world
if (this.isTouching(Rocket.class))
        {
            //remove enemy
            getWorld().removeObject(this);
            setLocation(getX(),30);

        }
        else if(this.isAtEdge( ))
        {
            //reset enemy to starting position 
            getWorld().removeObject(this);
            setLocation(getX(),30);
            
        }
danpost danpost

2021/12/22

#
Use the following in the class:
private int x0, y0; // original coordinates
private int vX, vY; // horizontal and vertical speeds

protected void addedToWorld(World world)
{
    x0 = getX();
    y0 = getY();
    int speed = 3; // adjust as required
    if (y0 <= 30)  vY = speed; else vX = speed;
}

public void act()
{
    setLocation(getX()+vX, getY()+vY);
    if (isAtEdge() || isTouching(Rocket.class)) setLocation(x0, y0);
}
roonie01 roonie01

2021/12/22

#
i did your suggestion but now when i hit spacebar top fire the laser it makes my enemy class get stuck at top of screen how can i remove the laser after it hits the enemy class
danpost danpost

2021/12/22

#
roonie01 wrote...
i did your suggestion but now when i hit spacebar top fire the laser it makes my enemy class get stuck at top of screen how can i remove the laser after it hits the enemy class
That sounds like Rocket class codes, which was not given.
You need to login to post a reply.