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

2019/6/3

Command Adding and Deleting

TravJ14 TravJ14

2019/6/3

#
I'm trying to add an object on command to act as a shield, but after lots of trial and error, I couldn't get it. Here's my code:
Actor Wall2;
        Wall2 = getOneObjectAtOffset(0,0,Wall2.class);
        if(Greenfoot.isKeyDown("w"))
        {
            World area;
            area = getWorld();
            area.addObject(Wall2,400,200);
        }
        if(Greenfoot.isKeyDown("s"))
        {
            World area;
            area = getWorld();
            getWorld().removeObjects(getWorld().getObjects(Wall2.class));
        }
Super_Hippo Super_Hippo

2019/6/3

#
Maybe this helps:
//outside methods
private Wall2 shield = new Wall2();



//in act
if (shield.getWorld() == null)
{
    if (Greenfoot.isKeyDown("w"))
    {
        getWorld().addObject(shield, getX(), getY());
    }
}
else if (Greenfoot.isKeyDown("s"))
{
    getWorld().removeObject(shield);
}
else
{
    shield.setLocation(getX(), getY());
}
TravJ14 TravJ14

2019/6/3

#
That didn't work. I was getting errors on the setLocation in the else statement and getWorld() in this if statement:
if (Wall1.getWorld() == null)
. I'll send my entire page:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class User here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class User extends Actor
{
    int speed;
    World area = getWorld();
    /**
     * Act - do whatever the User wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("7"))
        {
            setRotation(getRotation() - 3);
        }
        if(Greenfoot.isKeyDown("9"))
        {
            setRotation(getRotation() + 3);
        }
        if(Greenfoot.isKeyDown("enter"))
        {
            fire();
        }
        if(Greenfoot.isKeyDown("enter"))
        {
            World myWorld = getWorld();
            Area area = (Area)myWorld;
        }
        Wall1 shield = new Wall1();
        if (Wall1.getWorld() == null)
        {
            if (Greenfoot.isKeyDown("w"))
            {
                getWorld().addObject(shield, getX(), getY());
            }
        }
        else if (Greenfoot.isKeyDown("s"))
        {
            getWorld().removeObject(shield);
        }
        else
        {
            Wall1.setLocation(getX(), getY());
        }
    }  
    private void fire()
    {
        Bullet1 bullet1 = new Bullet1();
        area.addObject(bullet1, getX(), getY());
    }
}
Super_Hippo Super_Hippo

2019/6/3

#
You didn't do what I suggested. Remove lines 12, 31-36. Add this in line 12:
private Wall1 shield = new Wall1();
Change "Wall1" in lines 37+50 to "shield". (In line 56, use "getWorld()" instead of "area".)
You need to login to post a reply.