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

2018/6/7

space invader barriers

1
2
3
ogy2014 ogy2014

2018/6/8

#
i dont know how to do that
ogy2014 ogy2014

2018/6/8

#
i did this but the image doesn't change and the bullet still phases through
        Actor playermissile = getOneIntersectingObject(PlayerMissile.class);        
        if(playermissile != null)
        {
            if (getImage().equals(base1)) 
            {
                this.setImage(base2);  
                getWorld().removeObject(playermissile);
            }
            else 
            if (getImage().equals(base2)) 
            {
                this.setImage(base3);  
                getWorld().removeObject(playermissile);
            }
            else 
            if (getImage().equals(base3)) 
            {
                this.setImage(base4);
                getWorld().removeObject(playermissile);
            }
            else 
            if (getImage().equals(base4)) 
            {
                getWorld().removeObject(this);  
                getWorld().removeObject(playermissile);  
            }
        }
danpost danpost

2018/6/8

#
ogy2014 wrote...
i did this but the image doesn't change and the bullet still phases through << Code Omitted >>
This is better:
if (isTouching(PlayerMissile.class)
{
    removeTouching(PlayerMissile.class);
    if (getImage().equals(base1)) setImage(base2);
    else if (getImage().equals(base2)) setImage(base3);
    else if (getImage().equals(base3)) setImage(base4);
    else if (getImage().equals(base4)) getWorld().removeObject(this);  
}
But you still need to do this:
Try setting the image of the barrier to base1 at the end of the addedToWorld method.
ogy2014 ogy2014

2018/6/8

#
what do you mean by the addedToWorld method
danpost danpost

2018/6/8

#
ogy2014 wrote...
what do you mean by the addedToWorld method
Line 71 here -- is that not an addedToWorld method I see there in the Barrier class?
ogy2014 ogy2014

2018/6/8

#
    public void addedToWorld(World world) this.setImage(base1)
    {
        base1 = new GreenfootImage("base1.png");
        base2 = new GreenfootImage("base2.png");
        base3 = new GreenfootImage("base3.png");
        base4 = new GreenfootImage("base4.png");
    }
is this what you mean?
ogy2014 ogy2014

2018/6/8

#
oh im dumb ive got it sorted now
ogy2014 ogy2014

2018/6/8

#
when a barrier gets detroyed it gives me a error message about it using the actors location while it isnt in the world
danpost danpost

2018/6/8

#
ogy2014 wrote...
when a barrier gets detroyed it gives me a error message about it using the actors location while it isnt in the world
You should know by now to show (copy/paste) the complete error output and related coding.
ogy2014 ogy2014

2018/6/8

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.isTouching(Actor.java:972) at Barrier2.act(Barrier2.java:31) its this line at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
        if(isTouching(PlayerMissile.class))
        {
            removeTouching(PlayerMissile.class);
        }
danpost danpost

2018/6/8

#
ogy2014 wrote...
<< Error Omitted >> << Code Omitted >>
Show entire method, please.
ogy2014 ogy2014

2018/6/8

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

/**
 * Write a description of class Barrier1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Barrier1 extends Actor
{
    /**
     * Act - do whatever the Barrier1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */    
    private GreenfootImage base1;
    private GreenfootImage base2;
    private GreenfootImage base3;
    private GreenfootImage base4;
    
    public void act() 
    {
        if (isTouching(InvaderMissile.class))
        {
            removeTouching(InvaderMissile.class);
            if (getImage().equals(base1)) setImage(base2);
            else if (getImage().equals(base2)) setImage(base3);
            else if (getImage().equals(base3)) setImage(base4);
            else if (getImage().equals(base4)) getWorld().removeObject(this);  
        }       
    
        if(isTouching(PlayerMissile.class))
        {
            removeTouching(PlayerMissile.class);
        }
    }    

    public void addedToWorld(World world)
    {
        base1 = new GreenfootImage("base1.png");
        base2 = new GreenfootImage("base2.png");
        base3 = new GreenfootImage("base3.png");
        base4 = new GreenfootImage("base4.png");
        this.setImage(base1);
    }
}
ogy2014 ogy2014

2018/6/8

#
should i just modify and put that code in to my PlayerMissile
danpost danpost

2018/6/8

#
What happened to the rest of the PlayerMissile collision code? Change line 31 to:
if (getWorld() != null && isTouching(PlayerMissile.class))
and put back what you removed below it (should look similar to the first if block).
ogy2014 ogy2014

2018/6/8

#
since my player who shoots the playermissile is so close to the barrier it just rapid fires (i have code so that the player cant shoot when there is a playermissile on the screen)
There are more replies on the next page.
1
2
3