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

2017/7/14

Shield Method

1
2
LobsterL LobsterL

2017/7/24

#
         int dx=0, dy=0;
         if (Greenfoot.isKeyDown("up")) dy--;
         if (Greenfoot.isKeyDown("down")) dy++;
         if (Greenfoot.isKeyDown("right")) dx++;
         if (Greenfoot.isKeyDown("left")) dx--;
         if (dx != 0 || dy != 0)
         {
          turnTowards(getX()+dx*3, getY()+dy*3);
          setLocation(getX()+dx*3, getY()+dy*3);
          if (isTouching(Fence.class) || isTouching(FenceUp.class)) //I don't believe you need a separate FenceUp class
          {
           setLocation(getX()-dx*3, getY()-dy*3);
          }
          if (shield.getWorld() == null) getWorld().addObject(shield, getX()+30*dx, getY()+30*dy);
          else shield.setLocation(getX()+2*dx, getY()+2*dy);
          shield.setRotation(getRotation());
        }
          else getWorld().removeObject(shield);
danpost danpost

2017/7/25

#
No. I need to see the entire class. There is no Shield instantiation in what you gave above.
LobsterL LobsterL

2017/7/25

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

/**
 * Write a description of class Warrior here.
 * 
 * @author (your name) 
 * @version (a version number or a date)

I have commented it out to test other objects in the World without the distraction of the many shields
 */
public class Warrior1 extends Characters
{
    boolean touchingSoceror = false;
    public static int Warrior1X, Warrior1Y;
    private Shield shield = new Shield();
    public Warrior1()
    {
       GreenfootImage myImage = getImage();
       myImage.scale(50,50);
    }
    // public void addedToWorld()
    // {
         // getWorld().addObject(new Shield(), getX(), getY());
         // int dx=0, dy=0;
         // if (Greenfoot.isKeyDown("up")) dy--;
         // if (Greenfoot.isKeyDown("down")) dy++;
         // if (Greenfoot.isKeyDown("right")) dx++;
         // if (Greenfoot.isKeyDown("left")) dx--;
         // if (dx != 0 || dy != 0)
         // {
          // turnTowards(getX()+dx*3, getY()+dy*3);
          // setLocation(getX()+dx*3, getY()+dy*3);
          // if (isTouching(Fence.class) || isTouching(FenceUp.class)) //I don't believe you need a separate FenceUp class
          // {
           // setLocation(getX()-dx*3, getY()-dy*3);
          // }
          // if (shield.getWorld() == null) getWorld().addObject(shield, getX()+30*dx, getY()+30*dy);
          // else shield.setLocation(getX()+2*dx, getY()+2*dy);
          // shield.setRotation(getRotation());
        // }
          // else getWorld().removeObject(shield);
    // }
    /**
     * Act - do whatever the Warrior wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("right"))
        {
          setLocation(getX()+3, getY());
          //addedToWorld();
          if(isTouching (Fence.class)
          || isTouching (FenceUp.class))
          {
              setLocation (getX() -3, getY());
          }
        }
                if (Greenfoot.isKeyDown("up"))
        {
          setLocation(getX(), getY()-3);
          //addedToWorld();
          if(isTouching (Fence.class)
          || isTouching (FenceUp.class))
          {
              setLocation (getX(), getY() +3);
          }
        }
                if (Greenfoot.isKeyDown("left"))
        {
          setLocation(getX()-3, getY());
          //addedToWorld();
          if(isTouching (Fence.class)
          || isTouching (FenceUp.class))
          {
              setLocation (getX() +3, getY());
          }
        }
                if (Greenfoot.isKeyDown("down"))
        {
          setLocation(getX(), getY()+3);
          //addedToWorld();
          if(isTouching (Fence.class)
          || isTouching (FenceUp.class))
          {
              setLocation (getX(), getY()-3);
          }
        }
        if( isTouching(Bomb.class))
         {
             removeTouching(Bomb.class);
             
         }
         Warrior1X = getX();
         Warrior1Y = getY();
    }    
}


danpost danpost

2017/7/25

#
Okay, use of the 'addedToWorld' method was suggested at the time it was thought that the shield was to stay in the world as long as the warrior existed. Even then, only the first line was supposed to be in that method (the rest was for the act method). Because the shield is only in the world when a direction key is down, you will not use that method. All the code you have commented out within that method, except the first line, is to replace all the code in your act method down to line 88.
LobsterL LobsterL

2017/7/26

#
i have done that and it works but how do i spawn the shield in front of the character so it looks like a shield
Super_Hippo Super_Hippo

2017/7/26

#
Those two lines handle that:
if (shield.getWorld() == null) getWorld().addObject(shield, getX()+30*dx, getY()+30*dy);
else shield.setLocation(getX()+2*dx, getY()+2*dy);
You changed the 2's to 30's in the first line, you have to use the same numbers in the second line.
You need to login to post a reply.
1
2