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/14

#
I want to add a shield into my game and when the z key is pressed that the shield image rotate to face upwards and be 2 y coordinate above the Warrior and when the c key is pressed that the shield is flipped and put -2 x coordinates to the side of the Warrior1. this is how it looks so far and i can't figure out the right way to code it.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shield here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shield extends Actor
{
        int Warrior1X = getX();
        int Warrior1Y = getY();
    /**
     * Act - do whatever the Shield wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      if (Greenfoot.isKeyDown("z"))
      {
          setRotation(90);
          addObject(new Shield(), setLocation(getX(Warrior1.class), getY(Warrior1.class))+2); 
      }
      if (Greenfoot.isKeyDown("x"))
      {
          setRotation(180);
          addObject(new Shield(), setLocation(getX(Warrior1.class), getY(Warrior1.class))+2); 
      }
      if (Greenfoot.isKeyDown("c"))
      {
          setRotation(270);
          addObject(new Shield(), setLocation(getX(Warrior1.class), getY(Warrior1.class))+2); 
      }
      if (Greenfoot.isKeyDown("v"))
      {
          setRotation(0);
          addObject(new Shield(), setLocation(getX(Warrior1.class), getY(Warrior1.class))+2); 
      }
    }    
}
Super_Hippo Super_Hippo

2017/7/14

#
The code is in the shield class, so the shield executes this. It then adds a new shield object into the world whenever you press a key (despite the fact that the syntax in these lines is not correct). What you should do: Remove the code in the shield class. Have the warrior control the shield. You can save a reference to the shield.
private Shield shield = new Shield();
If the shield should be there immediately, you can add it in the addedToWorld method of the Warrior class. You can then control the shield by calling setLocation on the shield object. So when the warrior moves, it can also move the shield and change the shield position if he changes its direction:
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, getY()+dy);
    setLocation(getX()+dx, getY()+dy);
    if (isTouching(Wall.class)) //for example
    {
        setLocation(getX()-dx, getY()-dy);
    }
    if (shield.getWorld() != null)
    {
        shield.setLocation(getX()+2*dx, getY()+2*dy);
        shield.setRotation(getRotation());
    }
}
Don't forget to add the shield object 'shield' to the world from the Warrior class.
LobsterL LobsterL

2017/7/18

#
when ever i put the code in either the warrior class or shield class the shield isn't generating in the world and it slows down the warrior's speed
danpost danpost

2017/7/18

#
Show the code of the class where you 'addObject' the 'shield' into the world.
LobsterL LobsterL

2017/7/18

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

/**
 * Write a description of class Shield here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shield extends Actor
{
    /**
     * Act - do whatever the Shield wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     GreenfootImage myImage = getImage();
     myImage.scale(50,50);
             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, getY()+dy);
    setLocation(getX()+dx, getY()+dy);
    if (isTouching(Fence.class)) //for example
    {
        setLocation(getX()-dx, getY()-dy);
    }
    if (isTouching(FenceUp.class))
    {
        setLocation(getX()-dx, getY()-dy);
    }
  }
    }    
}
this is my shield class
Super_Hippo Super_Hippo

2017/7/18

#
The code was meant to go into the Warrior class, not the shield class.
Hippo wrote...
What you should do: Remove the code in the shield class. (...)
You did not show from where you add the shield.
LobsterL LobsterL

2017/7/18

#
when i put it in the warrior class it slows down my warrior's speed and the shield is not in the world. so how can i change the speed to 5 so it moves with the warrior.
LobsterL LobsterL

2017/7/18

#
i put the
private Shield shield = new Shield();
in the world class
LobsterL LobsterL

2017/7/18

#
when the code says
if (Greenfoot.isKeyDown("up")) dy--;
  if (Greenfoot.isKeyDown("down")) dy++;
  if (Greenfoot.isKeyDown("right")) dx++;
  if (Greenfoot.isKeyDown("left")) dx--;
should i have the addobject (new shield()) or where would i put it
danpost danpost

2017/7/19

#
LobsterL wrote...
when i put it in the warrior class it slows down my warrior's speed and the shield is not in the world.
Show what code you are using in the Warrior class.
LobsterL wrote...
i put the
private Shield shield = new Shield();
in the world class
It belongs in the Warrior class. You should not have the world create a Shield object as you cannot have a shield without a warrior. Each Warrior object created should create its own Shield object and add it into the world when it goes into the world (if the warriors are always to have a shield with them in the world). Hippo mentioned using the 'addedToWorld' method for this earlier.
so how can i change the speed to 5 so it moves with the warrior.
Each warrior should control its own shield. The shield should not do anything more than its function on its own. That is, the warrior should move the shield around and the shield should only block whatever it is used to protect the warrior from.
Super_Hippo Super_Hippo

2017/7/19

#
To make the warrior (and the shield) move with a speed of 5 instead of 1, you only have to multiply dx and dy with 5 in the setLocation part (in the warrior class).
LobsterL LobsterL

2017/7/20

#
i want the image to remove when the left, right, up and down keys are not pressed and only one can be in the world at one time. can you show me how i would do that.
Super_Hippo Super_Hippo

2017/7/20

#
This will remove the shield if either no key is pressed or right and left and/or up and down = it is removed when the character is not trying to move.
//...
if (dx != 0 || dy != 0)
{
    turnTowards(getX()+dx*speed, getY()+dy*speed);
    setLocation(getX()+dx*speed, getY()+dy*speed);
    if (isTouching(Fence.class) || isTouching(FenceUp.class)) //I don't believe you need a separate FenceUp class
    {
        setLocation(getX()-dx*speed, getY()-dy*speed);
    }
    if (shield.getWorld() == null) getWorld().addObject(shield, getX()+2*dx, getY()+2*dy);
    else shield.setLocation(getX()+2*dx, getY()+2*dy);
    shield.setRotation(getRotation());
}
else getWorld().removeObject(shield);
LobsterL LobsterL

2017/7/24

#
Although this does remove the object once no key is pressed it still doesn't have one object in the world at one point of time it still creates several shields
danpost danpost

2017/7/24

#
LobsterL wrote...
Although this does remove the object once no key is pressed it still doesn't have one object in the world at one point of time it still creates several shields
Please show your current Warrior class codes.
There are more replies on the next page.
1
2