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

2017/3/1

Game stops in in between two values of X?

SachaTb SachaTb

2017/3/1

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

public class Shroom extends Actor
{
    public int gravity = 0;
    GifImage gifLeft = new GifImage("Shroom-Flipped.gif");
    GifImage gifRight = new GifImage("Shroom-Cropped.gif");
    GifImage gifImage = gifRight;
    
     public void act()
   {
        gravity -= 1;
        setLocation(getX(),getY()-gravity);
        setImage("shroom-cropped0.png"); // image when not moving
        checkKeys();
        checkDead();
  }
  public void checkKeys()
  {
      int speed = 3;
      
      if(Greenfoot.isKeyDown("a"))
    {
        setLocation(getX() - speed, getY());
        gifImage = gifLeft;
        setImage(gifImage.getCurrentImage());
    }
    if(Greenfoot.isKeyDown("d"))
    {
        setLocation(getX() + speed, getY());
        gifImage = gifRight;
        setImage(gifImage.getCurrentImage());
    }
    if(Greenfoot.isKeyDown("w"))
    {
     
     if (getY() > 600)
     {
     gravity = 18;
     }
     }
     if("space".equals(Greenfoot.getKey()))
     {
        fire();
     }
  }
   public void checkDead()
  {
    if(getX() = "in between two values of X" )
    {
     Greenfoot.stop();  
    }
  }
   public void fire() 
  {
    Bullet bullet = new Bullet();
    getWorld().addObject(bullet, getX(), getY());
  } 
}

How can i make it that if the actor hits the values of X between (value) and (value) the game stops? as in, if the character is in between these two values of X, its can run something?? help me please!! (help with lines 47-53)
Super_Hippo Super_Hippo

2017/3/1

#
if (getX() >= 10 && getX() <= 20)
{
    //...
}
The condition is met if getX() is between 10 and 20.
SachaTb SachaTb

2017/3/2

#
ok thx!
You need to login to post a reply.